views:

2229

answers:

8

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];

+4  A: 

Neither. It is .data section.

EFraim
It depends if the global memory was allocated inline or alllocated dynamically from the application
Philippe Leybaert
If a memory was allocated dynamically it is not global (in a sense of global variable)
EFraim
that's debatable :)
Philippe Leybaert
Then in what sense it is global, if it is not in scope of all the program?!
EFraim
I'd have to agree with EFraim. The actual global variable is going to be located in the .data/.bss sections. Sure in the case of a pointer, it may eventually point to dynamically allocated memory, but I wouldn't say the pointer itself is dynamically allocated.
Falaina
We're talking about allocated memory which is globally accessible through a global variable. In fact, the compiler can even choose to allocate a pre-allocated block of memory on the heap instead of in a fixed memory block (if the block is too large)
Philippe Leybaert
If the global variable is going to be located in the data/.bss sections will it show up in the heap memory consumption . I mean will it record its memory consumption under virtual memory ?
sameer karjatkar
Another point worth noting is that ".data" sections are EXE only (so Microsoft-stuff). There are of course similar structures on other OS's , but the question was not specifically about PC development
Philippe Leybaert
@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
+1  A: 

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

Philippe Leybaert
That's not right. The pointer itself is a global variable, not the memory block you are pointing with it.
EFraim
That's semantics. I guess that's worth a downvote
Philippe Leybaert
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
@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
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
Yes that's my mistake in framing the question . I have edited it now
sameer karjatkar
A: 

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.

srnayak
global memory is never allocated on the stack. The stack is only used for local variables and parameters
Philippe Leybaert
stack variables are "destroyed" when the function returns
+8  A: 

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.

tkopczuk
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
That would be pointless, because the size of the statically allocated memory can never change
Philippe Leybaert
A: 

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

A: 

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.

Rudi Bierach
+2  A: 

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 */
   exit(-1);
}
Charlie Martin
The malloced buffer space has nothing to do with global variables. Only the pointers are global. Please to not confuse the people further.
EFraim
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
+11  A: 
Milan
Does that mean the Uninitialized data - bss and the Initialized - data are a part of the heap ?
sameer karjatkar
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
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