Skip to main content

Data Lifetime and Memory Segments

The computer has finite resources for "remembering" things. So, you can't just keep asking it to remember data without at some point also telling it that it's ok to forget the data. Otherwise, at some point it will be completely full of stuff you told it to remember, and it just won't be able to remember anything new, even if most of that data isn't even accessible through variables anymore (this is called a memory leak). The duration of time between remembering the data (allocating space for it in memory) and telling your computer that it can forget the data (freeing/deallocating the space) is called the lifetime of the memory.

Lifetime of Memory 

Three major categories for the lifetime of memory:memory in order to understand how variables are stored:

  • Global: This memory is around for the entire lifetime of your program. Generally this is a fixed amount of space that is allocated right when the program starts, and it cannot grow.

  • Local: The memory is allocated for you when entering a particular portion of the program, and automatically freed for you when exiting that part. In C++, this is denoted with curly braces {}, so it could be the scope of an...

      • entire function

      • if statement

      • loop

      • Anything inside curly braces {}

    Inside a block scope have limited lifetime and visibility. Outside that block, those variables no longer exist.

  • Dynamic: This memory has a lifetime which is unknown at the time of allocation. The program explicitly asks for a specific size of memory, and then that memory lives until the program explicitly says the memory is no longer needed.

    • In C++, this is done via malloc or new and then the corresponding free or delete calls. 

      Many languages (like Python, but not C++) uses a "Garbage Collector" takes control of the lifetime of dynamically allocated memory. You only ever call new and then there is a background algorithm always running which tracks which memory is still being used and frees it for you. This avoids most causes of memory leaks, but it cost performance. C++ puts the burden on the programmer to do it manually (and correctly) to be more performant. 

Memory Segments

Stack: you will pretty much never exhaust it unless 

  • you define absurdly large objects on the stack (e.g. an array of milions of objects)
  • you recurse too deeply (usually as a result of a bug of infinite recursion or unnecessarily large stack frame size)

Heap: this depends on what the program does.

  • This is limited by your system's RAM memory, which can be exhausted by programs of huge demands (usually games and scientific tools)

Memory Segments relation with Hardware

Whether they are stored on the stack, heap, or in static/global memory—are typically stored in RAM (Random Access Memory). However, they occupy different regions of RAM based on their storage duration and allocation mechanism.

|------------------------------|
|        Text Segment          |  <-- Program instructions (code)
|------------------------------|
|        Initialized Data      |  <-- Global and static variables with initialization
|------------------------------|
|     Uninitialized Data (.bss)|  <-- Global/static variables without explicit initialization
|------------------------------|
|        Heap                  |  <-- Dynamically allocated memory
|  (grows upwards)             |
|------------------------------|
|        Stack                 |  <-- Local variables, function calls
|  (grows downwards)           |
|------------------------------|