Question:
What if you delete without a new in c++?
Aaron
2012-12-16 19:18:02 UTC
In xcode on my mac, if I ran a program like this:

int i=9;
int* p;
p=&i
delete p;

what would happen? It doesn't have a new for the delete.
Would it mess up my computer's memory?

Also, what if you new without deleting?
Three answers:
Ratchetr
2012-12-16 19:38:29 UTC
Second question first: new without delete.



That's easy. It's a memory leak. If you do it a lot, then eventually your code runs slower, because all the newed up stuff forces older stuff to get paged to disk to make room in physical memory for the new stuff.



Eventually you will consume all the memory your program is allowed to allocate, and new will fail. And you probably didn't gracefully handle the case where new fails, so your code will just crash and burn. What it spits out as a dying gasp error message is rather irrelevant.



Deleting a pointer that wasn't allocated by new is even less predictable. Most anything could happen. The heap management code might be smart enough to recognize that you are freeing memory it didn't allocate. Which might cause it to ignore your delete. Or it might cause it to throw an exception. Or maybe it will just crash and burn as soon as you call it.



C++ is pretty much merciless when it comes to shooting yourself in the foot by doing silly things like freeing a pointer you didn't allocate. It can get in your face right away and tell you you *mucked* up. Or it can ignore it. Or it can do nasty things that cause your code to crash in very random ways 3 days from now.



If you are going to write C++ code, then it is best to learn how to not shoot yourself in the foot. Knowing what happens if you do is.... well, don't ask. Because it could very well do something totally different next time around. You don't want to go there.



ETA: @Robert: Non-aligned and not assigned using malloc are 2 very, *very* different things. What happens if you pass a properly *aligned* pointer that wasn't allocated by malloc? An alignment check is cheap: you do a mod on the address. Not allocated by is much more expensive.



Gotta agree with Oops here: Don't do that. And don't bother asking what if.
oops
2012-12-17 03:25:41 UTC
If you delete without new, you get undefined behavior. The C++ language specification does not define what happens in that case, so it is entirely up to your specific implementation. That is, it is up to your compiler, which may delegate the problem to your operating system, or handle it safely on its own. Regardless of how it is handled, don't do it. Don't write that code.



If you new without delete, you get a memory leak. If you leak too much memory, you run out of it. Then when you request more of it, you get a bad_alloc exception.
Robert
2012-12-17 03:27:26 UTC
On XCode this is the error you get:



malloc: *** error for object 0xbffff55c: Non-aligned pointer being freed

*** set a breakpoint in malloc_error_break to debug



... it is because this memory was not assigned using malloc (nor new), so the delete statement is obsolete.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...