If someone knows how to use VirtualAlloc properly, I could certainly use some help, because I really don't understand why it acts like it does and the doc is of no help.
Take this for example:
The first only allocates 1024 bytes, although I ask for 4K.Code:int* p = (int*)VirtualAlloc(NULL, 1024 * 4, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); int* p2 = (int*)VirtualAlloc((void*)((DWORD)p + 1024 *4 + 1), 1024 * 4, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); for (int i = 0; i <= 4096; i++) p[i] = 0;
The doc clearly says (for dwSize, the second argument):
And since a page size is 4K, I should get 4K, should I not? Whatever I specify, I get only 1024 bytes as apparent from the for loop which fails on i = 1024 (so 0 - 1023 works without access violations).Code:If the lpAddress parameter is NULL, this value is rounded up to the next page boundary.
The second VirtualAlloc simply fails with "an attempt to use an invalid address."
It's only (or is supposed to be) only 1 byte after the first allocation.
But I can't get any address that way, and I have no idea why.
IF anyone could help me understand VirtualAlloc... I want to play a little with it, but the doc is of absolutely no help at all.