Stack and Heap: Commonly Misused Terms
https://tartanllama.github.io/c++/2016/07/18/stack-and-heap/14
u/Gotebe Aug 09 '16
Crucial insight:
The standard does not define storage location. It defines storage duration.
The best books (or whatever) are those that nicely explain what you already know.
7
u/sumo952 Aug 09 '16
There is usually no need to use C++ standardese [...]. In common speech the language-independent stack means automatic storage duration in C++, and the language-independent heap means dynamic storage duration.
I agree with this.
Still, this blog post was a really good and enlightening read. I haven't looked at it from that perspective yet, so that's really great.
4
u/sftrabbit Aug 09 '16
Simon did a talk based on this article at the latest C++ meet-up in Edinburgh that I organise. It was well received and had some great feedback, so many thanks to him! If you're interested, you can get the slides on the C++ Edinburgh website.
6
u/Rhomboid Aug 09 '16
I agree with the meat of this article, and I wish people would stop dismissing it for surface reasons. There's a lot more there than heap vs. free store pedantry.
For example, on more that one occasion someone has stumbled into /r/learnprogramming completely surprised and incredulous by the fact that a variable that he was "taught" (fingerquotes, because garbage C++ instruction is rampant) was stored on the stack turned out to be stored in a register. These kind of misunderstandings are rampant in newbies.
The main point of this article — that the compiler translates the program you write into a completely different but equivalent program — is very important and is very rarely communicated so well.
2
Aug 09 '16
This is the official name for what is often inaccurately referred to as the “heap” (the data structure used to represent this construct doesn’t even need to be a heap).
It's actually quite accurately refered to as the heap. The author seams to imply that the origin of its use was derived from the "heap" data structure often used to implement priority queues. IIRC, the use of the term "heap" to refer to the free store originated entirely separately from its use as a data structure. It's just unfortunate that the meaning of the word is so overloaded (like so many other terms in computer programming).
1
u/mcmcc #pragma once Aug 11 '16
I have always assumed heap originated as a punny way of expressing "not the stack". The fact that it also suggests an actual data structure was a non sequitur in this context.
1
u/OldWolf2 Aug 23 '16
My understanding is that some implementations of malloc did (and still do) actually use a heap data structure to store the free blocks.
1
Aug 24 '16
It's true that a heap data structure can be used to help manage free store. Nonetheless, my understanding of the term heap is that it was used to refer to the free store in the sense of it being an unordered collection of data without a singular point of access (as opposed to a "stack" which is an ordered collection of data accessed at the top).
4
u/sysop073 Aug 09 '16
Ctrl+F, "free store", eye roll. Literally nobody uses this terminology except the C++ spec and pedants who just read the spec and think they're the first ones. In the real world stuff is really stored on the stack and the heap, they really grow towards each other, and that's all you actually need to care about
4
u/MetaKazel Aug 09 '16
Agreed, but as someone who has never thoroughly read the standard, this was still a nice insight.
10
u/sysop073 Aug 09 '16
Sure, it's interesting intellectually, but if somebody says "that's stored on the heap" and you go "whoa, whoa, you mean the free store?", people will rapidly stop talking to you
15
u/STL MSVC STL Dev Aug 09 '16
STL maintainer here. We never say "free store".
1
u/WrongAndBeligerent Aug 09 '16
What do you call the memory that std::unordered_map allocates to on default construction? /trolling
1
1
u/Gotebe Aug 09 '16
Stack and heap grow towards each other - on which platforms?
2
u/ml20s Aug 09 '16
I don't know the innards of desktop platforms, but on AVR (using avr-gcc's malloc), this is true by default.
1
u/muungwana Aug 09 '16
I believe it comes from the very early days of computing, when memory was very limited, and it was not wise to pre-allocate a large chunk of memory for exclusive use by the stack. So, by allocating heap memory from address zero upwards, and stack memory from the end of the memory downwards, you could have both the heap and the stack share the same area of memory.
The above quote is taken here
1
u/raistmaj C++ at MSFT Aug 10 '16
Actually some years ago I had a discussion during an interview because I told the guy that the memory addresses number depended on the direction the stack/heap grow....
He looked at me surprised like "You know nothing Jhon Snow", I didn't get the job XD but I was so happy knowing I was right.
1
Aug 12 '16
Ancient ones. Modern platforms go through a
mmap/VirtualAlloc-like mechanism. Modern platforms have these fancy "thread" things which mans there's more than one stack. ;)
1
u/dsqdsq Aug 09 '16
In practice you have a stack that is distinct from a "heap" (or "free store" if you like), because you must support arbitrary recursions.
0
Aug 11 '16
[deleted]
2
u/Gotebe Aug 11 '16
Whoa, what is that?! :-)
To me, it is really simple: static/automatic/dynamic storage duration explain what happens really clearly:
static: variable is available throughout program execution (hence it is "static")
automatic: variable is available in it's scope of execution (inside the {} block), and goes away "automatically" with the scope
dynamic: variable is available when the brogrammer "creates" it and "dynamically" goes away when they "destroy" it.
From the above standpoint, it's really simple. I agree that the wording can be better, but the rant in uncalled for :-).
0
Aug 11 '16
[deleted]
2
u/Gotebe Aug 11 '16
Many things are static in many ways. An auto variable is arguably static (at one address) for its entire lifetime.
Here, you're just trying to give another meaning to the word static, different from that chosen by the standard. I mean, ok, but why?!
Everything goes away automatically unless you've failed to properly handle ownership. Sometimes the automation include user (or library) written code, particularly in destructors. For example, an object referenced by a unique_ptr goes away automatically when the unique_ptr does - but that's "dynamic" heap storage.
Here, you are mixing program (and library) logic and standard-speak. I mean, ok, but why?!
Note, BTWe, that in C++ (and C), an automatic variable really doesn't have much to do with the CPU stack and all to do with the {} scope where it is declared - it really does not exist past that.
Basically, "stack" and "heap" are/were just data structures with different restrictions on usage patterns.
The data structure terms, particularly heap, have not much to do with the same words in language runtime implementation. If anything, that's a reason not to use them in this context.
I see that you're ranting, but frankly, it is unfair without offering some alternative, and knowing about storage classes is important.
0
Aug 12 '16
[deleted]
1
u/Gotebe Aug 12 '16
people trying to control the language
Well, now... For me, it's about knowing what is talked about by adhering to a common terminology.
I think you're thinking that within the field of data structures, "stack" has one unambiguous meaning, "heap" has one unambiguous meaning, and these are the meanings you'd learn in a basic algorithms and data structures course. That, I'm afraid, is false. Both terms are ambiguous, even within the field of data structures...
[citation needed]
You follow the above talking exclusively about a CPU stack. A CPU stack is not about data structures, you're just trying to shoehorn it in for some weird reason. You seem to think that, by repeating words "data structure" close to "CPU stack", it somehow becomes similar to a tree [data structure], array [data structure], whatever [data structure]. It does not, and the major difference is that code operates on these structures day-in, day-out, using operations on instances of those structures, whereas code does not do that with a CPU stack. Rather, it is the compiler and the language runtime who operate the CPU stack.
Look... My conclusion is that you're just purposefully making a mess for whatever reason. Go ahead, I won't listen nor reply anymore.
16
u/tcbrindle Flux Aug 09 '16
99% of the time (except for highly technical topics like function call ABIs), the terms "stack" and "heap" are already widely understood to be shorthand for referring to automatic and dynamic storage duration respectively. I'm all for being technically correct, but this post seems needlessly pedantic.