Why does calling free () on a pointer allocated with 'new' cause heap corruption?

C++ wants to call a destructor on the object when you use delete but passing it to free doesn't allow this to happen. If the object contained other objects then those objects' destructors would not be called either. If the object had pointers in it then those wouldn't get freed.

The standard says you have to match the allocation/deallocation function perfectly (new-delete, new-delete, malloc-free). Theoretically, it is quite possible that some compilers implement operator new() as a simple malloc(), so it wouldn't cause a crash, but "only" skipping the destructor call (which is bad by itself). However, operator may store the number of elements in the allocated chunk of memory in which case, the address returned by new points inside some block allocated by malloc() (not to the beginning), which means you can't free it with free().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions