I am trying to use VirtualAlloc to reserve and commit a block of memory, and then again to expand that block. Unfortunately, it returns NULL with the ERROR_INVALID_ADDRESS error, even though VirtualQuery says the requested range of addresses is free. Here is my code:
void* allocation = VirtualAlloc(NULL, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); void* desiredNextAllocation = (char*)allocation + 4096; MEMORY_BASIC_INFORMATION info; size_t memory_info = VirtualQuery(desiredNextAllocation, &info, sizeof(info)); void* extended = VirtualAlloc(desiredNextAllocation, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
The first distribution returns 0x00000000000d0000. A call to VirtualQuery results in the following data in "info":
BaseAddress 0x00000000000d1000 void * AllocationBase 0x0000000000000000 void * AllocationProtect 0x00000000 unsigned long RegionSize 0x00000000000ff000 unsigned __int64 State 0x00010000 unsigned long Protect 0x00000001 unsigned long Type 0x00000000 unsigned long
I interpret this as meaning that there are 0xff pages available, starting with 0xd1000, that are in the MEM_FREE state. So why is my attempt to freeze the page at 0xd1000 fail?
I am running Windows 7 and this is a 64 bit build.
I read a few StackOverflow posts about VirtualAlloc, but they all seem to imply that this code should work just like my understanding of the documentation.
source share