Stack memory

The stack differs in many ways compared to the heap. Here are some of the unique properties of the stack:

  • The stack is a contiguous memory block.
  • It has a fixed maximum size. If a program exceeds the maximum stack size, the program will crash.
  • The stack memory never becomes fragmented.
  • Allocating memory from the stack is always fast.
  • Each thread in a program has its own stack.

The code examples that follow in this section will examine some of these properties. Let's start with allocations and deallocations to get a feel for how the stack is used in a program.

We can easily find out in which direction the stack grows by inspecting the address of stack-allocated data. The following example code demonstrates how the stack grows ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.