[Autogenerated] seen in an earlier module,
the differences between reserved and committed memory.
Now it's time to see those differences in the VMs tool and in code.
So I have an application here that I've written as
the basis for some experimentation.
That application is supposed to like a mini Excel where there is a big list
of cells that the user can put data in at the moment.
There's no functionality that is implemented behind
allocate read and write _______ here.
That's something we'll add to the code.
So let's see the basic code here.
We have a class here called Simeon D L G which is used to manage the dialog box.
I'm using the Windows template library.
WTL library to provide some easier way of creating some UI
but those details are not really important.
What is important is to manage the chunk of allocated
memory and some details about it.
So first you might want to use the size to provide
The maximum size for our work here.
So I'm going to create two variable size X and size wise and initialize
them to some value and perhaps it's better to use long,
long here to make it a 64-bit value.
If you want to work with even bigger numbers,
then we'll also add a simple integer for cell size and give it some value.
It's a 256 bytes and of course we can use any other number that we see fit.
The last thing,
which is important is to create the variable to hold on to the
address where we're going to store the chunk of memory.
We'll start with the committed memory and then we'll see what reserve looks
like and try to play around with the windows APIs there,
especially the actual dialogue.
So I'm just creating a few variables here and then we have
the uh the button for allocating stuff.
So let's just implement allocating and the way we'll do
that is by using the address here to fill that up with
whatever virtual Alak is returning.
So virtual reality is the main API in the virtual API that
allows us to commit memory to reserve memory while we're doing
that we can set the page protection.
So we'll start with the address in this case,
I don't really care,
I want to get some address back which is available in the process,
address space the size that I want is m size x
times m seis y times the sell side.
That would be the size of the entire matrix of data.
Just a linear address range,
then I'm going to reserve and commit the memory up
front and it's important to do both.
That's the correct thing to do.
If you just do men commit,
It will still work but that actually shouldn't really work correctly,
but it will work because of an old bag in the Microsoft
implementation of mutual dialogue.
That will now never be fixed because some code might depend on that
behavior where men commit is treated as men committed.
Men reserved when you have a new chunk on your hands.
But I'm going to do both of them for now and we'll see the
another option to use just meant reserve later on.
And I'm going to have a read,
write a block here and once we have that of course it could theoretically fail.
In fact in practice could also fail if the chunk is really, really big.
So I'm just going to use a simple message box here to display that effect.
So failed to commit to memory or let's just call that
allocating more generic fashion.
We have to have some title huge idea.
String resource in the application.
Just use icon error here to indicate that we have an error on our
hands and there's really nothing else we can do.
Now assuming this succeeds then we then we just want to
display that thing in the dialog box.
So we be able to take that address and use that in VM
app to correlate our allocations.
We're going to use a simple formatting string here for c string class,
which is very common in WTL and the Mfc libraries as well.
If you're familiar with those just going to grab the address and then call
the C d L G item text and there's the I D C address.
That's the idea of the edit control for the address
that I've created in the dialog box.
Just paste in that text.
And essentially we're done.
So let's test just this little piece and see what that looks like with VM map.
So I'm going to press control of five to build that
and run that while we're doing that.
So now that we've run this,
let's minimize and run a VM app So VMs requests a process obviously,
and that's why it was important to run our application first.
And so that's the mini Excel process here.
I'm going to select that and it shows the various species of data and other
stuff that is part of the process such as the images,
the heaps and so on.
So for our purpose because we're using mutual dialogue,
that's going to be in the yellow part here showing just allocations that
have no particular purpose that VMs is able to guess.
So if we now go back to our application,
let's see what happens at this point,
we're going to click the allocated button and we get some
successful location in this address.
So this address is not used just yet, you will need to refresh VM app,
let's do that.
So here's refresh and you notice this address now comes up,
that's this particular chunk,
notices 256 megabytes in size and it is already committed.
So it's already committed because we have committed it up front,
which means that accessing this memory should definitely succeed.
So let's try a different approach just to see what
that looks like here in VM app.
Let me close the application now and go back to the code.
Instead of doing ma'am commit.
Let's do a meme reserve only.
I'm going to just use meme reserve here.
That's it.
If we run that once again,
of course we'll have to really select the process because the process I.
D.
Has definitely changed.
So we have a new process here to look at.
I've selected the new instance here and again, I'm going to click allocate now.
We have another address here.
Let me refresh VM app and you see again 256 megabytes size.
However, this time it's come reserved, there's nothing here by the way.
Just remember that these details here saying the
environment block is just a bug in via map.
It really has nothing to do with the third environment block.
So you see here that the memory chunk is reserved.
So it seems that the only difference is reserved versus committed
but we don't still see any effect while actually trying to access
that memory will do that in the next clip.