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

Blair P. Houghton bph at buengc.BU.EDU
Thu May 10 01:14:32 AEST 1990


In article <19461 at duke.cs.duke.edu> drh at cs.duke.edu writes:
>3.  If you are always freeing and mallocing a chunk of memory the same
>    size, then why not just allocate a static array and not mess with free()
>    and malloc() at all.  Such will accomplish exactly the same thing
>    as free() followed by malloc(), but with considerably fewer machine
>    cycles.  Example:
>
>    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.  Its only redeeming
feature is that it hides the allocation and address-taking
of the array test[] from the caller, and doesn't make one
suspect that it's an array type.  That is, if you consider
that a redeeming feature, which it might be if the allocation
is couched in #ifdef's and other implementation-dependencies.
I prefer to think of it as an obfuscation.

Use malloc() and free().

				--Blair
				  "Use Texaco System3.
				   Just five tanks is all we ask.
				   Just five measly 11-18 gallon tanks.
				   Just five tanks of $1.20-$1.40 gasoline.
				   Just $108-252 over the next 5-8 weeks..."



More information about the Comp.lang.c mailing list