1

Possible Duplicate:
Why Free crashes when called twice

I just want to know what exactly happened when we delete the pointer that already been deleted, and what cause the crash?

Community
  • 1
  • 1
Leon
  • 8,151
  • 11
  • 45
  • 51
  • I agree with @Tomalak that this is a duplicate, though I'll also note that if you have a pointer to a `T` and `T` has a destructor, the destructor will get called twice, which could cause all sorts of other issues. – James McNellis Apr 22 '11 at 03:43
  • an example of how the pointer was used would be helpful. – ServAce85 Apr 22 '11 at 03:43
  • If you hit your computer with a hammer and then try to use it, what happens? Same thing here. :P – user541686 Apr 22 '11 at 03:45
  • It doesn't necessarily crash - it causes undefined behaviour. It might do nothing, or it might corrupt memory and cause strange problems later, or (if you're very lucky) it might crash straight away and let you debug the problem. – Mike Seymour Apr 22 '11 at 03:47

3 Answers3

2

It's hard to predict exactly what will happen -- it depends a little on the compiler, and a lot on the standard library. Officially, it's just undefined behavior, so nearly anything can happen.

The most common thing that'll happen is that the heap will get trashed. It may not check that what delete is valid, so when it gets two blocks at the same address it may (for example) still treat them as two separate blocks, so when you allocate memory later you may get that same block of memory twice. In some other cases, it may (for example) just complement a bit to say whether that block of memory is in use, so the second time you delete it, you actually end up marking it as being in use again, so that memory can never be allocated again, and you've basically just created a leak.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

If you buy a car, can you sell it twice? No right, because second time you are no longer the owner of it. When you delete the pointer for the first time , you are relinquishing your ownership and the memory will be going back to the free pool getting ready to be given out again. If you delete it for the second time, that memory might be used by any other program now and when they try to access it it will crash. Hope this is helpful

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
0

It just does.

It's illegal to free memory that you don't own (and if you previously freed a block of memory then you no longer own it).

Because it's illegal to do, the internal algorithms don't waste time double-checking memory ownership every time you try to free something, assuming that you're wise enough to just not do it.

Of course, the side effect of this is that you can mangle your memory and make things go haywire.

Analysis of really specific examples is hugely out of scope of Stack Overflow or, indeed, sanity. This answer to a possible duplicate question does a pretty good job, though.

Why can't free() just check its "records" each time? Because C++ is designed on the principle of not doing stuff that it doesn't need to. If you're a sane programmer, you don't call free() twice on the same block of memory, so why would you want your program to waste time and resources on pointless checks? You wouldn't. The principles of the language (and, indeed, common sense) dictate that these checks are not performed automatically.

Sidenote — I'm being a bit generous with my use of the term "illegal". Within the scope of C++, it's merely "Undefined Behaviour". Within the scope of practicality, that might as well be the same darned thing.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055