51

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I take it the following doesn't answer your question? http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c – NPE Feb 07 '12 at 18:34
  • 1
    @aix it does not. Or at least it doesn't say way one term is preferred over the other, or the difference. – Luchian Grigore Feb 07 '12 at 18:36
  • possible duplicate of [Proper stack and heap usage in C++?](http://stackoverflow.com/questions/599308/proper-stack-and-heap-usage-in-c) – NPE Feb 07 '12 at 18:37
  • 3
    I always thought it's because standard doesn't specify that automatic storage duration must neccesarily be implemented like a stack. – jrok Feb 07 '12 at 18:38
  • 2
    @LuchianGrigore: Until the edit, it was not at all clear to me that the question was about terminology and nothing else. Judging from the comments/answers, it was not at all clear to others too. – NPE Feb 07 '12 at 18:42
  • @aix yes, in my head it sounded better. Probably because I knew what I meant to ask :). Therefore, I edited. I hope the question is clear now. – Luchian Grigore Feb 07 '12 at 18:43

6 Answers6

58

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.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Also good to know: Not all systems use a "stack" for non-recursive functions. Also: It is possible to create dynamic objects on the "stack" (I think `boost::variant does` this). – Mooing Duck Feb 13 '12 at 18:30
  • What other free stores are there? Actually, in what sense is *free store* free and stack memory is not free? – AlwaysLearning Jan 02 '20 at 17:21
  • 1
    "Free store" just means a "store of free (memory) resources which could be allocated". So any system-specific way to allocate memory, such as `mmap` or `sbrk`, would count. – Useless Jan 02 '20 at 21:31
  • And apart from being an optional implementation detail, stack memory is on the one hand normal memory back by one of those system-specific allocators (except when it isn't like on SPARC, or when it doesn't exist) - and on the other hand a way of handling (de)allocation of that memory. – Useless Jan 02 '20 at 21:34
13

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
9

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.

Grizzly
  • 19,595
  • 4
  • 60
  • 78
7

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
Asaf
  • 4,317
  • 28
  • 48
6

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.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
1

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

Jannat Arora
  • 2,759
  • 8
  • 44
  • 70