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.
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).
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.
I'm guessing sizeof(int) is 4 (not 1).
And <= would make you overrun.
Ah... silly bug!
Why not use ZeroMemory() or even the portable memset()?
It was merely a test to see if I could access all the allocated memory.
As for the second, aren't you ignoring the alignment requirement by trying to go 1 byte after the previous allocation?
Dunno. It's hard to tell usually. Such things are usually found out by debugging. But, in general, I think, maybe it is.
I'm starting to get the hang of it, but I'm still having trouble with some of the code using it (and the example above isn't it).
I could post the entire code that I'm working with, but I'm betting it's not going to be that fun to go through it to find mistakes.
I'm doing an experiment with VirtualAlloc since it can reserve and allocate memory ranges anywhere in the virtual memory, so I don't have to copy data when I expand an array.
It seems that it's paying off!
Around 200 ms vs. 350 ms in Release for VirtualAlloc vs. Heap for 10 * 7 iterations!
Wow! Now I just need to perfect this VirtualAlloc code!
Just checked, and it should actually just silently round down.
Hmm, but what is the allocation granularity on your computer?
4K - aka 4096 bytes.
I need more testing, of course. Using a bigger buffer shows performance gains and I haven't been able to make it possible to continue to reserve pages after the allocates one seamlessly for some reason.
Maybe this can be of some help to you.
Already saw that example.
I don't know why people think I still need help, though :p I don't... really. I think.
I think I just found my infamous bug that was screwing up my tests.
With heap functions:
double max = pow(10.0, 8.6);
DWORD dwTick1 = GetTickCount();
for (int i = 0; i < max; i++)
Array[i] = 12; //12345678;
DWORD dwTick2 = GetTickCount();
DWORD dwTick3 = dwTick2 - dwTick1;
Now I just have to try a bigger buffer! The bigger the faster.
With some optimizations, that time is now 1609 ms! :D:D:D:D:D
Now if anyone could just optimize assembly...