Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management?

混江龙づ霸主 提交于 2019-11-26 02:01:42

问题


Related to a lot of questions and answers on SO, I\'ve learned that it\'s better to refer to objects whose lifetime is managed as residing in automatic storage rather than the stack.

Also, dynamically allocated objects shouldn\'t be referred to as residing on the heap, but in dynamic storage.

I get that there is automatic, dynamic and static storage, but never really understood the difference between automatic-stack and dynamic-heap. Why are the former preferred?

I\'m not asking what stack/heap mean or how memory management works. I\'m asking why the terms automatic/dynamic storage are preferred over the terms stack/heap.


回答1:


Automatic tells me something about the lifetime of an object: specifically that it is bound automatically to the enclosing scope, and will be destroyed automatically when that scope exits.

Dynamic tells me that the lifetime of an object is not controlled automatically by the compiler, but is under my direct control.

Stack is an overloaded name for a type of container, and for the related popular instruction pointer protocol supported by common call and ret instructions. It doesn't tell me anything about the lifetime of an object, except through a historical association to object lifetimes in C, due to popular stack frame conventions. Note also that in some implementations, thread-local storage is on the stack of a thread, but is not limited to the scope of any single function.

Heap is again an overloaded name, indicating either a type of sorted container or a free-store management system. This is not the only free store available on all systems, and nor does it tell me anything concrete about the lifetime of an object allocated with new.




回答2:


Most implementations use a stack to back objects with automatic storage. This is not required by the standard, but it works out well on most CPU architectures these days.

Implementations use various strategies to back objects with dynamic storage duration. I'm not sure a heap is the best way to describe what modern memory allocators use, but that appears to be the "historical" term for that.

So automatic/dynamic storage are terms the standard uses to classify ("abstract") object lifetimes. Those are the proper terms to use if you want to talk objects as the standard describes them.
Stacks and heaps are ("concrete") implementation techniques that can be used to back them. Using those terms is less correct, unless you're talking about a specific implementation.




回答3:


The automatic/dynamic storage terms are preferable simply because this is what the standard requires. Stack/heap are implementation based and can theoretically be implemented another way.




回答4:


Technically speaking stack/heap allocation are implementation details, while automatic/dynamic storage are the more general terms. The standard itself doesn't mandate that the allocator has to use a stack/heap. Therefore automatic/dynamic is the more proper term, although personally I find that distinction to be a bit overly pedantic.




回答5:


The terms "static storage duration", "automatic storage duration", and "dynamic storage duration" appear in the C++ standard.

The terms "stack" and "heap" are used to refer to features in the Standard Library (stack<>, make_heap(), push_heap(), etc.) which have little to do with storage duration.




回答6:


Stack and heap bring in concepts related to implementation into picture, whereas the terms "automatic" and "dynamic" are more general



来源:https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!