Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: put macro in a block and used reserved identifiers

...

Some implementations provide language extensions that make it possible to define safe function-like macros, such as the macro ABS(), that would otherwise require evaluating their arguments more than once. For example, the GCC extension Statements and Declarations in Expressions makes it possible to implement the macro ABS() in a safe way. Note, however, that because relying on implementation-defined extensions introduces undesirable platform dependencies that may make the resulting code nonportable, such solutions should be avoided in favor of portable ones wherever possible. (See MSC14-C. Do not introduce unnecessary platform dependencies.)

Another GCC extension known as statement expression makes it possible for the block statement to appear where an expression is expected:

 

Code Block
bgColor#ccccff
langc

#define ABS(x) __extension__ ({ __typeof (x) __tmp = (x); __tmp < 0 ? - __tmp : __tmp; })

Risk Assessment

Defining an unsafe macro leads to invocations of the macro with an argument that has side effects, causing those side effects to occur more than once. Unexpected or undefined program behavior can result.

...