Global memory management in C++ in stack or heap?

C++Memory ManagementStack

C++ Problem Overview


If I declare a data structure globally in a C++ application , does it consume stack memory or heap memory ?

For eg

struct AAA
{

.../.../.
../../..
}arr[59652323];

C++ Solutions


Solution 1 - C++

Since I wasn't satisfied with the answers, and hope that the sameer karjatkar wants to learn more than just a simple yes/no answer, here you go.

Typically a process has 5 different areas of memory allocated

  1. Code - text segment
  2. Initialized data – data segment
  3. Uninitialized data – bss segment
  4. Heap
  5. Stack

If you really want to learn what is saved where then read and bookmark these:

COMPILER, ASSEMBLER, LINKER AND LOADER: A BRIEF STORY (look at Table w.5)

Anatomy of a Program in Memory

alt text

Solution 2 - C++

The problem here is the question. Let's assume you've got a tiny C(++ as well, they handle this the same way) program like this:

/* my.c */

char * str = "Your dog has fleas.";  /* 1 */
char * buf0 ;                         /* 2 */

int main(){
    char * str2 = "Don't make fun of my dog." ;  /* 3 */
    static char * str3 = str;         /* 4 */
    char * buf1 ;                     /* 5 */
    buf0 = malloc(BUFSIZ);            /* 6 */
    buf1 = malloc(BUFSIZ);            /* 7 */

    return 0;
}
  1. This is neither allocated on the stack NOR on the heap. Instead, it's allocated as static data, and put into its own memory segment on most modern machines. The actual string is also being allocated as static data and put into a read-only segment in right-thinking machines.
  2. is simply a static allocated pointer; room for one address, in static data.
  3. has the pointer allocated on the stack and will be effectively deallocated when main returns. The string, since it's a constant, is allocated in static data space along with the other strings.
  4. is actually allocated exactly like at 2. The static keyword tells you that it's not to be allocated on the stack.
  5. ...but buf1 is on the stack, and
  6. ... the malloc'ed buffer space is on the heap.
  7. And by the way., kids don't try this at home. malloc has a return value of interest; you should always check the return value.

For example:

char * bfr;
if((bfr = malloc(SIZE)) == NULL){
   /* malloc failed OMG */
   exit(-1);
}

Solution 3 - C++

Usually it consumes neither. It tries to allocate them in a memory segment which is likely to remain constant-size for the program execution. It might be bss, stack, heap or data.

Solution 4 - C++

Neither. It is .data section.

Solution 5 - C++

Global memory is pre-allocated in a fixed memory block, or on the heap, depending on how it is allocated by your application:

byte x[10]; // pre-allocated by the compiler in some fixed memory block
byte *y

main()
{
   y = malloc(10); // allocated on the heap
}

EDIT:

The question is confusing: If I allocate a data structure globally in a C++ application , does it consume stack memory or heap memory ?

"allocate"? That could mean many things, including calling malloc(). It would have been different if the question was "if I declare and initialize a data structure globally".

Many years ago, when CPUs were still using 64K segments, some compilers were smart enough to dynamically allocate memory from the heap instead of reserving a block in the .data segment (because of limitations in the memory architecture).

I guess I'm just too old....

Solution 6 - C++

Neither declaring a data structure globally in a C++ consumes heap or stack memory. Actually, global variables are typically allocated in a data segment whose size remains unchanged during the whole program. Stacks and heaps are typically used for variables that get created and destroyed during executing the program.

Program Memory Space

Solution 7 - C++

The global object itself will take up memory that the runtime or compiler reserves for it before main is executed, this is not a variable runtime cost so neither stack nor heap.

If the ctor of the object allocates memory it will be in the heap, and any subsequent allocations by the object will be heap allocations.

It depends on the exact nature of the global object, if it's a pointer or the whole object itself that is global.

Solution 8 - C++

global variables live on the heap. these are a special case because they live for the life of the program

Solution 9 - C++

If you are explicitly allocating the memory yourself by new or malloc, then it will be allocated in heap. If the compiler is allocating the memory, then it will be allocated on stack.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsameer karjatkarView Question on Stackoverflow
Solution 1 - C++MilanView Answer on Stackoverflow
Solution 2 - C++Charlie MartinView Answer on Stackoverflow
Solution 3 - C++Tomek KopczukView Answer on Stackoverflow
Solution 4 - C++EFraimView Answer on Stackoverflow
Solution 5 - C++Philippe LeybaertView Answer on Stackoverflow
Solution 6 - C++DMLView Answer on Stackoverflow
Solution 7 - C++Rudi BierachView Answer on Stackoverflow
Solution 8 - C++user128026View Answer on Stackoverflow
Solution 9 - C++srnayakView Answer on Stackoverflow