Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 354
Luchian Grigore Avatar asked Feb 07 '12 18:02

Luchian Grigore


People also ask

Why stack memory is faster than heap?

Because the data is added and removed in a last-in-first-out manner, stack-based memory allocation is very simple and typically much faster than heap-based memory allocation (also known as dynamic memory allocation) typically allocated via malloc .

Why do we have difference memories like heap memory stack memory dynamic memory?

Stack space is mainly used for storing order of method execution and local variables. Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated.

What is the difference between dynamic variables and automatic variables?

Automatic variables are stored in a "function call stack". Dynamic: The lifetime of a dynamic object begins when memory is allocated for the object (e.g., by a call to malloc() or using new) and ends when memory is deallocated (e.g., by a call to free() or using delete). Dynamic objects are stored in "the heap".

What's the difference between heap and stack?

The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application. Primitive local variables are only accessed the Stack Memory blocks that contain their methods.


2 Answers

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.

like image 141
Useless Avatar answered Oct 06 '22 00:10

Useless


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.

like image 25
Mat Avatar answered Oct 05 '22 23:10

Mat