In 64-bit mode, pointers and int types are no
longer the same size. The implications of this are:
- Exchanging pointers and int types causes segmentation
faults.
- Passing pointers to a function expecting an int type
results in truncation.
- Functions that return a pointer, but are not explicitly prototyped
as such, return an int instead and truncate the resulting
pointer, as illustrated in the following example.
Although the following code construct is valid in 32-bit mode:
a=(char*) calloc(25);
Without a function prototype for calloc, when
the same code is compiled in 64-bit mode, the compiler assumes the
function returns an int, so a is
silently truncated, and then sign-extended. Type casting the result
does not prevent the truncation, as the address of the memory allocated
by calloc was already truncated during the return.
In this example, the correct solution is to include the
header file, stdlib.h, which contains the prototype
for calloc.
To avoid these types of problems:
- Prototype any functions that return a pointer.
- Be sure that the type of parameter you are passing in a function
(pointer or int) call matches the type expected by
the function being called.
- For applications that treat pointers as an integer type, use type long or unsigned
long in either 32-bit or 64-bit mode.