Array

From LQWiki
Jump to navigation Jump to search

An array is a data structure made up of a numbered series of smaller data structures.

Typically, an array is allocated as a contiguous block of memory, and the individual elements of the array are accessed by providing an index into the array. An array allows random access in O(1) time to its elements, an advantage over more complex data structures. The disadvantage is that resizing an array is generally impossible or expensive. Strings are generally represented in memory with arrays.

  • In languages like C, there is nothing more to the array; there is no checking to see whether the index is within the bounds of the array, which can lead to buffer overflow bugs.
  • In languages like Ada, extra data is stored in the array, allowing run-time checks of array indices for every access, and forcing an exception if an out-of-bounds access is attempted.
  • In languages like Perl, bounds checking is also performed, but an out-of-bounds access results in the runtime automatically growing the array to accommodate the element.