A Quick guide to C++ Memory Allocation – Part 3

This article explains what take place in your computer memory when you executes a C++ program. Unlike Java and C# .Net, C++ programs needs to manage the heap memory manually. This article explains different types of memory allocations including stack and heap.

What happens in memory when you execute a C++ program ?

A typical C++ program will have four separate sections of the memory it will be utilising from the memory allocated by the C++ run-time.

C++ Memory Allocation
(1) Memory allocated for Program Code:

This is the memory area that is used by the run-time to execute our code. As programmers, we do not have access to this area of the memory rather, it is managed totally by the C++ run-time environment.

(2) Area for Global Variable Allocations:

This is the memory area is occupied by the variables declare in class level (global scale). Based on the C++ run-time you are using this space is limited so you needs to be mindful not to over allocate.

(3) Memory allocated for function Stack:

This are is used by the run-time to ‘stack’ function calls that are been called during the program execution.

This space is considered as a stack space which means functions will be added in FILO (First In Last Out) order. (Read more about Stacks & Queues)

Every method that is called during the life-cycle of your application starting with the main() method will be added to this space.

Stack Overflow Error – This is an error that causes applications to fail when there is not space left in the ‘stack’ space on the allocated memory for the application.
(4) Dynamically allocated memory (heap memory):

This memory area is used by the variables we declare explicitly. Mainly the reference type variables like Classes, Arrays and custom data structures resides in this area.

When you want to define a variable that resides in the heap, you need to do 3 things:

  1. Use the ‘new’ keyword to reserve a memory location. (new)
  2. Manage errors by properly handling them in case there is no space left in the heap. (nothrow)
  3. Clean up after you use the memory space so rest of the program can use it. (delete)

Below example demonstrates the : allocation of memory, using error protection to prevent memory unavailable issues and finally removing unused space.

#include <iostream>

using namespace std;

int main()
    int *z = 0;

    // step 1 and 2 : using nothrow to catch errors when allocating space.
    z = new (nothrow) int;

    *z = 5+5;

    cout << *z << " , stored in address = " << &z << endl;

    // step 3 : remove unused heap space.
    delete z;

    cout << *z << " , stored in address = " << &z << endl;

    return 0;

Interview tips.
What is Stack & Heap memory?
Stack Memory usually resided in CPU cache & fast but has limited space. When a program executes all the functions and parameters passed to the functions are staked up in this space.

Heap Memory is used to store the objects allocated with the new keyword. In C++ we need to manually clear the heap memory. Use of pointers and reference are used to mange heap memory.
Download the Code.
Cite this article as: Guest Contributor, "A Quick guide to C++ Memory Allocation – Part 3," in Cyber Memos, March 22, 2020, https://cybermemos.com/developent/c/a-quick-guide-to-c-memory-allocation-part-3/.

Leave a Reply

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