• Advertisement
Sign in to follow this  

How does delete[] know how many bytes to release?

This topic is 2265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I read a file from disk to a char* pBuffer in one function, then pass the pBuffer to another function where it is delete[]'d. How does the "delete[] pBuffer" operattion know how many bytes to release? Is there a "end of block" byte at the end or does the OS know?

Share this post


Link to post
Share on other sites
Advertisement
In fact, while you new or malloc a range of memory, there is a header block who will record these information used to release the memory.

Share this post


Link to post
Share on other sites

any differance in new or malloc, besides new->delete and malloc->free() ?

Operator new will call the constructor for an object, whereas malloc will not. You could use malloc and then placement new and get the same effect as if you had just allocated the memory with new (Except you need to free the memory with free() rather than delete, and need to manually call the destructor first).

Operator new is usually implemented as a call to malloc() internally.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement