[LCP] Realloc and null pointers

Bill Rausch BRausch at owt.com
Thu Feb 12 03:11:54 EST 2009


At 02:01 PM -0900 2/10/09, Christopher Howard wrote:
>Let's say I declare a char pointer:
>
>   char * label;
>
>Can I simply allocate memory to it like so?:
>
>   realloc(label, 100*sizeof(char));
>
>Or do I need to malloc it first?

If label is a global, then it is already null. If it is a local 
variable, then it has a random value and you need to set it:  char* 
label = NULL;

realloc is quite powerful but prone to accidentally doing things you 
don't expect so be careful with it.  Explicitly calling malloc and 
free is normally better practice.

Bill

-- 
Bill Rausch

We first make our habits and then our habits make us. --John Dryden



More information about the linuxCprogramming mailing list