Gaurav Gautam
15 posts
Has VirtualAlloc failed for anyone when using baseAddress (win10)?

I am on windows10 and virtualAlloc fails with error code 487 (accessing invalid memory address) whenever I pass a baseAddress. This is done for looped live code editing in the hmh streams. I tried to find any solutions on google and the closest thing I got was some advise about never allocating with a baseAddress.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2e4166b4-e99e-48e1-807f-df72853a2208/virtual-allocation-fails-for-the-given-memory-address?forum=vcgeneral

An interesting post at the end of this thread says that someone has two identical(ish?) devices running windows 7 and one always succeeds in allocating at the address and the other always fails. And the poster is asking if maybe there is some os level setting to enable this.

Has anyone else faced the always failing problem in windows 10 and if so how did you solve it?

Simon Anciaux
1184 posts
Has VirtualAlloc failed for anyone when using baseAddress (win10)?

Could you make a small and simple reproduction case that we could try on our end to be sure it's not an issue with your code ?

Gaurav Gautam
15 posts
Has VirtualAlloc failed for anyone when using baseAddress (win10)?
Edited by Gaurav Gautam on

Yeah so I wrote the following:

#include <windows.h>
#include <stdio.h>
#include <stdint.h>

#define Kilobytes(count) ((count) * 1024)
#define Megabytes(count) (Kilobytes(count) * 1024)
#define Gigabytes(count) (Megabytes(count) * 1024)
#define Terabytes(count) (Gigabytes(count) * 1024)

int WINAPI wWinMain(
HINSTANCE Instance,
HINSTANCE PrevInstance,
PWSTR CommandLine,
int ShowCode) {

LPVOID BaseAddress = (LPVOID)Terabytes((uint64_t)2);

/********In here if I replace BaseAddress with 0 it works****/
void *Mem = VirtualAlloc(BaseAddress, 64 * 1024, MEM_COMMIT, PAGE_READWRITE);
/*******************^^*****************/

DWORD Error = GetLastError();
char Buffer[256];
printf("Error code: %d\n", Error);
if (Mem) {
free(Mem);
printf("Memory was allocated and freed\n");
} else {
printf("Memory was not allocated\n");
}

return 0;
}


I am attaching 2 files showing the success and error runs in visual studio. Look at the watch window in the bottom left to see the values of the error and pointer to the memory allocated. You may have to open the images in a new tab to actually see the small text.

14 posts
Has VirtualAlloc failed for anyone when using baseAddress (win10)?

It looks like the flAllocationType parameter is wrong for VirtualAlloc.

"Attempting to commit a specific address range by specifying MEM_COMMIT without MEM_RESERVE and a non-NULL lpAddress fails unless the entire range has already been reserved. The resulting error code is ERROR_INVALID_ADDRESS."

Also, memory allocated with VirtualAlloc should be freed with VirtualFree instead of free.

Gaurav Gautam
15 posts
Has VirtualAlloc failed for anyone when using baseAddress (win10)?

@William thank you. This worked. I should have read the docs more thoroughly.