0

The terms "automatic" and "dynamic" storage are arguably preferable in the C++ specifications over "stack" and "heap" respectively because the C++ specifications do not require that allocation/deallocation be implemented specifically using the stack/heap model.

Are there any alternate models for allocation/deallocation other than stack and heap?

4

2 Answers 2

1

There's been plenty of research done in allocators (fortunate or not), with different memory layout, segregation, etc. Andrei Alexandrescu authored a cool presentation about those CppCon 2015: Andrei Alexandrescu “std::allocator...”. You may find it useful.

One of the examples he provides may shed some light on the possibilities:

typedef Segregator<4096,
    Segregator<128,
        Freelist<Mallocator, 0, 128>,
        MediumAllocator>,
    Mallocator>
Allocator;

Allocation strategy:

  • if object is smaller than than 4096B:
    • if object is smaller than 128B use a Freelist (batches of elements),
    • else use a MediumAllocator (supposedly good for medium sized objects),
  • else use Mallocator (based on malloc) to alloc memory block.

Ergo depending on the type of objects, you may use a different allocation strategy (there's also a stack-based allocator to choose from).

3
  • Re "there's also a stack-based allocator to choose from", I doubt if a std::allocator can allocate from the machine stack. One problem is that a call of its allocation function, and the call that invoked that, and so on, uses the machine stack. There is at least one proposal for variadic arrays in C++ (a bit safer than non-standard and not very portable alloca), but I think that to work, it needs compiler support. Jun 6, 2016 at 12:05
  • Anyway, the small objects allocator was discussed in Andrei's now classic book "Modern C++ Design", and therefore is nearly certainly part of the Loki library. I think some mention of that, and linking, would be nice. ;-) Jun 6, 2016 at 12:07
  • @Cheersandhth.-Alf - the idea presented in the video is to give up on std::allocator entirely, and do it simpler in code, with StackAllocator being a simple template with char buffer. See the video at: 36:02. It is all of course compile-time, unlike C-like variadic arrays.
    – hauron
    Jun 6, 2016 at 12:12
0

The terminology opinions seem to be based on faulty assumptions.

Regarding allocation schemes, it's difficult to grok what you mean by "heap", but if you mean explicit deallocation of dynamically allocated objects, then C++ has never formally required that. When garbage collection gained some support in C++11 it was because it was already proven technology, with e.g. the Boehm garbage collector for C++03.

C++ does require and has always required a stack, but not any particular implementation of that stack. With fine-grained cooperative multitasking it may be that we'll see implementations that use linked list based stacks. I don't know any such yet, though, as of medio 2016.

5
  • C++ does and has always required a stack Are you talking about a memory stack or std::stack? Jun 6, 2016 at 11:48
  • @NathanOliver: A memory stack. You can't have general recursive functions without a stack. Hence the standard's use of the word, "stack". ;-) Jun 6, 2016 at 11:54
  • it's difficult to grok what you mean by "heap" > most likely, that area of memory that is used to allocate dynamic objects in...
    – Niall
    Jun 6, 2016 at 12:00
  • @Niall: I agree that ordinarily "heap" would mean that, but it doesn't make much sense in the context of this question, as "models for allocation/deallocation". If the OP did mean that then I should mention static lifetime? Jun 6, 2016 at 12:10
  • I don't really get what the "models" part is, maybe he wants to know if there are any other implementations for automatic and dynamic memory other than the "classic" stack and heap - and this takes me back to my assembler days.
    – Niall
    Jun 6, 2016 at 12:20

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