Assigning pointers

In 64-bit mode, pointers and int types are no longer of the same size. The implications of this are as follows:

  • 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.
    In C, the following code is valid in 32-bit mode without a prototype:
    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 best solution is to include the header file, stdlib.h, which contains the prototype for calloc. An alternative solution is to prototype the function as it is in the header file.

To avoid these types of problems, you can take the following measures:

  • Prototype any functions that return a pointer, where possible by using the appropriate header file.
  • Ensure 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.
  • XL-based front end beginsUse the -qwarn64 option to get warning messages in the listing file about potential problems.