Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How the compiler differentiates delete[] and delete (http://www.go4expert.com/forums/compiler-differentiates-delete-delete-t899/)

sharmila 16Jun2006 10:41

How the compiler differentiates delete[] and delete
 
Hi all,
To delete an array of objects we use delete[] and to delete an object we use delete.But how the compiler knows that in delete[] how many objects it has to delete?
i.e
if we write as
char *c = new char[10];
To delete this we use delete[].
For char *c = new char;
we use delete.
Then how the compiler differentiates between these two.

Thanks in advance,
Regards,
sharmila.

shabbir 16Jun2006 17:08

Re: How the compiler differentiates delete[] and delete
 
Short answer: Magic.

Long answer: The run-time system stores the number of objects, n, somewhere where it can be retrieved if you only know the pointer, p.

Offtopic comment:
I have added the reference to the question in the Some C-C++ Programming tips thread

sharmila 17Jun2006 16:16

Re: How the compiler differentiates delete[] and delete
 
Hi,
I got the answer in a site.I am giving the link.

http://www.awprofessional.com/articl...30642&seqNum=1

Regards,
sharmila.

shabbir 19Jun2006 15:45

Re: How the compiler differentiates delete[] and delete
 
It does not answer how the compiler knows if its how many element to delete in delete[] but it just says when you should be using delete[]

sharmila 19Jun2006 19:27

Re: How the compiler differentiates delete[] and delete
 
Hi shabbir,
In that link it is menctioned that

Typically, array new will insert information adjacent to the memory allocated for an array that indicates not only the size of the block of storage but also the number of elements in the allocated array. This information is examined and acted upon by array delete when the array is deleted.

The format of this information is probably different from that of the information stored with a block of storage obtained through scalar new. If scalar delete is invoked upon storage allocated by array new, the information about size and element count—which are intended to be interpreted by an array delete—will probably be misinterpreted by the scalar delete, with undefined results. It's also possible that scalar and array allocation employ different memory pools. Use of a scalar deletion to return array storage allocated from the array pool to the scalar pool is likely to end in disaster.

Regards,
sharmila.

shabbir 20Jun2006 12:02

Re: How the compiler differentiates delete[] and delete
 
Quote:

Originally Posted by sharmila
Typically, array new will insert information adjacent to the memory allocated for an array that indicates not only the size of the block of storage but also the number of elements in the allocated array. This information is examined and acted upon by array delete when the array is deleted.

Actually thats not true. Allocate a pointer using
int *i = new int[5];
and then try seeing the memory around the allocated area (I saw that in VC++ 6) and you will not find that data anywhere.

sharmila 20Jun2006 12:56

Re: How the compiler differentiates delete[] and delete
 
Hi shabbir,
We will not get one.B'caz that format is different.They wrote as

The format of this information is probably different from that of the information stored with a block of storage obtained through scalar new.

and also menctioned that

It's also possible that scalar and array allocation employ different memory pools.

shabbir 20Jun2006 15:10

Re: How the compiler differentiates delete[] and delete
 
Yes but when you new and new[10] you can get the memory where the buffer is allocated and that can be watched what ever be the pool be.

sharmila 20Jun2006 19:15

Re: How the compiler differentiates delete[] and delete
 
Hi shabbir,
Yes, we can get the memory where the buffer is allocated.But, with that we are not sure that the compiler is searching for that amount of memory only.When we allocate memory using new it will return the address if it is sucessful else null.But with this we don't know actually how much it is allocating.
In case of char* a = "abcd";compiler is adding one more byte and it is placing '\0' . May be if we asked for allocating 10 bytes but internally it may take 12 bytes and in the last bytes it is storing the info. in different formatt.It is my guess.
So if we check how much memory it is allocated manually? then it will be easy.otherwise Is it possible to allocate memory at a particular address? so that we menction the address and we try to allocate memory to that particular one.

Regards,
sharmila.

shabbir 20Jun2006 19:34

Re: How the compiler differentiates delete[] and delete
 
Quote:

Originally Posted by sharmila
So if we check how much memory it is allocated manually?

Thats what I have done and came to the conclusion that it does not store where ever it allocates the buffer.
Quote:

Originally Posted by sharmila
Is it possible to allocate memory at a particular address?

Yes using the placement new operator.


All times are GMT +5.5. The time now is 05:51.