0

If I make a std::vector which nearly as large as the heap size of the process is, what happens if there is a small chunk of memory allocted just in the middle of the heap?

Will the "alloc" call be successful and the memory management of the computer will take care of moving around the memory pages in order it is possible to use continuous memory?

In my particular case I have an ARM Cortex A4 without Operating system and with gcc or iar as the compiler. But I am also interested on a more general consideration independent of my hardware. I guess that each system having a memory management unit should handle this, isn't it?

Ernie Mur
  • 350
  • 1
  • 15
  • Check this [memory fragmentation](https://stackoverflow.com/questions/3770457/what-is-memory-fragmentation) Strongly suggesting not to reinvent the wheel, i.e. write your own memory allocator, unless you don't 100% sure what are you doing. Use some generic propose memory allocator like jemalloc, or tcmalloc etc. jemalloc knows how to properly mix small and huge memory block in memory. See https://www.facebook.com/notes/facebook-engineering/scalable-memory-allocation-using-jemalloc/480222803919/ – Victor Gubin Jun 26 '18 at 10:14
  • I wouldn't normally expect an implementation to quietly defragment memory to ensure a memory allocation succeeds, since such tricks would cause performance impacts (e.g. hanging while waiting for the memory to be defragmented). It is, however, conceivable that a virtual memory system might allow a process to "see" a block as contiguous, even if the underlying physical memory is not contiguous (i.e. a mapping that is invisible to the running process). I'm yet to encounter an implementation which does such things though - a failed allocation is more likely in the situation you describe. – Peter Jun 26 '18 at 10:34
  • 1
    The short answer: the memory allocation will fail. The End. – Sam Varshavchik Jun 26 '18 at 10:45

0 Answers0