liveBook · Manning

Capacity

As explained in Grokking Data Structures

Array capacity is the maximum number of elements allocated for an array.

Capacity is the number of elements an array has room to hold. It differs from current size: an array may have space for more elements than it currently stores. section 2.11 section 5.2

Capacity affects what happens when elements are added or removed:

  • A static array has a fixed capacity. When it is full, insertion must report failure; adding capacity later requires allocating another array and moving elements. section 2.11 section 5.2
  • A dynamic array can grow when full, commonly by doubling its capacity. It can also shrink after removals; one described strategy halves capacity when only a quarter is filled. section 5.14
  • Extra capacity can avoid frequent resizing, but allocating far more space than needed wastes memory. If you can estimate usage, an initial capacity can reserve space in advance. section 5.16 section 5.2

A changing workload

An order-tracking array might grow to hold 10,000 open orders during a holiday peak, then keep that capacity after orders close. Without shrinking, most allocated space could sit unused when only about 100 orders remain. section 5.11

How Grokking Data Structures covers it

How deep it goes
Defines capacity against current size, then explains full-array insertion and doubling or halving policies.
Examples
Uses a full static array, dynamic order tracking during a holiday peak, and stack growth to illustrate capacity.
What you can do after
Evaluate how allocated capacity limits static arrays and affects dynamic-array growth, shrinking, and memory use.
How it connects
Connects capacity to static arrays, dynamic arrays, stacks, insertion, and resizing.

Also explained in

Compare how 5 books explain capacity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.