For the sake of this question I will picture memory as a simple array of bytes, and I will be talking about heap memory because it is possible to dynamically allocate it.
Lets say that I am instantiating some class, and creating an object on the heap where some memory has already been allocated. Then, after creating the object, I allocate some more memory (maybe by instantiating another class). This implies the use of new
and delete
keywords, of course.
The memory now looks like this:
... byte byte my_object ... my_object byte byte ...
What exactly happens when delete my_object;
is executed? Is all other memory shifted to the left by sizeof(MyClass)
? If so, by who? The OS? Then what happens when there is no OS to provide virtual memory?