Depending on your platform, you have several options:
- Since this is C ++, you can trick and call one of the STL allocators . I doubt the interviewer wanted to, but who knows for sure?
- You can always use fixed-size pools, as several answers suggest.
sbrk also an option, but its use is not recommended, and it is no longer part of POSIX.- You can also use
mmap (or VirtualAlloc or CreateFileMapping on Windows) as a memory source, but if you want chunks of memory to be smaller than entire pages, you still need to write code to control the memory returns these functions.
Your allocator should ensure that the memory alignment is correct for your platform: on some systems, non-smooth access to memory is an invalid operation and other performance is included there compared to aligned access. In real, production code, you also probably want to provide a free operation to avoid all system memory and locks to ensure the security of your heap.
source share