53

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.

6
  • I take it the following doesn't answer your question? stackoverflow.com/questions/408670/stack-static-and-heap-in-c
    – NPE
    Feb 7, 2012 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. Feb 7, 2012 at 18:36
  • possible duplicate of Proper stack and heap usage in C++?
    – NPE
    Feb 7, 2012 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 7, 2012 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 7, 2012 at 18:42

6 Answers 6

59

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.

4
  • 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). Feb 13, 2012 at 18:30
  • What other free stores are there? Actually, in what sense is free store free and stack memory is not free? Jan 2, 2020 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 2, 2020 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 2, 2020 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.

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.

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.

0
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.

1
  • Sorry for downvoting your previous answer, I realized the question was ambiguous. +1 Feb 7, 2012 at 18:58
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.