c-generic-ring-buffer icon indicating copy to clipboard operation
c-generic-ring-buffer copied to clipboard

A Bug In Macro

Open thetaQ opened this issue 8 years ago • 3 comments

You should surround your macro by do{...}while(0)

thetaQ avatar Mar 25 '16 09:03 thetaQ

What benefit does this provide?

pthrasher avatar Feb 23 '17 21:02 pthrasher

It will work in cases where you do:

if (whatever == 1)
   MACRO

Which would fail with multiple statements inside the macro (only the first would be guarded by the if-statement). The do-while wrapper makes the macro safe in any context.

niondir avatar Feb 27 '17 15:02 niondir

If someone would like to contribute this with tests, I'll happily pull it in. :-)

pthrasher avatar Oct 23 '19 16:10 pthrasher