malloc vs mmap in C

Solution 1:

Look folks, contrary to common believe, mmap is indeed a memory allocation function similar to malloc..

the mmaped file is one use of it.. you can use it as memory allocation function passing -1 as file descriptor..

so.. the common use is to use malloc for tiny objects and mmap for large ones..

this is a good strategy..

i use alloca() to for function scope only variables..

Solution 2:

mmap doesn't actually load the file into memory, so it will load faster, but editing it will be slower.

Another point is that mmap doesn't use any memory, but it takes up address space. On a 64bit machine, most of the memory address space will not have memory, so you could load up huge files, say 5GB, that you would not want to malloc.