How do C++ recognize the exact memory space when using DELETE?

I know C++ offers a powerful tool to dynamically assign and destroy memory space. But I'm curious how it works. For example, for the code:

1
2
3
4
int *a,*b;
a = new int[100];
b = a;
delete [] b;

Are the 100 units all destroyed? How did C++ know there are 100 units?
Last edited on
hi,
all 100 units are destroyed cos you have use delete[] and not delete
so when you use delete[] compiler will know how many units are there from your declaration a = new int[100]; otherwise if you would use delete only compiler wouldn't check for how many unitst there realy are, just delete the pointer. ( maby I'm wrong but thats why you cant resize the array, value must be constant)
so it knows from your array declaration.

codekiddy (82) Nov 9, 2011 at 3:29pm
hi,
all 100 units are destroyed cos you have use delete[] and not delete
so when you use delete[] compiler will know how many units are there from your declaration a = new int[100]; otherwise if you would use delete only compiler wouldn't check for how many unitst there realy are, just delete the pointer. ( maby I'm wrong but thats why you cant resize the array, value must be constant)
so it knows from your array declaration.


So it means that when put a pointer on a assigned memory space, the information of not only the address of the first unit but also the total number of units of the assigned memory space are stored in it? But how is that possible? If I remember it right, a pointer is simply a address recorder...
Well if I remember correctly the new keyword only requests memory from the Operating System. So when you say:

a = new int[100];

your program will request some memory from the OS and the OS will reserve and protect that memory for your program only so other programs can't jump in and start modifying the values. However there is also a chance that the OS can't allocate anymore memory probably because other programs are using it and there's no more left. That's why it's a good idea to check for null or 0 after requesting for memory.

When you call delete [] your program just tells the OS "ok I don't need the memory anymore" and the OS frees the memory so it can be allocated to other programs that need it.

So in a sense it's the OS that manages all that behind the scenes, and to answer your question the OS just knows how much memory it allocated for that specific variable so it also knows how much it's supposed to free up.
Last edited on
But how is that possible? If I remember it right, a pointer is simply a address recorder...

It is. That information about the size is stored elsewhere. Memory management always needs to do some bookkeeping, e.g. which sections of memory are already used. One could store the array size just before the address that is returned, for example. But the details on how it's done depend on the specific memory management implementation.
Last edited on
Many thanks~
I learnt a lot.
:-)
Topic archived. No new replies allowed.