Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

Noncompliant Code Example

Wiki MarkupIn this noncompliant code example, the programmer has written a macro called {{EXEC_BUMP()}} to call a specified function and increment a global counter \[ [Dewhurst 2002|AA. Bibliography#Dewhurst 02]\]. When the expansion of a macro is used within the body of a function, as in this example, identifiers refer to the declarations in scope where the body occurs. As a result, when the macro is called in the {{aFunc()}} function, it inadvertently increments a local counter with the same name as the global variable. Note that this example also violates recommendation [DCL01-C. Do not reuse variable names in subscopes].

Code Block
bgColor#FFCCCC
langc
size_t count = 0;

#define EXEC_BUMP(func) (func(), ++count)

void g(void) {
  printf("Called g, count = %zu.\n", count);
}

void aFunc(void) {
  size_t count = 0;
  while (count++ < 10) {
    EXEC_BUMP(g);
  }
}

...

Platform-Specific Details

Wiki MarkupGNU C (and some other compilers) supported inline functions before they were added to C99 and, as a result, have significantly different semantics. Richard Kettlewell provides a good explanation of differences between the C99 and GNU C rules \[ [Kettlewell 2003|AA. Bibliography#Kettlewell 03]\].

Exceptions

PRE00-EX1: Macros can be used to implement local functions (repetitive blocks of code that have access to automatic variables from the enclosing scope) that cannot be achieved with inline functions.

...

Tool

Version

Checker

Description

Section

LDRA tool suite

Include Page
c:LDRA_Vc:
LDRA_V
Section

340 S

Section

Fully Implemented

...

MISRA Rule 19.7

Bibliography

...

\[[FSF 2005|AA. Bibliography#FSF 05]\] Section 5.34, "[An Inline Function Is as Fast as a Macro|http://gcc.gnu.org/onlinedocs/gcc/Inline.html]" \[[Dewhurst 2002|AA. Bibliography#Dewhurst 02]\] Gotcha #26, "#define Pseudofunctions" \[[Kettlewell 2003|AA. Bibliography#Kettlewell 03]\] \[[Summit 2005|AA. Bibliography#Summit 05]\] Question "
[Dewhurst 2002] Gotcha #26, "#define Pseudofunctions"
[Kettlewell 2003]
[Summit 2005] Question 10.4

...

01. Preprocessor (PRE)      01. Preprocessor (PRE)