Contents | Prev | Next | Index


Pointer Types

Variables of a pointer type P assume as values pointers to variables of another type T. T is called the pointer base type of P and must be a structured type (record, array, or set type). Notice that in this compiler, unlike standard Modula-2, pointer to non-structured basic types, enumerations or subranges are not supported because Java references are only possible for structured types.

In the following language grammar for pointer types, language extensions are shown in red.

pointer_type ::= POINTER TO type
             ::= POINTER TO open_array type
open_array   ::= open_array ARRAY OF
             ::= ARRAY OF

If the language extensions for object oriented features are being used, the following applies: Pointer types adopt the extension relation of their pointer base types: if a type T1 is an extension of T, and P1 is of type POINTER TO T1, then P1 is also an extension of P.

If p is a variable of type P = POINTER TO T, a call of the standard procedure NEW allocates memory from a heap and assigns the pointer variable to its address. The allocated memory is a new instance of type T. Standard procedure NEW is called as:

  NEW( p )

This Modula-2 compiler also allows the following extended call of NEW as part of its object oriented language extensions (which can be turned on via a compiler switch or directive):

  NEW( p, x0, x1, ... )

The additional parameters are used in constructor call for Java. In Modula-2, constructors can be declared like regular type-bound procedures except that they are to be preceded by the comment embedded directive (*$JAVA CONSTRUCTOR*).

Also, if the language extensions are enabled via a compiler switch or directive: If the pointer base type is a n-dimensional open array, the allocation has to be done as follows:

  NEW( p, e0, e1, ... )

where the additional actual parameters are integers specifiying the array lengths for each dimension.

An operand p designates a variable of its pointer type, whereas an operand p^ designates a variable of pointer base type T whose instance was allocated from the heap.

Any pointer variable may assume the value NIL, which points to no variable at all.

There is no need for explicitly deallocating memory blocks because this is automatically done by Java's garbage collection of the heap. Standard procedure DISPOSE(p) is still available, though. It only sets the pointer variable p to NIL, assuming that Java's garbage collection for unused memory blocks will eventually handle the rest.


Contents | Prev | Next | Index

Canterbury Modula-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com