Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I don't know when does the memory is actually allocated for a variable .
Few questions as below :

1. when does the memory allocated to a simple primitive data type say int .
2. when does the meomory allocated to a class object .

What I have tried:

I searched on net but there are discrepancy of opinions .
Posted
Updated 31-Jul-18 21:30pm

Memory allocation is actually a big C++ topic. You could start reading a tutorial, see, for instance C++ Tutorial: Memory Allocation - 2018[^], and then study it on a good C++ book. Because, as C++ developer, you have to master it.
 
Share this answer
 
Comments
iampradeepsharma 1-Aug-18 12:14pm    
thanks
There are different kinds of storage areas:
  1. Text: Contains the executable code (named here for completeness)
  2. Initialised data: Global and static variables
  3. Uninitialised data: Global and static variables (initialised to zero when the application starts)
  4. Stack: Local variables
  5. Heap: Allocated storage using new() or malloc()
Memory for the first two kinds is part of the executable itself and is allocated by the operating system when loading the application into memory.

The memory for unitialised global data is also allocated by the operating system and initialised to zero.

Stack memory is allocated by the operating system when a thread is started. For the main thread, the size is determined from the executable file. Other threads created during runtime will use the same size as default value but a different size can be passed as parameter to the thread creation function.

Each time a local variable is created, it will be assigned the address of the stack pointer and that is decremented by the size of the variable (stacks are using the memory in top down direction). Once the stack pointer reaches the bottom, a stack overflow occurs and the application terminates because the stack memory is not resizable during runtime.

A compiler may also decide to store local variables in CPU registers instead of the stack. This can be also proposed with the register keyword.

Heap memory is allocated in chunks during runtime. This is done by code inserted by the compiler to every application (called heap manager). When a request for memory can't be fulfilled from the current heap memory, the heap manager will request a larger block of memory from the operating system.

There is generally no difference between the memory allocation or assignemnt of basic and complex types (besides that the compiler might store basic types in registers rather than the stack).

Some examples:
C++
// Initialised global data
int global1 = 1;
static int global2 = 2;

// Uninitialised global data (will be set to zero)
int global1U;
static int* globalPtr;

void some_func()
{
    // Initialised static data
    static int localS1 = 1;

    // Uninitialised static data
    static int localS2;

    // Local variables on the stack
    // The compiler might store some of them in CPU registers
    int local1;
    int local2 = 2;
    int localArr[10];
    SomeObj obj;

    // Local variable stored in CPU register
    // Deprecated with C++17 and the compiler may ignore it (use the stack instead)
    register r = 0;

    // Heap memory where the pointer is stored as local variable
    int *localPtr = new int[10];
    SomeObj *objPtr = new SomeObj();
    
    // Heap memory where the pointer is stored in a global variable
    globalPtr = new int[10];


}
 
Share this answer
 
The memory of member is allocated while object creation. A big difference are pointers, they are only plain numeric values.
C++
Object obj;//memory of an Object instance
Object *p = 0;//only a pointer (long) allocated
p = new Object();//Object allocated and pointer set 

You can try it by stepping into the code with the debugger.
 
Share this answer
 
It is allocated when you request its creation. For a simple variable that means when it is declared. For an object of a class, when it is declared, or created with new, as Karsten shows above.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900