Passing Addresses

Previous releases of Quintus Prolog had the restriction that integers of greater than 29 bits could not be represented as Prolog integers. Certain platforms, however, have pointers that use some of the four most significant bits; for these machines, pointers could not be represented as Prolog integers. This problem motivated the address, which could be treated specially, as a distinct data type that can be passed through the foreign interface.

With release 3 the restriction of integers to 29 bits has been lifted; however, the internal representation of integers of more than 29 bits is more bulky and somewhat slower to manipulate than that of smaller integers. While this is not a problem in normal programs, it could penalize programs that manipulate pointers in Prolog on certain platforms whose pointers require more than 29 bits. We have chosen to retain the address data type in release 3 so that such penalties can be avoided where possible, as well as for backward compatibility. Addresses can be passed both to and from Prolog from foreign functions, and to and from foreign functions from Prolog. (See fli-p2f-poi.)

As when calling foreign code from Prolog, pointers should be passed through the interface using the type specification


as described in more detail below. typename should be the name used in the foreign language to identify the type of object named by the pointer.

     Prolog:  +address(typename)
     C:       typename *x;

The C pointer is converted to a 32-bit Prolog integer, which is passed to the Prolog call. If the C pointer contains garbage when it is passed, Prolog will receive that garbage as an integer.

     Prolog:  -address(typename)
     C:       typename **x;

A pointer to a C pointer is passed to the foreign interface. When Prolog returns a solution, a Prolog integer is expected in the corresponding argument of the call. If the argument is 0, the foreign function writes the NULL pointer at the location supplied. Otherwise, the foreign interface converts the integer into a C pointer and writes it at the location. The previous contents of the location will be destroyed. If the Prolog call does not return an integer in the appropriate position, a type error is signaled and the contents of the location is undefined.

     Prolog:  +address
     C:       char *x

This is equivalent to +address(char).

     Prolog:  -address
     C:       char **x

This is equivalent to -address(char).

Using +address in place of +address(typename), or -address in place of -address(typename), has no effect on the execution of the program; however, doing so can reduce the readability of the program and compromise program checking tools.