Just a minor new twist on free()

Richard Minner rtm at christmas.UUCP
Fri Oct 5 14:39:16 AEST 1990


I personally like all `destroy_thing()' functions to quietly
accept a non-existent (NULL) `thing'.  (After all, what I want
is for the thing to be gone, and if it's already not there,
that's fine with me. :-)

I find all the `if (ptr)' tests cluttersome (not a real word).
If the thing really should not be null, I'll do an assert(thing)
before destroying it.

One particular usage this model simplifies is the multiple create, e.g:

	thing1 = create_thing();
	thing2 = create_thing();
	...
	thingN = create_thing();
	if (!thing1 || !thing2 || ... || !thingN)
	{
		destroy_thing(thing1);
		...
		destroy_thing(thingN);
		<fail>
	}

which to me reads nicely as: "try to get things, if didn't get
them all, get rid of any you got."

As a final bit, I second the recommendation of using your own
`shell' functions around the standard malloc family, rather than
macros; it's more flexible (as noted by ??, sorry) and the overhead
of the `extra' call is (almost always) insignificant compared to the
work the library functions will be doing.

-- 
Richard Minner  || {uunet,sun,well}!island!rtm     (916) 736-1323 ||
                || Island Graphics Corporation     Sacramento, CA ||



More information about the Comp.lang.c mailing list