`static foo array[SIZE]' is not `malloc()' (Re: free (NULL);)

John S. Price john at stat.tamu.edu
Thu May 10 02:14:23 AEST 1990


In article <5804 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
>In article <19461 at duke.cs.duke.edu> drh at cs.duke.edu writes:
>>[stuff deleted]
>>    char *squirl()
>>    {
>>       static char test[100];
>>       return test;
>>    }
>
>Using this function the array test[] will be allocated
>exactly once during the execution of the program, and the
>same value will be returned no matter how many times
>squirl() is called.  If you want two blocks of 100 char's
>simultaneously, this won't work.  
>[stuff deleted]
>
>				--Blair

The original won't work if you want two blocks of 100 char's
simultaneously, for it frees the old block before it allocates
the new block.  So, the solution that was suggested is identical
to the first to the caller.  A pointer is returned in both
cases, a pointer to 100 chars.  I am still curious as to why
someone would want to allocate 100 chars with malloc, and 
free that space before you allocate some more.  If all you
want is space for 100 chars, just use a static array like in 
the above example.  I am assuming that this space is re-assigned
each time the function is called...


--------------------------------------------------------------------------
John Price                   |   It infuriates me to be wrong
john at stat.tamu.edu           |   when I know I'm right....
--------------------------------------------------------------------------



More information about the Comp.lang.c mailing list