/daɪˈnæmɪk əˈreɪ/
noun … “Resizable contiguous memory collection.”
Dynamic Array is a data structure similar to an array but with the ability to grow or shrink at runtime. Unlike fixed-size arrays, dynamic arrays allocate memory on the heap and can expand when more elements are added, typically by allocating a larger block and copying existing elements. They balance the efficiency of indexed access with flexible memory usage.
Key characteristics of Dynamic Array include:
- Resizable: automatically increases capacity when the current block is full.
- Indexed access: supports constant-time access to elements by index.
- Amortized allocation: resizing occurs infrequently, so average insertion cost remains low.
- Memory trade-offs: larger capacity may be preallocated to reduce frequent reallocations.
- Integration with pointers: in languages like C++, dynamic arrays are managed via pointers and memory management functions.
Workflow example: Adding elements to a dynamic array in pseudocode:
function append(dynamic_array, value) {
if dynamic_array.size >= dynamic_array.capacity:
new_block = allocate(2 * dynamic_array.capacity)
copy(dynamic_array.block, new_block)
free(dynamic_array.block)
dynamic_array.block = new_block
dynamic_array.capacity *= 2
dynamic_array.block[dynamic_array.size] = value
dynamic_array.size += 1
}Here, when the array reaches capacity, a larger memory block is allocated, existing elements are copied, and the old block is freed, allowing continued insertion without overflow.
Conceptually, Dynamic Array is like a backpack that can magically expand to hold more items as you acquire them, maintaining order and direct access to each item.
See Array, Heap, Pointer, Memory Management, Vector.