2. Content
1. C++ Memory Segments
2. Program in RAM
3. Dynamic Memory
4. Call Stack
5. Call Stack Segment
6. Stack in Action
3. C++ Memory Segments
A program is typically divide into different memory
areas called segments.
Code Segment
It also called text segment where the compiled program sits in
memory. The code segment is typically read-only.
BSS Segment
It is also called the uninitialized data segment, where zero-
initialized global and static variables are stored.
Data Segment
It is also called initialized data segment where initialized global
and static variables are stored
Heap
It holds dynamically allocated variables.
Stack
It stores function parameters, local variables, and other function-
related information.
4. Program in RAM
Sections:
1. Read only
a) .text: Program code.
b) .rodata: constants (const modifier)
and strings.
2. Global and Static
a) .data: Initialized global and static
variables (startup value ≠ 0)
b) .bss: Uninitialized global and static
variables (zero value on startup)
5. Dynamic Memory
Heap Segment (also known as the “free store”) keeps
track of memory used for dynamic memory allocation.
In C++, when you use the new operator to allocate memory,
this memory is allocated in the application’s heap segment.
int *ptr = new int; // ptr is assigned 4 bytes in the heap
int *array = new int[10]; // array is assigned 40 bytes in the heap
Heap has advantages and disadvantages:
Allocating memory on the heap is comparatively slow.
Allocated memory stays allocated until it is specifically
deallocated (beware memory leaks) or the application ends
(at which point the OS should clean it up).
Dynamically allocated memory must be accessed through a
pointer. Dereferencing a pointer is slower than accessing a
variable directly.
Because the heap is a big pool of memory, large arrays,
structures, or classes can be allocated here.
6. Call Stack
Call stack usually referred to as “stack” .
The call stack keeps track of all the active functions from the start of the program to the current point of
execution, and handles allocation of all function parameters and local variables.
The call stack is implemented as a stack data structure.
A stack is a last-in, first-out (LIFO) structure. The last item pushed onto the stack will be the first item
popped off.
Stack has advantages and disadvantages:
1. Allocating memory on the stack is comparatively fast.
2. Memory allocated on the stack stays in scope as long as
it is on the stack. It is destroyed when it is popped off
the stack.
3. All memory allocated on the stack is known at compile
time. Consequently, this memory can be accessed
directly through a variable.
4. Because the stack is relatively small, it is generally not
a good idea to do anything that eats up lots of stack
space. This includes passing by value or creating local
variables of large arrays or other memory-intensive
structures, it leads to stack Overflow
7. Call Stack Segment
The Call Stack Segment holds the memory used for the call stack.
When a application function call is encountered, the function is pushed onto the call stack. When the current
function ends, that function is popped off the call stack. There is no need erase the memory just leave it to
be overwritten by the next item pushed to that piece of memory.
The stack itself is a fixed-size chunk of memory addresses,
and the “items” we’re pushing and popping on the stack are
called stack frames. A stack frame keeps track of all of the
data associated with one function call.
The “marker” is a register (a small piece of memory in the
CPU) known as the stack pointer (sometimes abbreviated
“SP”). The stack pointer keeps track of where the top of the
call stack currently is.
8. Call Stack in Action
How the call stack works?
Below is the sequence of steps that takes place when a function is called:
1. The program encounters a function call.
2. A stack frame is constructed and pushed on the stack. The stack frame consists of:
a) The address of the instruction beyond the function call (called the return address). This is how
the CPU remembers where to return to after the called function exits.
b) All function arguments.
c) Memory for any local variables.
d) Saved copies of any registers modified by the function that need to be restored when the function
returns
3. The CPU jumps to the function’s start point.
4. The instructions inside of the function begin executing.
When the function terminates, the following steps happen:
1. Registers are restored from the call stack
2. The stack frame is popped off the stack. This frees the memory for all local variables and arguments.
3. The return value is handled.
4. The CPU resumes execution at the return address