What happens when you deallocate a pointer twice or more in C++?

11,119

Solution 1

You get undefined behaviour if you try to delete an object through a pointer more than once.

This means that pretty much anything can happen from 'appearing to work' to 'crashing' or something completely random.

Solution 2

It's undefined behavior, so anything can happen.

What's likely to happen is bad. Typically, the free store is a carefully managed system of free and allocated blocks, and new and delete do bookkeeping to keep everything in a consistent state. If you delete again, the system is likely to do the same bookkeeping on invalid data, and suddenly the free store is in an inconsistent state. This is known as "heap corruption".

Once that happens, anything you do with new or delete may have unpredictable results, which can include attempting to write outside the application's memory area, silently corrupting data, erroneously thinking there's no more memory, or double or overlapping allocation. If you're lucky, the program will crash soon, although you'll still have problems figuring out why. If you're unlucky, it will continue to run with bad results.

Solution 3

Aside from the old saw about "undefined behavior" meaning anything could happen from nothing to a gateway to the seventh circle of the inferno opening up in main memory, in practice what will usually happen in most implementations is that the program will continue to run past the deletes, and then mysteriously crash sometime later in some unrelated memory allocation.

Solution 4

You are likely venturing into 'undefined behavior' territory.

On many systems this will cause a crash; for example, on my Linux machine:

*** glibc detected *** ./cctest: double free or corruption (fasttop): 0x0000000000d59900 ***
======= Backtrace: =========
/lib/libc.so.6[0x7f399f4cbdd6]
/lib/libc.so.6(cfree+0x6c)[0x7f399f4d074c]
./cctest[0x400a7a]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f399f474abd]
./cctest[0x400959]

Solution 5

If you're really lucky it will crash. What normally happens is it stores up karma until your CEO is demonstrating the code to your most important new customer when it will corrupt/destroy all of their data.

In checked or debug builds often this kind of thing is caught, but it can go completely undetected and cause havoc later. This is especially profound when multiple threads get involved.

Share:
11,119

Related videos on Youtube

flopex
Author by

flopex

Computer Science student. I know the basics of some languages like: Java,C++,Ruby, and Objective-C. Enjoy solving puzzles and doing math.

Updated on September 30, 2021

Comments

  • flopex
    flopex over 2 years
    int main() {
        Employee *e = new Employee();
    
        delete e;
        delete e;
        ...
        delete e;
        return 0;
    }
    
    • Brian Postow
      Brian Postow about 14 years
      out of cucumber error restart universe.
    • John Dibling
      John Dibling about 14 years
      Why the downvote? Its a simple & straightforward question. Just because you think it's an elementary question doesn't mean it should be d/v'ed.
  • David Thornley
    David Thornley about 14 years
    Yes, and that's one of the better things that can happen.
  • Brian Postow
    Brian Postow about 14 years
    often When I get the mysterious memory crashes, I WISH a gateway to the seventh circle of the inferno had opened up in main memory instead...
  • flopex
    flopex about 14 years
    I've tried it and it doesn't crash. But what I think is that you might deallocate memory that other part of your program is using.
  • TheUndeadFish
    TheUndeadFish about 14 years
    It might not crash at that moment. But if it corrupts part of the heap, then there's a significant chance that a crash could happen at some arbitrary point afterwards. However, it could become something of a lurking time-bomb. Not causing any problem until later when some seemingly unrelated action happens to touch the corrupted part and then boom