malloc
will use mmap
if this is beneficial.
What is the benefit of mmap
with MMAP_PRIVATE
flag and file instead of just use malloc
?
Access to the memory is read / write.
From what I know, the only benefit would be that system will swap the memory into the file instead of system swapfile. This file may be on faster disk etc.
Any other reason someone may decide to use it this way?
It really depends on the application. But one application may be mapping a shared memory segment (file), to which you want to make local modifications, without affecting the other users of that same shared memory. Thanks to Copy-on-Write only those pages of the memory segment that were actually altered need to be copied.