Take the 2-minute tour ×

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

For eg

struct AAA

share|improve this question
also, what is the difference between a global variable and a static variable (within a function). They have to live for the life of the program... –  user128026 Jul 23 '09 at 6:19
agreed but theirs a difference between accessibility –  sameer karjatkar Jul 23 '09 at 6:29
@dspinozzi: the constructors for global variables are called before main(), but the constructors for static variables are called the first time the function is called. Both types of variables are typically stored in the same parts of memory -- I think GCC puts them in the .data section. –  Neil Jul 24 '09 at 5:26

9 Answers 9

up vote 84 down vote accepted

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:


Anatomy of a Program in Memory

alt text

share|improve this answer
Does that mean the Uninitialized data - bss and the Initialized - data are a part of the heap ? –  sameer karjatkar Jul 23 '09 at 13:36
No, they are not a part of the heap, they are in different areas as was written in my answer (the 5 different areas). The heap and stack occupy the virtual memory above the text and data segments. –  Milan Jul 23 '09 at 13:53
The important point is that the bss and data segments are allocated when the program is first loaded into memory, and their size does not change while it runs. The contents of the heap by contrast are volatile and change throughout the run, as dynamic memory operations are performed. –  quark Jul 24 '09 at 6:50
+1 for visual representation –  Nilesh Mar 18 '14 at 23:23
I thought the idea of letting the stack grow downwards and letting the heap grow upwards was so that they can use the available memory in any ratio. However, isn't that prevented by loading the dynamic libraries in between? –  danijar Jan 2 at 11:23

The probelm 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 it's 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 alocated 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 */
share|improve this answer
The malloced buffer space has nothing to do with global variables. Only the pointers are global. Please to not confuse the people further. –  EFraim Jul 23 '09 at 6:18
Oh, don't be silly. The questioner clearly wasn't clear on what went where, so I wrote an answer that was directed to improving his understanding. –  Charlie Martin Jul 24 '09 at 7:19

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.

share|improve this answer
By editing the boot.ini file we can extend the virtual memory to 3GB . Like wise is there any setting for the memory segment ? –  sameer karjatkar Jul 23 '09 at 6:10
That would be pointless, because the size of the statically allocated memory can never change –  Philippe Leybaert Jul 23 '09 at 6:14

Neither. It is .data section.

share|improve this answer
It depends if the global memory was allocated inline or alllocated dynamically from the application –  Philippe Leybaert Jul 23 '09 at 6:03
If a memory was allocated dynamically it is not global (in a sense of global variable) –  EFraim Jul 23 '09 at 6:06
that's debatable :) –  Philippe Leybaert Jul 23 '09 at 6:08
@Philippe - the point is that the data pointed to by global pointer cannot be considered global. It can even change during program execution (different functions might reset the global pointer to whatever place they want) –  EFraim Jul 23 '09 at 6:42
@Philippe: .data sections are also not .EXE only. –  EFraim Jul 23 '11 at 19:22

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

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


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....

share|improve this answer
That's not right. The pointer itself is a global variable, not the memory block you are pointing with it. –  EFraim Jul 23 '09 at 6:12
That's semantics. I guess that's worth a downvote –  Philippe Leybaert Jul 23 '09 at 6:13
It sais "allocated on the heap" and that's pretty correct. Unless this question is flagged "novice" or "beginner" this should be a sufficient reminder to what is happening. –  Don Johe Jul 23 '09 at 6:20
@Don: No. The global thing is the pointer, and not the memory it is pointing to. You can handle the memory the way you want. Neither it is there to stay for all the run. You can even point it at stack sometimes. –  EFraim Jul 23 '09 at 6:35
If there is one lesson to be learned from this, it's that you should avoid answering questions where the exact meaning of the question is unclear. My answer is not wrong, it's just that some people think that their interpretation of a word is enough to vote down everything that doesn't support their view. Even now, 10 hours after the question was asked, it's still not clear what the OP meant. –  Philippe Leybaert Jul 23 '09 at 17:03

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
global memory is never allocated on the stack. The stack is only used for local variables and parameters –  Philippe Leybaert Jul 23 '09 at 6:04
stack variables are "destroyed" when the function returns –  user128026 Jul 23 '09 at 6:14

Your Answer


