Search code examples
c++winapimemory-managementmemory-mapped-files

Strange behaviour of memory mapped file, some observations and some questions


Please look at this code below.

#include <windows.h>

void Write(char *pBuffer)
{
//  pBuffer -= 4*sizeof(int);
    for(int i = 0; i<20; i++)
        *(pBuffer + sizeof(int)*i) = i+1;
}

void main()
{
    HANDLE hFile = ::CreateFile("file", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if(INVALID_HANDLE_VALUE == hFile)
    {
        ::MessageBox(NULL, "", "Error", 0);
        return;
    }

    HANDLE hMMF = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 32, NULL);

    char *pBuffer = (char*)::MapViewOfFile(hMMF, FILE_MAP_WRITE, 0, 0, 0);

    Write(pBuffer);

    ::FlushViewOfFile(pBuffer, 100);

    ::UnmapViewOfFile(pBuffer);
}

I have allocated only 32 bytes yet when I attempt to write past the allocated size, I don't get any error at all. Is this by design or is this a bug in Windows code? However, if you include the commented part, it gives error, as expected.

I ask this because I am thinking of using this "feature" to my advantage. Can I? FYI, I have Win XP ver 2002 SP 3 but I suspect this to be "fixed" in newer Windows' which might fail my code, IDK. Any useful link explaining some internals of this would really help.

Thanks


Solution

  • The virtual memory manager has to map memory by the page, so the extent will in effect be rounded up to the nearest 4kB (or whatever your system page size is).

    I don't think it's documented whether writes into the same page as mapped data, but beyond the end of the mapping, will be committed back to the file. So don't rely on that behavior, it could easily change between Windows versions.