My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Why should we have to allocate memory in heap and why not in stack ? | Heap vs Stack | Every C/C++ application developer should know.

PRASHANTH H N's photo
PRASHANTH H N
·Mar 2, 2019

Let us consider the program below,

#include<iostream>

using namespace std;

class kB{
    char a[1024];
public:
    void set(){
        cout << "kB :: set" << endl;
        for (int i = 0; i < 1024; i++){
            a[i] = '1';
        }
    }
};

class MB{
    kB a[1024];
public:
    void set(){
        cout << "MB :: set" << endl;
        for (int i = 0; i < 1024; i++){
            a[i].set();
        }
    }
};

class GB{
    MB a[1024];
public:
    void set(){
        cout << "GB :: set" << endl;
        for (int i = 0; i < 1024; i++){
            a[i].set();
        }
    }
};

void allocate_in_stack(){
    cout << "trying to allocate in stack" << endl;
    GB objGB;
    cout << sizeof(GB) << endl;
    objGB.set();
    cout << "allocated in stack  and this is a miracle" << endl;
}

GB* allocate_in_heap(){
    cout << "trying to allocate in heap" << endl;
    GB* pGB = new GB();
    cout << "allocated in heap" << endl;
    return pGB;
}

int main(int NARG, char** ARGS){
    if (NARG != 2){
        cout << "Usage : " << endl;
        cout << "\"stack_size.exe 1\" for allocation in stack" << endl;
        cout << "\"stack_size.exe 2\" for allocation in heap" << endl;
        return 0;
    }

    string strArg(ARGS[1]);
    GB* pGB = nullptr;

    if (strArg == "1"){
        allocate_in_stack();
    }
    else{
        pGB = allocate_in_heap();
    }

    system("pause");
    if (pGB != nullptr){
        delete pGB;
    }
    else{
        if (strArg != "1"){
            cout << "did not find continous 1GB space to allocate" << endl;
        }
    }
    return 0;
}

There are three classes — kB , MB and GB their description is as follows,

  • kB It has a member variable array of type char ( 1 byte ) and size 1024 which comes to 1 kB. i.e. every object of type kB will require 1 kB of memory.
  • MB It has a member variable array of type kB and size 1024 which comes to 1 MB.i.e. every object of type MB will require 1 MB of memory.
  • GB It has a member variable array of type MB and size 1024 which comes to 1 GB.i.e. every object of type GB will require 1 GB of memory.

One should have the knowledge of below topics to understand this article further,

Coming back to the topic, when the application is run the main() function gets spawned as a thread and the following things will happen :

  • It will parse the command line argument.

  • If the value is “1” then it will call allocate_in_stack() otherwise allocate_in_heap().

  • allocate_in_stack() will try to create an object (objGB) of class GB for which the memory has to be allocated in the stack space available for the main thread. Of course, the required memory is 1 GB which is not available in the stack space of the thread and the stack overflow occurs.

  • allocate_in_heap() will try to get a pointer to the object of class GB by using new operator. In this case the object will be residing in heap and a pointer to the object will be assigned to the pointer variable (pGB) which will be consuming the size of an integer (4 or 8 bytes depending on the target platform for which the application is built) in the stack space of the main thread.

We can actually confirm through task manager or resource monitor that allocate_in_heap() does the allocation by checking the amount of RAM that the application is consuming when system(“pause”); gets executed.

This program is compiled and the the visual studio solution of the same is available here - github.com/PraGitHub/Prapository/tree/maste..