Possible Duplicates:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
do { … } while (0) what is it good for?
I'm working on some C code filled with macros like this:
#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
Can anyone explain what this macro does, and why do {} while(0)
is needed? Wouldn't that just execute the code once?
SAFE_FREE(get_buffer())
won't compile. Something to think about when creating macros like this. – Dan Moulding Apr 22 '10 at 1:13if ((x) != NULL)
is redundant. you can remove that safely.free(NULL)
does nothing so why waste oneif
over it? – N 1.1 Apr 22 '10 at 1:27