A Dynamically Allocated Array is an array whose size is determined at runtime rather than compile time. Unlike standard (static) arrays which live on the Stack(s), dynamic arrays live on the Heap(s).

This is the foundational concept behind high-level structures like std::vector in C++, ArrayList in Java, or Vec in Rust. Related: Basic Array(s), Time Complexity


Core Concepts

In C/C++, we manage this manually using Pointers. The OS gives us a block of memory, and it is our responsibility to manage its life-cycle.

Key Operations:

  1. Allocation (malloc): Request a contiguous block of bytes.
    • Risk: If the Heap(s) is full, this returns NULL. Always check!
  2. Resizing (realloc): Change the size of the block.
    • Under the Hood: If the adjacent memory is free, the OS extends the block (). If not, it allocates a new block, copies all data over, and frees the old block ().
  3. Freeing (free): Return the memory to the OS.

Time Complexity

OperationComplexityNotes
AccessSame as static arrays (pointer arithmetic).
Append (Amortized)If capacity exists.
ResizeRequires copying the entire array to a new address.
Insertion/DeletionShifting elements is still required.

Pros & Cons

FeatureDynamic Array (Heap)Static Array (Stack)
SizeMutable (can grow/shrink)Fixed at compile time
LifetimeManual (free)Automatic (Scope-based)
PerformanceSlower (Indirection + Cache misses)Faster (Hot cache)
SafetyHigh Risk (Leaks, Dangling Pointers)Low Risk