If I create 2 separate mappings of the same file in the same process will the pointers be shared?
in other words:
LPCTSTR filename = //...
HANDLE file1 = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0);
HANDLE fileMapping1 = CreateFileMapping(file1, NULL, PAGE_READONLY, 0, 0, 0);
void* pointer1 = MapViewOfFile(fileMapping1, FILE_MAP_READ, 0, 0, 0);
CloseHandle(fileMapping1);
CloseHandle(file1);
HANDLE file2 = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0);
HANDLE fileMapping2 = CreateFileMapping(file2, NULL, PAGE_READONLY, 0, 0, 0);
void* pointer2 = MapViewOfFile(fileMapping2, FILE_MAP_READ, 0, 0, 0);
CloseHandle(fileMapping2);
CloseHandle(file2);
Will pointer1 ever be equal to pointer2?
The reason I am asking is that I have several threads that need to search in a large (300+MB) file and I want to use memory mapping for that. However the process needs to be able to run on an old 32bit xp machine, so if each thread allocated their own copy in virtual memory then I could run out of memory.
pointer1cannot point to the same block of memory aspointer2. In fact, both pointers being identical is the most straight forward way to implement the coherency guarantee. There is no documented guarantee either way (with respect to identity of the pointers), though. - IInspectablefile2to be an argument for gettingfileMapping2, notfile1(copy/paste thing). The same thing on the following line. - Roman R.MapViewOfFilespecifically calls out that "file views derived from any file mapping object that is backed by the same file are coherent". Neither file mapping objects nor file handles need to be shared. I also do not understand the part in your comment that says: "You always get two different pointers being mapped to the same physical memory." - IInspectable