[vox-tech] Nifty macro expansion bug

Bill Kendrick nbs at sonic.net
Thu Jan 18 16:14:22 PST 2007


So at work, a coworker noticed an issue with the following code:

  for (i = 0; i < n; i++)
    myFREE(foo[i]);

which went away when he wrapped it in braces:

  for (i = 0; i < n; i++)
  {
    myFREE(foo[i]);
  }

Figured maybe it was a weird compiler bug.  Being author of the myFREE()
code -- which happens to be a macro -- I realized immediately what I did
wrong.  (And continue to be annoyed with C syntax ;) )

See, myFREE() is actually a macro that looks like this:

  #define myFREE(x) if ((x) != NULL) FREE(x); x = NULL;

Two statements... hence the need for {} around the call to the macro.
(Or, more sensibly, including {}s around what the macro expands to.)

That was a neat catch. :)

-- 
-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/


More information about the vox-tech mailing list