Potential pointer corruption
When porting a program from ILP32 to LP64, be aware of the following potential problems:
- An invalid address might be the result of either of the following
actions:
- Assigning an integer (4 bytes) or a 4-byte hexadecimal constant to a pointer type variable (8 bytes)
- Casting a pointer to an integer type
Note: An invalid address causes errors when the pointer is dereferenced. - If you compare an integer to a pointer, you might get unexpected results.
- Data truncation might result if you convert pointers to signed or unsigned integers with the expectation that the pointer value will be preserved.
- If return values of functions that return pointers are assigned to an integer type, those return values will be truncated.
- If code assumes that pointers and integers are the same size (in an arithmetic context), there
will be problems. Pointer arithmetic is often a source of problems when migrating code. The ISO C
standard dictates that incrementing a pointer adds the size of the data type to which it points to
the pointer value. For example, if the variable
p
is a pointer tolong
, the operation(p+1)
increments the value ofp
by 4 bytes (in 32-bit mode) or by 8 bytes (in 64-bit mode). Therefore, casts betweenlong*
andint*
are problematic because of the size differences between pointer objects (32 bits versus 64 bits).