title: Mmap() an entire large file
body: <p>I am trying to "mmap" a binary file (~ 8Gb) using the following code (test.c).</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;sys/mman.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  
  #define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)
  
  int main(int argc, char *argv[])
  {
     const char *memblock;
     int fd;
     struct stat sb;
  
     fd = open(argv[1], O_RDONLY);
     fstat(fd, &amp;sb);
     printf("Size: %lu\n", (uint64_t)sb.st_size);
  
     memblock = mmap(NULL, sb.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
     if (memblock == MAP_FAILED) handle_error("mmap");
  
     for(uint64_t i = 0; i &lt; 10; i++)
     {
       printf("[%lu]=%X ", i, memblock[i]);
     }
     printf("\n");
     return 0;
  }
  </code></pre>
  
  <p>test.c is compiled using <code>gcc -std=c99 test.c -o test</code> and <code>file</code> of test returns: <code>test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped</code></p>
  
  <p>Although this works fine for small files, I get a segmentation fault when I try to load a big one. The program actually returns:</p>
  
  <pre><code>Size: 8274324021 
  mmap: Cannot allocate memory
  </code></pre>
  
  <p>I managed to map the whole file using boost::iostreams::mapped_file but I want to do it using C and system calls. What is wrong with my code?</p>