| |||||||||
A parallel array is a simple data structure for representing arrays of records. It keeps a separate, homogenous array for each field of the record, each of the same size. Then, objects located at the same index in each array are implicitly the fields of a single record. This contrasts with the normal approach of storing all fields of each record together in memory. For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.
Parallel arrays have a number of practical advantages over the normal approach:
However, parallel arrays also have several strong disadvantages, which serves to explain why they are not generally preferred:
The bad locality of reference is the worst issue. However, a compromise can be made in some cases: if a structure can be divided into groups of fields that are generally accessed together, an array can be constructed for each group, and its elements are records containing only these subsets of the larger structure's fields. This is a valuable way of speeding up access to very large structures with many members, while keeping the portions of the structure tied together. An alternative to tying them together using array indexes is to use references to tie the portions together, but this can be less efficient in time and space. Some compiler optimizations, particularly for vector processors, are able to perform this transformation automatically when arrays of structures are created in the program.