Could anyone please explain what memory mapped files are and why this method is better then reading a file with I/O operations when needed? i've read msdn...wiki...and im still confused at why i cant understand how this works/ what it is doing exactly -thx

Why memory maps? Exe's, Dll's or shared objects need to be mapped into memory before they can be execute, you can't execute code from a drive it must be mapped into memory(RAM). Is it better? Depends, what are you reading and what are you doing with it.

Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.

to me this says "the program is still on the hard drive but the pc thinks its in RAM" i dont understand the purpose of this then

Check out the strace of a 'hello, world' program, especially line 9 which opens the standard C library /lib64/libc.so.6, the next line 10 which reads the file and the next line 12 which maps the code from /lib64/libc.so.6 into memory.

execve("./test", ["./test"], [/* 83 vars */]) = 0
brk(0)                                  = 0x18dc000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffb72108000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=123320, ...}) = 0
mmap(NULL, 123320, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ffb720e9000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\354\1\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1711148, ...}) = 0
mmap(NULL, 3586216, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ffb71b81000
mprotect(0x7ffb71ce4000, 2093056, PROT_NONE) = 0
mmap(0x7ffb71ee3000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x162000) = 0x7ffb71ee3000
mmap(0x7ffb71ee8000, 18600, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ffb71ee8000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffb720e8000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffb720e7000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffb720e6000
arch_prctl(ARCH_SET_FS, 0x7ffb720e7700) = 0
mprotect(0x7ffb71ee3000, 16384, PROT_READ) = 0
mprotect(0x7ffb72109000, 4096, PROT_READ) = 0
munmap(0x7ffb720e9000, 123320)          = 0
fstat(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 1), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffb72107000
write(1, "Hello, World!\n", 14Hello, World!
)         = 14
exit_group(0)                           = ?

Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.

to me this says "the program is still on the hard drive but the pc thinks its in RAM" i dont understand the purpose of this then

Could you post the link that the above statement is taken from.

Memory mapped files: this allows you to write to a physical on-disc file as though you were writing to memory. Ie, the file is "mapped" to a memory location. Let's say you want to change the data 100 bytes into the file, replacing the contents with "hello world". Ok, assume the file is mapped to the pointer:

char* pMappedFile;

Once that is done (won't get into details how to do that here - this is theory!), you can do this:

const char* hello = "hello world";
strncpy(&pMappedFile[100], hello, strlen(hello));

Whatever was at that location in the file, on disc, has now been changed to "hello world".

So, the idea is that in some instances, this can significantly simplify your code as you don't need to concern yourself with seek()/write() and other file operations. You just treat the file as a chunk of program data memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.