O'Reilly logo

C++ High Performance by Bjorn Andrist, Viktor Sehr

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required