: I find that I still use the:
:
: if( foo )
: free( foo );
:
:...syntax a large part of the time. It's a habit developed before you
:could trust the free() implementation on every platform to conform to
:sanity. It also prevents me from getting into the habit of using syntax
:that will get me into trouble when programming in embedded environments
:& kernels. In any case, this style doesn't raise the "what the hell?"
:flag the way a number of other things do.
:
:Jack
I go one step further and have a routine which NULLs out the
pointer variable itself.
routine()
{
...
safe_free(&ptr);
...
}
void
safe_free(void **ptr)
{
if (*ptr) {
free(*ptr);
*ptr = NULL;
}
}
And for malloc, so I don't have to check the return value
in the hundreds of places I call it:
void *
safe_malloc(int bytes)
{
void *ptr;
if ((ptr = malloc(bytes)) == NULL)
*(int *)0 = 1; /* force seg fault */
return(ptr);
}
And for asprintf(), same deal. It isn't much use if you call it
a billion times and have to check the return value every time.
int
safe_asprintf(char **pptr, const char *ctl, ...)
{
va_list va;
int r;
va_start(va, ctl);
r = vasprintf(pptr, ctl, va);
va_end(va);
if (r < 0)
*(int *)0 = 1; /* force seg fault */
return(r);
}
-Matt
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message