...
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: Don't Do not immediately cast the results of malloc()
when compiling as C90 or earlier because it is possible for the cast to hide a bug in platforms where pointers are larger than ints.
For example, if 64-bit applications. If stdlib.h
is not properly included, the compiler compilers will implicitly declare malloc()
as int malloc(int)
. If the sizeof(int)
on size of int
on the platform is 4, such as on Microsoft Windows32 bits, but the size of a pointer is 64 bits, the resulting pointer could be truncated due to the implicit declaration of malloc()
returning a 32-bit integer.
...
When compiled with Microsoft Visual Studio, the above code will eventually cause an access violation when dereferencing ptr
in the loop. Note that the this code also does not comply with DCL31-C. Declare identifiers before using them by using an implicit declaration of malloc()
.
...