Why can't I reserve two contiguous memory regions in the same allocation without reserving both with a single call?
NickName:Mikubyte Ask DateTime:2016-12-01T09:24:05

Why can't I reserve two contiguous memory regions in the same allocation without reserving both with a single call?

I have two memory regions I'm allocating with VirtualAlloc: 0x1E0000 (Size: 0x39000, Reserve) and 0x219000 (Size: 0x3000, Commit). These are both within the same allocation boundary (which in this case is rounded to 0x40000 (64K*4)), and the second region starts where the first ends.

Now forget the commit part for a minute. If I MEM_RESERVE the first 0x39000 and then MEM_RESERVE the next 0x3000, I get ERROR_INVALID_ADDRESS. However, if I MEM_RESERVE both in one go, 0x39000+0x3000=0x3C000, then it works, and I can use MEM_COMMIT to commit the second region successfully.

Why is that? Why can't I reserve each part on its own instead of as one big reserved region? After reserving the first region the remaining area within the allocation (0x219000-0x21FFFF) will have the MEM_FREE state, so how come I cannot reserve the first 0x3000 of the remaining 0x7000 in the allocation boundary?

Copyright Notice:Content Author:「Mikubyte」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901127/why-cant-i-reserve-two-contiguous-memory-regions-in-the-same-allocation-without

Answers
Harry Johnston 2016-12-01T01:32:38

You cannot have two separate reservations within the same allocation boundary.\n\nFrom the documentation for VirtualAlloc:\n\n\n lpAddress [in, optional]\n The starting address of the region to allocate. If the memory is being reserved, the specified address is rounded down to the nearest multiple of the allocation granularity.\n\n\n(emphasis mine)\n\nSo your request to reserve memory starting at 0x219000 actually attempts to reserve memory starting at 0x210000, which is inside the existing allocation and hence illegal.\n\n(It should also be noted that there is no guarantee that any particular region of virtual memory will be available for you to reserve; Windows may have already reserved it for some other purpose. Best practice is to always set the lpAddress parameter to NULL, allowing Windows to choose an address for you.)",


More about “Why can't I reserve two contiguous memory regions in the same allocation without reserving both with a single call?” related questions

Can a process have multiple parents? why or why not?

I know a parent process can have multiple children. But can a child have multiple parents? why or why not?

Show Detail

Why it can get in the For loop?

array has 0 objects, so array.count is 0, and then (array.count - 1) is -1, i < -1 is -1, why it can get in this for loop? for (int i=0; i<array.count-1; i++) { NSLog(@"why it can get in...

Show Detail

Can not understand why Hbase not A in CAP

I read a document said that Hbase is Consistency and Partition in CAP theory. But I can not understand why Hbase is not A? I think Hbase can always keep service available, so why not A? My

Show Detail

Why can a class not be defined as protected?

Why can we not define a class as protected? I know that we can't, but why? There should be some specific reason.

Show Detail

Why can a class not be defined as protected?

Why can we not define a class as protected? I know that we can't, but why? There should be some specific reason.

Show Detail

Why can there be a NullReferenceException?

I have the following code: try { using (var stream = new MemoryStream()) { var ms = stream; if (control is DockLayoutManager) { if (control.Dispatcher == null || co...

Show Detail

Why a link can not contain a link

Why a link can not contain a link? Why can not use such a structure? What is the rule on the w3c specification? <pre> <a href="xxx"> <a href="yyy"> &a

Show Detail

Why can not subclass AVPlayerViewController

In Apple API Reference, it says do not subclass AVPlayerViewController as above. I want to know why. I write a demo subclass AVPlayerViewController intentionally, it can play video normally. I can ...

Show Detail

Why Hashids can be decoded?

I've used this popular library Hashids. As this poster mentioned,Hashes produced by these algorithms are designed to be 'one-way'. Then, why is it possible for a hash value to be decoded? I've re...

Show Detail

Why can an identifier not start with a number?

Why in java (I dont know any other programming languages) can an identifier not start with a number and why are the following declarations also not allowed? int :b; int -d; int e#; int .f; int 7g;

Show Detail