Edoardo Meneghini
Edoardo Meneghini

Reputation: 598

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

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 - book page 205 / Google Drive page 231

Upvotes: 46

Views: 46438

Answers (2)

Jan Schultke
Jan Schultke

Reputation: 39658

The relevant quote from the book is 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 simply wrong because global variables are typically stored in a static memory section such as .data, or .bss, and these sections are allocated during program startup, not dynamically through e.g. std::malloc.

Furthermore, the C++ standard doesn't say anything about the stack or the heap; the C++ standard only concerns itself with storage duration, i.e. when storage is obtained and released for an object. Allocation strategy is the way that the implementation allocates storage when it is needed. Here is how the two concepts correspond:

Storage Duration Usual Allocation Strategy Common Optimizations
static storage duration
e.g. global int,
local static int, etc.
static memory section,
e.g. .data, .rodata, .bss
no memory, only register
for global constants,
especially constexpr
thread storage duration
, anythingthread_local
TLS (thread-local storage)
automatic storage duration,
i.e. local int,
function parameters, etc.
stack memory very often optimized to
use only registers,
especially for small types
like int and float
dynamic storage duration,
i.e. storage was obtained
via std::malloc, new, etc.
heap memory heap elision if possible,
i.e. use registers instead
(powerful compiler needed,
optimization can often not
be applied)

Of course, one common optimization I haven't yet mentioned is that if the compiler can tell that any object is unused, it can also completely remove it. In that case, a global variable is not stored anywhere, it just disappears.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

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.

Upvotes: 66

Related Questions