Definition
An array is a data structure that stores a sequence of elements of one type in one contiguous block of memory and identifies each element by an integer index, counted from 0. Because the elements are equal in size and adjacent, the address of element is the start address plus times the element size, so any element is reached in constant time. A fixed-size array has a capacity set at creation; a dynamic array also records a logical length, its number of valid elements, and can grow.
The idea
A program reserves room for 16 sensor readings and has recorded 3 so far. Memory for all 16 exists, but only the first 3 slots hold readings; the other 13 hold whatever bytes were there before, perhaps zeros, perhaps data from an earlier use of that memory. A loop that runs up to 16 reports 13 meaningless values and may expose old data. So an array that is not always full needs two numbers: how many slots exist, the capacity, and how many hold valid elements, the logical length.
Arrays are used for their address arithmetic: because all elements have the same size and sit side by side, finding element is a calculation, not a search. A row of numbered lockers is a fair picture: locker 7 is found by counting along the row, without opening lockers 0 to 6. The picture stops at the unused lockers. An unused array slot is not empty; it holds some pattern of bits, and nothing in the memory marks it as unused. Only the length says which slots are valid.
How it works
An array is described by these quantities:
base address : the address of element 0;
element size : the number of bytes each element takes; element starts at address , a computation called pointer arithmetic §4.25;
capacity : the number of element slots for which memory is reserved, bytes in all;
logical length : the number of slots, counted from index 0, that hold valid elements; the valid elements occupy indices 0 to , and .
A fixed-size array, such as int a[16] in C, has no length of its own; a program that fills it only partly must keep the length in a separate variable. A dynamic array, such as std::vector in C++, ArrayList in Java or the list type of Python §12.23, keeps the base address, length and capacity together and grows when full.
The operations follow from that layout.
Read or write element : check , against the length, not the capacity; then use the address .
Append: if , write the new element into slot first and only then increase by 1, so that the length never counts an unwritten slot. If , grow first.
Grow: reserve a new block, usually twice as large; copy the valid elements into it; release the old block; record the new base address and capacity. With growth by a constant factor, any appends cost at most a constant times , although one append may copy every element; the amortized analysis §14.4 entry proves this.
Insert or delete at index : shift every later element one slot right or left, which moves or elements.
Remove the last element: decrease by 1. The removed element’s bytes stay in memory until overwritten.
Two further consequences of the layout matter in practice. A pass from index 0 upward reads memory in address order, which processor caches (small fast memories that hold recently used data and the data next to it) serve well; a linked list §14.6, whose elements are scattered across memory, is usually much slower to scan. And a two-dimensional array of rows with elements each, stored row after row as C stores it, places element at address .
Worked example
An array starts at the illustrative address 1000, holds 4-byte integers, and has capacity 16 and length 3.
The reserved block occupies bytes, addresses 1000 to 1063.
Element 2 starts at and occupies addresses 1008 to 1011.
The 3 valid elements occupy bytes, addresses 1000 to 1011.
Index 10 gives the address . That address lies inside the reserved block, so reading it does not touch memory that belongs to something else. But , so slot 10 holds no valid element; its bytes are leftover or were never written. A check against the capacity, , would wrongly accept this index.
Index 16 gives the address , the first byte after the block. Reading there is an out-of-bounds read §4.5.
Appending writes the new element into slot 3, at address , and then sets the length to 4. Suppose instead that the array is full, with length 16 and capacity 16. The next append reserves a block of 32 slots, bytes, which the memory allocator places at address 2000, say. The append copies the 16 elements, 64 bytes, to addresses 2000 to 2063, writes the new element at , sets the length to 17 and the capacity to 32, and releases the old block. Element 2 now lives at . A pointer saved earlier to element 2 still holds the address 1008, which now lies in released memory: it is a dangling pointer §4.1, and using it is a use-after-free §4.6.
Stated precisely
Let be the base address, the element size in bytes, the capacity, the logical length and an index. The array keeps the invariant . The reserved block is the half-open byte range , meaning the addresses from up to but not including . Element occupies the range . An access to element is valid when and the block is still reserved. An append fits without growing when .
On a machine, the check must come before any arithmetic that could overflow: compare the index with the length first, , and only then compute . Hold in an unsigned type, or also check , so that a negative index cannot pass. Computing first and comparing the address with the end of the block fails when exceeds the largest value of its type and wraps around to a small number. A check in the safe order is an overflow-safe range check §4.17. In C, a pointer may point one element past the end of an array, where it may be compared but not used to read or write; forming any pointer further out is undefined behavior §4.12.
Limits and common mistakes
An index below the capacity is not necessarily a valid index. Slots to lie inside reserved memory but hold no valid element, only leftover data, possibly from an earlier user of that memory, or bytes never written. In C such a slot holds an indeterminate value, and the language gives no guarantee about what a read of it returns.
Reducing the length erases nothing. An array that held a secret, such as a key, must be cleared explicitly, with a clearing function that the compiler cannot remove as a useless write.
A C array does not carry its length. Passed to a function, it becomes a pointer to its first element, so the function must receive the length as a separate argument; sizeof applied to such a parameter gives the size of a pointer, not of the array.
Off-by-one errors are the classic array bug. The valid indices are 0 to , so a loop that continues while reads one element too many.
Growing invalidates every pointer into the old block, as the worked example shows. Code that keeps a position across an append must keep the index instead, or reserve enough capacity in advance that no growth happens.
Constant-time indexing does not make every operation cheap. Inserting or deleting in the middle moves up to elements, and an append that grows the array copies all of them.
When several threads (flows of execution that share memory) use one array, a reader may see the new length before the new element is written. The writer must publish the length so that a reader who sees the new length also sees the element: a mutex §9.5 (a lock that one thread at a time can hold) around both does this, and so does an atomic store of the length with release ordering, read by an atomic load with acquire ordering. Without either, the program has a data race §9.3.
Questions answered
What is the difference between allocated capacity and current logical length?
Allocated capacity is the number of element slots for which memory has been reserved; current logical length is the number of those slots, counted from index 0, that currently hold valid elements. Always . The length decides which indices may be read or written: index is valid only when . The capacity decides whether an append fits without growing: it fits when the length is less than the capacity; otherwise the array must reserve a larger block and copy its elements into it.
An index between the length and the capacity lies inside reserved memory, so using it does not leave the block, but the slot holds no valid element, only leftover or never-written bytes. Checking reads against the capacity instead of the length is therefore a bug that can return stale data, including secrets left by earlier use. An array with capacity 16 and length 3, for example, has 16 reserved slots, but only indices 0, 1 and 2 hold elements.
Related entries: Overflow-safe range check (§4.17), Amortized analysis (§14.4), Peak versus retained memory (§11.37), Object lifetime (§4.1), Out-of-bounds read (§4.5), Pointer arithmetic (§4.25), Deamortization: spreading array migration while preserving lookup meaning (§14.40).