STATUS_SECTION_TOO_BIG (0XC0000040) Fix – Map a Large File
This error means your app tried to map a file section larger than 2GB on a 32-bit process. The fix is to target x64 or use memory-mapped file paging.
You hit the 2GB wall. Here's the fix.
STATUS_SECTION_TOO_BIG (0XC0000040) slaps you in the face when you try to map a file that's bigger than 2GB on a 32-bit process. The OS literally says "nope, can't fit that." The fix isn't complicated, but you need to understand what's happening.
The Fix: Go 64-bit or Split the Mapping
You've got two real options. Pick the one that fits your situation.
Option 1: Recompile as x64 (the real fix)
If you control the source code, target x64 instead of x86. A 64-bit process gets a 8TB user address space—no more 2GB limit for file mapping sections. Here's the change in Visual Studio:
- Open Project > Properties > Configuration Properties > Linker > Advanced
- Set Preferred Base Address to default (or leave it)
- Under Build > Configuration Manager, set Active Solution Platform to x64
If you don't see x64, create it: Active Solution Platform > New > Type x64, copy from Win32.
Option 2: Map the file in chunks (no source change needed)
Can't recompile? Use CreateFileMapping with dwMaximumSizeLow and dwMaximumSizeHigh set to map only a portion of the file. Then call MapViewOfFile with dwNumberOfBytesToMap set to something under 2GB—say 0x7FFFFFFF. Here's a C++ snippet:
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0x7FFFFFFF, NULL);
void* pView = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0x7FFFFFFF);This maps the first ~2GB. For the rest, unmap and map the next chunk by changing dwFileOffsetHigh. It's clunky but works when you're stuck with a 32-bit app.
Why It Happens: The 32-bit Address Space
What's actually happening here is that the Windows memory manager uses a section object to back memory-mapped files. The CreateFileMapping function creates this section. On a 32-bit process, the maximum size of any single mapping is limited to 2GB (or 3GB with /3GB boot switch, but that's rare). The error 0XC0000040 is returned when the section object's requested size exceeds the process's virtual address space capacity.
The reason option 1 works is that on x64, the user-mode address space jumps from 2GB to 8TB. The section can be as big as the file itself. Option 2 works because each mapping view is under the 2GB threshold, so the OS doesn't complain—you just need to manage multiple views yourself.
Less Common Variations
You might see this error in a few other scenarios:
| Scenario | What's really happening |
|---|---|
| Database server (e.g., SQL Server 2005 Express) | Older 32-bit SQL editions hit this when the database file grows past 2GB and memory-mapped I/O is used. Fix: upgrade to 64-bit SQL Server. |
| Custom file viewer in WPF | File.ReadAllBytes on a file >2GB triggers it internally because the CLR uses memory mapping. Fix: use StreamReader with buffered reads. |
| Virtual machine disk images | Tools that map VHD/VMDK files as memory sections can fail on files above 2GB. Fix: use regular file I/O instead of mapping. |
Prevention: Don't Map Giant Files on 32-bit
If you're writing new code, test with files over 2GB early. Use GetNativeSystemInfo to check if you're on 64-bit Windows. If you're compiling for 32-bit, never assume the file fits. Either:
- Use streaming I/O (
ReadFilein chunks) - Always map less than
0x80000000bytes per view - Target 64-bit from day one if the app deals with large files
One more thing: the /LARGEADDRESSAWARE linker flag gives 32-bit apps up to 3GB on 64-bit Windows, but that still doesn't remove the 2GB mapping limit per section. It won't fix this error. Don't bother.
Was this solution helpful?