Are global variables in C++ stored on the stack, heap or neither of them?

29,596

Here is what the book says on page 205:

If you’re familiar with operating system architecture, you might be interested to know that local variables and function arguments are stored on the stack, while global and static variables are stored on the heap.

This is definitely an error in the book. First, one should discuss storage in terms of storage duration, the way C++ standard does: "stack" refers to automatic storage duration, while "heap" refers to dynamic storage duration. Both "stack" and "heap" are allocation strategies, commonly used to implement objects with their respective storage durations.

Global variables have static storage duration. They are stored in an area that is separate from both "heap" and "stack". Global constant objects are usually stored in "code" segment, while non-constant global objects are stored in the "data" segment.

Share:
29,596
Edoardo Meneghini
Author by

Edoardo Meneghini

Updated on November 03, 2021

Comments

  • Edoardo Meneghini
    Edoardo Meneghini over 2 years

    Initially I was pretty sure that the correct answer had to be "None of them", since global variables are stored in the data memory, but then I've found this book from Robert Lafore, called "Object Oriented Programming in C++" and it clearly states that, according to the C++ standard, global variables are stored on the heap. Now I'm pretty confused and can't really figure out what's the correct answer to the question that has been asked.

    Why would global variables be stored on the heap? What am I missing?

    EDIT: Link to the book - page 231

  • aschepler
    aschepler almost 7 years
    "Stack" and "heap" are definitely related to storage duration, but that's not what they are. Stack is an allocation strategy suitable for objects with automatic storage duration, and heap is an allocation suitable for objects with dynamic storage duration. (Of course, neither is really suitable for objects with static storage duration.)
  • AdrianRK
    AdrianRK almost 7 years
    I know this is valid for the elf format, but is this valid for others as well?
  • Sergey Kalinichenko
    Sergey Kalinichenko almost 7 years
    @AdrianRK You mean the "data" and the "code" segments? Most formats have segments that serve similar purpose, although names are often different (e.g. "code" segment is often called "text" segment).
  • Solomon Slow
    Solomon Slow almost 7 years
    Many C toolchains have two segments for global variables: One (traditionally called "data" in Unix land) is for global variables with compile-time-constant initializers, and the other (traditionally called "BSS") for globals that get initialized at run-time. The executable file contains the actual initial values for the variables in the "data" segment, but for the BSS it only has to say how big to make it.
  • Darsen Lu
    Darsen Lu about 4 years
    Thanks for answering the question in terms of storage duration. My follow-up question is whether global non-constant objects count towards the stack size limit of a process. Since they are stored in the data segment, it sounds like they are part of the stack. The typical stack size is on the order of 2MB. But I was able to define and write into a global array with 48MB size ("static uint8_t buffer[48 << 20]"), which definitely exceeds the 2MB limit, without encountering stack overflow error.
  • Sergey Kalinichenko
    Sergey Kalinichenko about 4 years
    @DarsenLu Even non-constant global and static objects (which are the same as globals, but with reduced name visibility) are stored outside the stack. That is why your globals did not overflow stack.