Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated based on feedback from David Svoboda

...

The use of type-generic function-like macros is an allowed exception (PRE00-EX4) to PRE00-C. Prefer inline or static functions to function-like macros.

...

Exceptions

MEM02-EX1: When compiling a 64-bit application on an LP64 or LLP64 platform, this recommendation should not be used because Don't immediately cast the results of malloc when compiling as C90 or earlier because it is possible for the cast to hide a bug in 64-bit applications.  If stdlib.h is not properly included, the compiler will assume the declaration of implicitly declare malloc() to be  as int malloc()When If the sizeof(int) on the platform is 4, such as on Microsoft Windows, the resulting pointer could be truncated due to the compiler assuming implicit declaration of malloc() returns a 32-bit integer.  Additionally, casting the results of malloc() to a pointer on these platforms can also sign extend a negative returning a 32-bit integer to an invalid pointer.

Code Block
bgColor#FFcccc
langc
/* #include <stdlib.h> is missing */
 
int main() {
  int i;
  for (i = 0; i < 100; ++i) {
    char *ptr = (char*)malloc(0x10000000); /* int malloc() assumed */
    *ptr = 'a';
  }
  return 0;
}

On an LLP64/LP64 platform, such as Microsoft WindowsWhen compiled with Microsoft Visual Studio, the above code will eventually cause an access violation when dereferencing ptr in the loop.  Note that the code also does not comply with DCL31-C. Declare identifiers before using them by using an implicit declaration of malloc().

...