modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Type declarations

Pointer types

Pointer types have values that are memory addresses. A pointer type is bound to a specific object type, so the value of the pointer is the address of an object of a specific type.

To specify pointer types, use:

POINTER TO TypeName

Example:

POINTER TO CHAR

The constant NIL is compatible with all pointer types, and designates a pointer that does not point to any object.  NIL can be assigned to any pointer type, and any pointer type can be compared to NIL.

Allocating pointers

Generally, pointers are set by allocating a block of memory for the object that they point to.  The NEW standard procedure and the ALLOCATE procedure in the ISO module Storage performs this function. You can deallocate the memory with the DISPOSE standard procedure, or the DEALLOCATE procedure. The ALLOCATE and DEALLOCATE procedures are exported from the ISO module Storage.

Dereferencing pointers

Dereferencing is used to refer to the object that a pointer variable points to. You dereference a pointer as follows:

variable^

Note: Be sure to assign a value to a pointer before dereferencing it. If an uninitialized pointer is dereferenced, it might refer to any location in memory which will probably result in a runtime error.


Source:

  • Wirth N: Programming in Modula-2, 3rd ed. Springer Verlag, Berlin, 1985.
  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.