Memory management in C++

By | May 26, 2014

We are getting a some questions on our forum asking how a static variable is stored in memory? How a global variable is stored in memory ? When is the memory gets allocated from heap and when is the memory gets allocated form stack.

Instead of answering these questions individually we wrote this article to give our readers a clear picture on how memory is managed in a C++ program .We hope this article will answer all your queries , but still if you have any questions feel free to contact us . We will be happy to help you.

Memory Management

When a program is loaded into memory, it’s organized into various areas of memory, called segments. In general, an executable program generated by a compiler (like gcc) will have the following organization in memory on a typical architecture (such as on MIPS):
Memory-Layout-crackdinterviewMemory-Layout

The C/C++ program is organised into following segments in memory

    1. Code Segment or Text Segment : The text segment (sometimes also called the code
      segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user and system
    2. Data segment: Data segment is sub divided into two parts
      • Initialized Data Segment: All initialized (explicitly initialized with a non-zero or non-NULL value) global, static and constants data are stored in the data segment.
      • Uninitialized data segment: All the uninitialized data are stored in BSS(Block Started by Symbol) also called Uninitialized data segment.For example a variable declared like this         static int i; will be stored in this segment
    3. Heap: When program allocate memory at runtime using calloc and malloc function, then memory gets allocated in heap. When some more memory need to be allocated using calloc and malloc function, heap grows upward as shown in above diagram.
    4. Stack: Stack is used to store your local variables and is used for passing arguments to the functions along with the return address of the instruction which is to be executed after the function call is over.When a new stack frame needs to be added (as a result of a newly called function), the stack grows downward.

The stack and heap are traditionally located at opposite ends of the process’s virtual address space.
The stack grows automatically when accessed, up to a size set by the kernel (which can be adjusted with
setrlimit(RLIMIT_STACK, …)). The heap grows when the memory allocator invokes the brk() or sbrk()
system call, mapping more pages of physical memory into the process’s virtual address space. Implementation of both the stack and heap is usually down to the runtime/OS. Often games and other applications that are performance critical create their own memory solutions that grab a large chunk of memory from the heap and then dish it out internally to avoid relying on the OS for memory.

Take a look at our Tutorials Page for more such informative articles like this.

Leave a Reply

Your email address will not be published. Required fields are marked *