...
In the following noncompliant code example, if malloc()
is not declared, either explicitly, or by including stdlib.h
, a compiler may implicitly declare malloc()
as int malloc()
. (Compilers that only comply with C90 are required to provide an implicit declaration of malloc()
.) If the platform's size of int
is 32 bits, but the size of pointers is 64 bits, the resulting pointer could be truncated as a result of the implicit declaration of malloc()
returning a 32-bit integer.
Code Block |
---|
|
/* #include <stdlib.h> is missing */ |
int
for (i = 0; i < 100; ++i) |
{
char
{
char *ptr = (char*)malloc(0x10000000); /* int malloc() assumed */ |
}
return
0;
When compiled with Microsoft Visual Studio (a C90-only platform), the above code will eventually cause an access violation when dereferencing ptr
in the loop.
...