Search code examples
c++cwinapiheap-memorymemory-mapped-files

Pagefile-backed memory-mapped files vs. Heap -- what's the difference?


What is the advantage of using a memory-mapped file backed by the system paging file (through CreateFileMapping(INVALID_HANDLE_VALUE, ...), instead of just allocating memory from the heap the usual way (malloc(...), HeapAlloc(...), etc.)?

i.e. When should I use which?


Solution

  • It's lower level, it gives you more than malloc does:

    • You can share the mapping with other processes (of course you also need to synchronize)
    • You can set permissions on the memory (for example you can have read-only memory via PAGE_READONLY)
    • You can set some cache / page parameters