Long and Short Pointers [ HP C/iX Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Reference Manual
Long and Short Pointers
HP C defines two classes of data pointers: short and long pointers. A
short pointer is a 32-bit pointer that contains the offset of an object
local to its process. A long pointer is a 64-bit pointer that may point
to an object outside its current process space. The high-order 32 bits
of a long pointer contain a space ID number, and the low-order 32 bits
represent an offset within the space defined by that space ID.
A short pointer can point to any addressable object within its own
process space. This includes local and global variables, function
parameters, and heap variables.[REV END]
[REV BEG] Long pointers may point to
any addressable object on the system. This includes objects that are
outside the space of the current process. Long pointers are useful for
calling system intrinsics that require long pointer parameters (such as
PRINT), and for accessing user-mapped files.
[REV END]
Miscellaneous Pointer Features
* Pointers to functions should not be compared using relational
operators because the pointers represent external function labels
and not actual addresses.
* Dereferencing a pointer that contains an invalid value results in
a trap if the address references protected memory or if the
address is not properly aligned for the object being referenced.
* A declaration of a pointer to an undefined structure tag is
allowed, and the tag need not be defined in the source module
unless the pointer is used in an expression.
* fp() is equivalent to (*fp) () in an expression when fp is of type
pointer to function.
Declaring Long Pointers
You declare long pointer variables or parameters using the normal (short
pointer) syntax except you need to substitute a caret (^) for the
asterisk(*). Refer to chapter 3 for information on declaring pointers.
For example:
/* int_pointer is a long pointer to an integer */
int ^int_pointer;
/* char_pointer is a long pointer to a character */
char ^char_pointer;
Long pointers may not be declared with initializers. Therefore, a
statement such as:
char ^long_ptr = "some string";
is not allowed. Also, long function pointers are not allowed.
Using Long Pointers
Long pointers are dereferenced the same as short pointers. If p is
declared as a long pointer to an int, such as int ^p;, then *p returns
the contents of that integer. If p is declared as a pointer to a
structure containing a member mem, p->mem will return the contents of
that member.
To produce a long pointer that points to the same object as a short
pointer, you can use an assignment statement. For example, after the
assignment statement in the following C code, long_ptr and short_ptr both
point to the same object.
int ^long_ptr; /* long_ptr is a long pointer to an integer */
int *short_ptr; /* short_ptr is a short pointer to an integer */
.
.
.
long_ptr = short_ptr;
After the assignment statement, the low-order 32 bits of the long pointer
are identical to the original short pointer.
A short pointer may not be assigned the contents of a long pointer unless
the long pointer points to an object within the space of the current
process.
You can apply the unary operator & to a long pointer to get the address
of the pointer. The resulting type of the operation &long_ptr is a short
pointer. The following example assigns the address of a long pointer to
a variable declared as a short pointer to a long pointer.
int ^long_ptr;
int ^*sprt_to_lptr;
sptr_to_lptr = &long_ptr;
Note that the variable sptr_to_lptr must be declared as a short pointer
to a long pointer because the type of &long_ptr is a short pointer.
Standard pointer arithmetic and pointer subscripting may be applied to
long pointers, but only the low-order 32 bits are used and modified.
Comparison of long pointers using the relational operators <, <=, >, and
>= compares only the low order 32 bits, the offset portion. The high
order 32 bits, the ID portion, are ignored. Comparison of long pointers
using the relational operators, == and !=, compares both the low order 32
bits and the high order 32 bits.
Assignment of zero (NULL) to a long pointer assigns zero to the high
order 32 bits and to the low order 32 bits.
Casts can be applied to long pointers but you must observe the standard
alignment restrictions. For example, a long pointer to a character
should not be cast to a long pointer to an integer because an integer has
more restrictive alignment requirements than a character. Integers are
aligned on 4 byte boundaries and characters are aligned on byte
boundaries.
There are no standard conversion rules for passing long pointer
parameters to other routines. Therefore, no implicit conversions are
performed when these parameters are passed. That is, short pointers are
not converted to long pointers when a function call is made, and vice
versa.
All pointers passed to standard C library routines, including the heap
manager routines, must be short pointers.
[REV
BEG]
MPE/iX 5.0 Documentation