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?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Evan
  • 1,348
  • 2
  • 10
  • 20
  • 3
    To me it seems a close duplicate of [Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management?](http://stackoverflow.com/q/9181782/514235), but leaving upto other guys. Don't want it to get reopened, after me closing. – iammilind Jun 06 '16 at 11:40
  • I find the question too ambiguous: like are you counting garbage collectors as *an alternative model* for memory management? – BeyelerStudios Jun 06 '16 at 11:41
  • This seems to be **two** separate questions: one asking for opinions about terminology, which should be closed, and one technical about allocation schemes. Can you remove the terminology part? – Cheers and hth. - Alf Jun 06 '16 at 11:42
  • 1
    Static Allocation is another model used most probably for allocation of memory which can remain valid through out the life of program. For example: global variables. – sameerkn Jun 06 '16 at 11:45

2 Answers2

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

hauron
  • 4,550
  • 5
  • 35
  • 52
  • 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. – Cheers and hth. - Alf Jun 06 '16 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](https://en.wikipedia.org/wiki/Loki_%28C%2B%2B%29). I think some mention of that, and linking, would be nice. ;-) – Cheers and hth. - Alf Jun 06 '16 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 06 '16 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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • *C++ does and has always required a stack* Are you talking about a memory stack or `std::stack`? – NathanOliver Jun 06 '16 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". ;-) – Cheers and hth. - Alf Jun 06 '16 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 06 '16 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? – Cheers and hth. - Alf Jun 06 '16 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 06 '16 at 12:20