Friday, March 13, 2009

Memory Mapped files in Boost and C++

Memory Mapped I/O is a crucial methodology for increasing I/O performance. The standard I/O approach is costly due to system call overhead and memory copying. Anyway, the memory mapped approach has its cost in page faults which should be carefully handled.

Before BOOST there was no portable API for memory mapped I/O. POSIX-compliant systems, such as UNIX, Linux, Mac OS X or OpenVMS supports the Posix function mmap(), while Microsoft Windows supports CreateFileMapping().

Boost C++ provides a portable implementation of memory-mapped files for Microsoft Windows and POSIX-compliant platforms.

Here you have the code for portable memory mapped file reading in Boost.


  1. Antonio, avrei una domanda...
    This is my first program with Boost, it uses a memory mapped file, and it seems to work correctly. However I noticed that the file, which should be created in the RAM, is in effect created on the hard disk. I must have missed something, but I cannot find what...
    Ops... I think I cannot attach my code to this message (they are just a few lines). Grazie!

  2. It's great, man! I am dealing with reading very large files (~1GB) so using memory-mapped file mechanism is a good option.

  3. i am trying to use memory mapped files but, have not found any example or tutorial yet , that i can use to learn about the use of boost function , i am new to boost and not a pro at cpp... can some one please send me a link or example that can simply read and write to a memory mapped file ... that will be really nice ..