开发者

C++ standard terminology for heap/stack [closed]

开发者 https://www.devze.com 2023-03-16 12:01 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

There seems to be some uncertainty about the terminology that should be used. There seems to be 2 different points of view:

  1. some people prefer to use heap and stack to mean the location of bytes
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号