free (NULL);

Dan Kogai c60c-3cf at e260-3f.berkeley.edu
Thu May 10 08:07:56 AEST 1990


In article <19461 at duke.cs.duke.edu> drh at cs.duke.edu writes:
>In article <1194 at wet.UUCP> noah at wet.UUCP (Noah Spurrier) writes:
>>
>>Is there anything wrong with freeing a NULL? pointer?
>>[Example code ommited]
>>This function is run many times so iI do not want to protect it with an if
>>because the if would only be useful for the first time it is run, after that
>>it just eats up run time.
>1.  Different implementation do different things with NULL pointers
>    passed to free().  I find it best to assume that it is illegal
>    to not give a null pointer to free().

	Yep.  And it's really annoying.  Sun manual said nothing about
what's gonna happen--segfault?  Just do nothing safely.  I don't have
ANSI draft handy but ANSI should've included some "specs" in case of NULL
pointer if it hadn't.

>2.  The amount of time used by an "if" statement to protect the free(),
>    is insignificant compared to the amount of time used by free() itself,
>    and is REALLY insignificant compared to the amount of time used
>    by malloc().  Use an "if".

	Very true.  Usually I write higher level function to release memory
because in some cases structure contains substructure which is malloc'ed
and substructure cannot be freed like the following case:

typedef struct Baz{
	char *foo
	char *bar
}Baz, *baz;
/* ... */
baz blech = (baz)malloc(sizeof(Baz));
blech->foo = (char *)malloc(FOOSIZE);
blech->bar = (char *)malloc(BARSIZE);
/* ... */
free(blech);	/* foo and bar remain unfreed */

	If it's simple and contains no substructure, I usually use following
macro:

#define trash(foo)	if (foo != NULL) free(foo)

>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;
>    }

	Or allocate externally.  If externally allocated you don't even
have to write a function for it.

>4.  If you are doing something more complex than your example shows,
>    consider using "realloc()".

	Plus make sure the object is already allocated by [mc]alloc().  Again
I'm not sure what's gonna happen if realloc gets NULL object as an arg.
Also don't forget realloc might fail--that happened to me.

Happy coding!
---
##################  Dan The "free" Man
+ ____  __  __   +  (Aka Dan Kogai)
+     ||__||__|  +  E-mail:     dankg at ocf.berkeley.edu
+ ____| ______   +  Voice:      415-549-6111
+ |     |__|__|  +  USnail:     1730 Laloma Berkeley, CA 94709
+ |___  |__|__|  +              U.S.A
+     |____|____ +  Disclaimer: I'd rather be blackmailed for my long .sig
+   \_|    |     +              than give up my cool name in Kanji. And my
+ <- THE MAN  -> +              citizenship of People's Republic o' Berkeley
##################              has nothing 2 do w/ whatever I post, ThanQ.



More information about the Comp.lang.c mailing list