Java Reference
In-Depth Information
Array Values for
examScore
Size of examScore
Pointer to
examScore
Frame for P
Runtime Stack
Figure 12.10: Allocation of a Dynamic Array
in Figure 12.10. Within P's frame we allocate space for a pointer to examScore's
values as well as it size.
Accessing a dynamic array requires an extra step. First the location of the
array is loaded froma global address or from an o
ff
set within a frame. Then, as
usual, an o
ff
set within the array is computed and added to the array's starting
location.
A variant of the dynamic array is the flex array , which can expand its
bounds during execution. (The Java Vector class implements a flex array.)
When a flex array is created, it is given a default size. If during execution an
index beyond the array's current size is used, the array is expanded to make
the index legal. Since the ultimate size of a flex array is not known when the
array is initially allocated, we store the flex array in the heap, and point to it.
Whenever a flex array is indexed, the array's current size is checked. If it is
too small, another larger array is allocated, values from the old allocation are
copied to the new allocation, and the array's pointer is reset to point to the
new allocation.
When a dynamic or flex array is passed as a parameter, it is necessary to
pass an array descriptor that includes a pointer to the array's data values as
well as information on the array's bounds and size. This information is needed
to support indexing and assignment.
12.3.2 Multidimensional Arrays
In most programming languages multidimensional arrays may be treated as
arrays of arrays . In Java, for example, the declaration:
int matrix[][] = new int[5][10];
 
 
Search WWH ::




Custom Search