Concepts of Programming Languages

(Sean Pound) #1
6.11 Pointer and Reference Types 297

All variables in the object-oriented languages Smalltalk, Python, Ruby, and
Lua are references. They are always implicitly dereferenced. Furthermore, the
direct values of these variables cannot be accessed.

6.11.7 Evaluation


The problems of dangling pointers and garbage have already been discussed at
length. The problems of heap management are discussed in Section 6.11.8.3.
Pointers have been compared with the goto. The goto statement widens the
range of statements that can be executed next. Pointer variables widen the range
of memory cells that can be referenced by a variable. Perhaps the most damning
statement about pointers was made by Hoare (1973): “Their introduction into
high-level languages has been a step backward from which we may never recover.”
On the other hand, pointers are essential in some kinds of programming
applications. For example, pointers are necessary to write device drivers, in
which specific absolute addresses must be accessed.
The references of Java and C# provide some of the flexibility and the
capabilities of pointers, without the hazards. It remains to be seen whether
programmers will be willing to trade the full power of C and C++ pointers for
the greater safety of references. The extent to which C# programs use pointers
will be one measure of this.

6.11.8 Implementation of Pointer and Reference Types


In most languages, pointers are used in heap management. The same is true
for Java and C# references, as well as the variables in Smalltalk and Ruby, so
we cannot treat pointers and references separately. First, we briefly describe
how pointers and references are represented internally. We then discuss two
possible solutions to the dangling pointer problem. Finally, we describe the
major problems with heap-management techniques.

6.11.8.1 Representations of Pointers and References
In most larger computers, pointers and references are single values stored in
memory cells. However, in early microcomputers based on Intel micropro-
cessors, addresses have two parts: a segment and an offset. So, pointers and
references are implemented in these systems as pairs of 16-bit cells, one for
each of the two parts of an address.

6.11.8.2 Solutions to the Dangling-Pointer Problem
There have been several proposed solutions to the dangling-pointer problem.
Among these are tombstones (Lomet, 1975), in which every heap-dynamic
variable includes a special cell, called a tombstone, that is itself a pointer to the
heap-dynamic variable. The actual pointer variable points only at tombstones
Free download pdf