![]() |
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. |
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 |
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. |
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[]
|
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. |
Re: How the compiler differentiates delete[] and delete
Quote:
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. |
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. |
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.
|
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. |
Re: How the compiler differentiates delete[] and delete
Quote:
Quote:
|
| All times are GMT +5.5. The time now is 11:47. |