The Memory Model: Stack, Heap and Static
Storage duration determines how long storage is available for an object. C++ distinguishes automatic, static, thread and dynamic storage duration.
Relate automatic, static and dynamic storage to ownership and cleanup.
Rocket Run keeps a few numbers for a moment, one high score for the whole session, and a level full of asteroids whose count is only known after the level file loads. These three needs map onto three kinds of storage, and knowing which one an object uses tells you exactly when it disappears.
The rule
Braces end locals, owners end heap objects, program exit ends statics.
Every object occupies some bytes, has an address, and has a storage duration:
- Automatic (usually called the stack): ordinary local variables. Created when the line runs, destroyed at the closing
}of their scope. Fast and automatic. - Dynamic (usually called the heap): objects you create at run time, whose size or lifetime is not known in advance. They live until their owner releases them.
- Static: globals and
staticlocals. Set up once, and they last until the program ends.
void playLevel(int asteroidCount) {
int lives = 3; // automatic
static int highScore = 0; // static: kept between calls
std::vector<Asteroid> rocks(asteroidCount); // the vector's elements live on the heap
auto boss = std::make_unique<Boss>(); // heap object, owned by boss
} // lives, rocks and boss end here, and rocks and boss free their heap memoryIn modern C++ you rarely touch the heap by hand. std::vector, std::string and std::make_unique allocate for you and free the memory when they themselves are destroyed. The raw version, new with a matching delete, is easy to get wrong: forget the delete and the memory leaks.
You can ask any type how many bytes it takes with sizeof. sizeof(char) is always 1; most other sizes depend on the platform.