6
votes

Just curious if I'm creating a shared memory array on Windows platform like so:

HANDLE hFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 1024 * 4, _T("mySharedMemName"));
if(hFile)
{
    VOID* pData = MapViewOfFile(hFile, FILE_MAP_ALL_ACCESS, 0, 0, 1024 * 4);

    //Is 'pData' array initialized with zeros the first time the 'hFile' is used?
}

Is the memory array initialized with 0's the first time I call this code snippet? And if no, how to make it zero initialized?

1
Most operating systems zero fill all pages that are newly mapped into the address space of a process (unless those pages are shared or backed by disk files or peripheral devices). Not doing so would be unwise from security point of view. - Hristo Iliev

1 Answers

8
votes

From the documentation:

The initial contents of the pages in a file mapping object backed by the operating system paging file are 0 (zero).