Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

An unsafe macro function is one that evaluates a parameter more than once in the code expansion. Never invoke an unsafe macro with arguments containing an assignment, increment, decrement, volatile access, or other side effects including function calls which may cause side effects. Any input or output is also a side effect, even though it might be accomplished through function calls or volatile access. Thus input and output must similarly be avoided in arguments to unsafe macros. Any input or output is also a side effect, although this is commonly accomplished through function calls or volatile access.

The documentation for unsafe macros must warn about putting side effects on the invocation, and the responsibility is on the programmer using the macro. Because of the risks associated with their use, it is recommended that you avoid the creation of unsafe macro functions. See also PRE00-A. Prefer inline or static functions to function-like macros.

...

Code Block
bgColor#ccccff
#define ABS(x) (((x) < 0) ? -(x) : (x)) /* UNSAFE */
/* ... */
++n;
m = ABS(n);

Note the comment declaring the macro unsafe as a warning for programmers.

A second, preferable, compliant solution is to declare ABS() as an inline function (see PRE00-A. Prefer inline or static functions to function-like macros).

...