Here is a simple example to explain the issue (with c++) :
A* a1 = new A;
A* a2 = new A;
A* a3 = new A;
delete(a2);
B* b = new B;
.
.
.
let's say the size of an A
object is 3,
and the size of a B
object is 4,
and the size of my heap is 12,
after deleting a2, the memory will be like this :
XXX---XXX---
I can't create a the object B* b
even if there is enough memory, since it's not contiguous.
Just a simple example of memory fragmentation.
Can I avoid this dynamically by creating some kind of reallocate()
function,
a function that would "move" the memory of object a3
and put it right after a :
XXXXXX------
The function should obviously be called after deleting a2
, so maybe reimplemeting deallocate()
or delete()
can do this, how can I do this please ?
This is just a very simple example to show the kind of problem i am dealing with