Member-only story
Complete Guide to Memory Management in C++ (No More Leaks!)
Do you know where variables live in memory when your C++ program runs?
When a C++ program runs, memory is divided into four main regions: Code Segment, Global & Static Data Segment, Stack, and Heap.

1. Code segment
The code segment stores the compiled machine code of your program. These are the actual instructions that the CPU executes. I have a detailed article on the compilation process.
2. Global & Static Variables is the (Data Segment)
This part of memory holds global and static variables, which exist throughout the program’s execution.
3. Stack
The stack is where local variables and function calls are stored. When a function is called, its local variables are pushed onto the stack, and when the function returns, they are popped off automatically. We will understand this in just a bit.
4. Heap
The heap is where dynamically allocated memory lives. Unlike the stack, memory on the heap is not automatically managed — you must allocate and free it manually.