On 2012-12-16 13:00:33 -0800, Michael Elkins wrote:
> On Sun, Dec 16, 2012 at 11:45:37AM -0600, Derek Martin wrote:
> >- Calling free(NULL) is a noop--perfectly safe (at least since ANSI
> >  C, I don't have a copy of K&R handy)
> 
> I don't remember the reason for this.  Back in 1995 when I started writing
> it I'm not sure that all target platforms had ANSI C support.

On

http://stackoverflow.com/questions/1938735/does-freeptr-where-ptr-is-null-corrupt-memory

it is said that PalmOS crashed on free(NULL). Also, even nowadays,
Android doesn't have full ISO C support (I don't know about free(NULL)
on it, though). Them it's up to you to choose to support non-conforming
implementations or not...

> >- the extra cast seems silly; ptr is required to be **ptr, it should
> >  be declared that way.
> >
> >I'm not much of a fan of function-like macros in general, but this
> >case seems like exactly when you'd want to use one.  I think this
> >should be replaced with:
> >
> >#define FREE(x)\
> >     free((x));\
> >     x = NULL
> 
> It should be free(*(x)), since a ** is passed.  And if you want to avoid a
> function, you need to make sure it can be called in and if- or else- block
> without the braces, like this:
> 
> #define FREE(x)\
>       do { free(*(x)); *x = NULL } while(0)

#define FREE(x)\
        do { free(*(x)); *(x) = NULL; } while(0)

or

#define FREE(x)\
        do { void **_x = (x); free(*_x); *_x = NULL; } while(0)

to avoid double evaluation of x (though it should be clear that
one mustn't use FREE on an expression with side effects).

> At this point you may well just consider using an inlined function.

Yes.

-- 
Vincent Lefèvre <[email protected]> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

Reply via email to