How to Map Files into Memory in C (mmap, memory mapped file io)

Jacob Sorber · 133K views · 2018 · 4:47

Open on YouTube

C, C++, Rust, Go & systems languagesOperating systems & kernelstutorial

Why Stack Overflow recommends it

Stack Overflow users link it as a quick, concrete introduction to memory-mapped file I/O in C, both as an alternative to malloc plus fread and as a starting example for mmap questions.

Recommended by 1 people across 1 different months, 2018 to 2022.

What it is

A short tutorial by Jacob Sorber showing how to use mmap() in C to map a whole file into memory and access it like an array instead of calling read or fread. It walks through opening the file, getting its size, mapping it and unmapping it.

What people said

Recommends Mihir Luthra · 2019-09 · Stack Overflow · on “Unexpected end of file on a tar.gz file”

Instead of 2 steps for `malloc` then `fread`, you can simply use `mmap(2)`, would be faster too. See this [this video] .

Cites Hemant Bhargava · 2018-12 · Stack Overflow · on “reading selected lines of file using mmap”

I posted one question [LINK] , which was related to faster reading of a file, by skipping specific lines but that does not seem to go well with standard c++ api's. I researched more and got to know what memory mapped files could come handy for these kinds of cases. Details about memory mapped files are here [this video] . All in all, Suppose, the file(file.txt) is like this: A quick brown fox // Blah blah // Blah blah jumps over the little lazy dog And then in code, opened file, Read that as memory mapped file and then iterate over the contents of the char* pointer, skipping the file pointers itself. Wanted to give it a try before reaching to an conclusion on it. Skeleton of my code looks like th

Cites autistic456 · 2020-05 · Stack Overflow · on “How to change value at address from mmap without malloc?”

Having this: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> #include <sys/mman.h> int main (void) { int fd = open("./sometext.txt", O_RDONLY); struct stat sb; if(fstat(fd,&sb)) perror("in function fstat"); printf("file size is %ld bytes\n\n",sb.st_size); char* file_p = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); printf("printing file as an array of chars \n\n"); for(int i =0; i<sb.st_size ; i++){ if(file_p[i]=='a') //cannot do this file_p[i]='5'; //string/char is read-only, but then how? printf("%c",file_p[i]); } munmap(file_p, sb.st_size); close(fd); return 0; } As I found from other questions, to change str

Cites MasonMac · 2020-05 · Stack Overflow · on “What are memory mapped files?”

Recently, I've come across this video [this video] that shows how to use mmap() with file io. However, I can't find the video of his that documents the function. I don't have an understanding of what it is, why it exists, nor how it relates to files. Too much of the jargon is flying over my head to make sense of it. I had the same problem with sites like Wikipedia.

Cites Patrick Na · 2020-11 · Stack Overflow · on “Accessing 'shared memory' with Python 3.x < 3.8”

@Joac pointed me to the right direction and I learned a little bit more about shared memory, mmap and then also about shmget , shmat , etc. For anyone who is interested in learning more about it I found the following videos and blog-post in particular useful: How to Map Files into Memory in C (mmap, memory mapped file io) [this video] How to Set up Shared Memory in Your Linux and MacOS Programs. (shmget, shmat, shmdt, shmctl, ftok) [LINK] Using python to read shared memory under Linux [LINK] In the end I solved the problem by means of the posix_ipc [LINK] module that was introduced for Python2.x already and in fact works very similar to the native Python3.8 solution I am referring to above. Examp

Cites Cprogrammer · 2022-04 · Stack Overflow · on “Question about mapping a file and memory using mmap()”

I was writing a small program which maps file and memory using mmap(). And searched a lot of places and found an interesting example from [this video] I wrote a short program based on the youtube video and works well. But what I want to do is a little different. This example assumes a file which already has a content. What I want is use an existing file which is empty or create a file inside of the program. mmap() between a memory area and empty file update contents of the file by using pointer mmapped inside of the program. update contents of the file by accessing the file directly outside of the program. Here is the code I mostly copied from the youtube video. And this code works well. #include