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

StackOverflow https://stackoverflow.com/questions/9182127

Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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.

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.

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.

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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top