views:

222

answers:

2

Will the memory block returned by VirtualAlloc always be aligned with the page size? In other words, will the modulus always be zero of the return value from VirtualAlloc and the page size?

+1  A: 

From the MSDN documentation for VirtualAlloc:

If the memory is already reserved and is being committed, the address is rounded down to the next page boundary.

So the answer is yes, if you are committing the memory and not just reserving it.

flodin
So you're saying following call will always return an address that is rounded down to the next page boundary. Notice the lpAddress parameter is NULL and therefore could not of been reserved.VirtualAlloc(NULL, 1024, MEM_COMMIT, PAGE_READWRITE)
Mike
Yes that's what I'm saying. But I think you misunderstand the meaning of "reserved." The documentation talks about the MEM_RESERVE flag, which has nothing to do with the lpAddress parameter.
flodin
@flodin: I believe I do know what "reserved" means. My example function call didn't operate on a "reserved" page(s). I never use "reserved" pages. The documentation never mentions what happens if a page(s) is commit without first be reserved. My use of NULL was just a quick way of saying I never reserved that block of memory.
Mike
+2  A: 

Well, yes.

After all, you call VirtualAlloc to allocate some memory pages. You cannot allocate only 1 byte without receiving a whole page, so it makes sense that you will receive a buffer aligned with the page size.

botismarius