C++ standard terminology for heap/stack [closed]
There seems to be some uncertainty about the terminology that should be used. There seems to be 2 different points of view:
- some people prefer to use heap and stack to mean the location of bytes
- others are insisting that those "heap" and "stack" terms shouldn't be used in c++, but instead prefer "automatic storage duration" terminology.
But obviously programmers use one set of terminology and standard uses another. But what terms should be used? And why? There is still problem that they mean different thing? But there seems to be constant disagreement when using stack/heap terms in ##C++ channels in freenode. We've now concluded that duration!=location, b开发者_如何学JAVAut still we should decide what is the preferred terminology.
"heap" and "stack" are merely practical implementations of the general concepts specified by the C++ standard.
The standard doesn't care where you store your objects, it cares about the lifetime of those objects, and so it talks about "automatic storage duration".
But compilers typically use a stack as one of the tools for implementing this. However, as pointed out in comments, not every object with automatic duration is located on the stack. Class members have automatic storage duration, whether or not the owning object is on the stack.
For a lot of common usage, the two can be interchanged without really causing confusion. Objects on the stack have automatic storage duration, and they are typically what comes to mind when talking about "automatic storage duration" as well.
As long as you don't claim something is stack-allocated when it is on the heap, I'm not too fussed about which term you use.
Stack is an implementation of object storage with automatic storage duration (an abstract concept). Likewise for heap and dynamic storage duration.
精彩评论