1

Possible Duplicate:
Do-While and if-else statements in C/C++ macros

I have the following macros :

#define FREE1(x) do { free(x); x = NULL; } while (0)
#define FREE2(x) free(x); x = NULL

What is the difference between these macros?

Community
  • 1
  • 1
developer
  • 4,744
  • 7
  • 40
  • 55

2 Answers2

4

It might be more satisfying to figure this one out yourself.

Hint:

if(y) FREE2(x);
geocar
  • 9,085
  • 1
  • 29
  • 37
4

Consider the following example

if (some_test) FREE2(x);

expands to

if (some_test)
    free(x);
x = NULL; // happens regardless of value of some_test
simonc
  • 41,632
  • 12
  • 85
  • 103