https://stackoverflow.com/questions/8036474/when-vectors-are-allocated-do-they-use-memory-on-the-heap-or-the-stack is concise.
- The vector “shell” can be on stack or heap or static memory, as you wish.
- The continuous array + the payload objects are always allocated on the heap, so that the vector can grow or deallocate them.
Deallocation is explained in https://stackoverflow.com/questions/13944886/is-stdvector-memory-freed-upon-a-clear
P9 [[c++game development primer]] has a short implementation without using heap. The memory pool comes from a large array of chars. The allocator keeps track of allocated chunks but doesn’t reuse reclaimed chunks.
It showcases the header associated with each allocated chunk. This feature is also part of a real heap allocator.
reinterpret_cast is used repeatedly.
http://stackoverflow.com/questions/11108328/double-alignment Wug’s answer echoes Ashish’s comment that char fields should be grouped together.
I now think there are various overhead with DMA
* search the free list for a suitable chunk
* If an allocation is needed where the heap is almost used up, glibc must grab more memory from the kernel, then hand out a slice of it.
Linux alloca() and variable-length-array both avoids some of the overhead. See P267 [[linux sys programming]].
If a low-latency module does a ton of malloc(), then alloca() might outperforms malloc() easily. We should benchmark.