What’s the worst which can occur as soon as you unintentionally double erase?

What’s the worst which can occur as soon as you unintentionally double erase?

Will it matter? Could be the compiler likely to put one?

8 Answers 8

norwegian girls dating

It causes undefined conduct. Such a thing sometimes happens. Used, a runtime collision is most likely everything I’d expect.

Vague behavior. There aren’t any assures at all from the regular. Probably the operating system helps make some ensures, like “you won’t corrupt another techniques”, but it doesn’t assist the system quite definitely.

The system could freeze. Your data could possibly be corrupted. The direct deposit of then income could instead take 5 million bucks out of your levels.

Its vague behavior, therefore, the genuine lead may differ with regards to the compiler & runtime environment.

In most cases, the compiler won’t notice. In a lot of, if you don’t most, cases, the runtime memories administration library will crash.

According to the bonnet, any memories management has got to preserve some metadata about each block of information it allocates, such that allows it to research the metadata through the tip that malloc/new returned. Usually this requires the type of a structure at fixed offset prior to the allocated block. This framework can consist of a “magic quantity” — a consistent which extremely unlikely to occur by pure odds. In the event the memories supervisor views the magic amounts inside forecasted room, it understands that the pointer provided to free/delete is probably appropriate. If this does not understand miraculous numbers, or if it sees a different numbers which means “this tip was recently freed”, it can often silently overlook the complimentary demand, or could reproduce a helpful message and abort. Either is appropriate underneath the spec, there are pro/con arguments to either approach.


In the event the memory space supervisor doesn’t hold a miracle numbers inside metadata block, or does not or else check the sanity of metadata, subsequently everything sometimes happens. Dependent on how the mind management try applied, as a result, almost certainly a collision without a beneficial content, either instantly from inside the mind manager logic, significantly later on the very next time the storage manager attempts to designate or complimentary mind, or a lot afterwards and far when two various areas of this system each consider they will have possession of the same chunk of memory space.

Let us test it. Change the laws into a whole system in so.cpp:

Compile it (i am making use of gcc 4.2.1 on OSX 10.6.8, but YMMV):

Lookie truth be told there, the gcc runtime actually finds that it was a two fold erase and is pretty helpful before it crashes.

While this is undefined:

this is well defined:

Considered i ought to send it since no body more got discussing it

The compiler can provide a warning or something like that, especially in clear (like in your sample) but it is extremely hard because of it to usually recognize. (you can make use of something like valgrind, which at runtime can detect they though). As for the actions, it could be such a thing. Some safer collection might see, and take care of it good — but other runtimes (for rate) can make the presumption you call is actually correct (which it’s not) right after which crash or bad. The runtime try allowed to result in the assumption you aren’t dual deleting (although double deleting should do some thing bad, e.g. crashing your desktop)

Everybody else currently said that you shouldn’t repeat this and this will cause vague conduct. That is well known, very let us elaborate with this on a lesser amount and why don’t we see what actually occurs.

Regular universal response is that things can occur, that isn’t completely true. As an example, the pc cannot attempt to eliminate you for doing this (unless you will be programming AI for a robot) 🙂

Why there cannot be any universal answer is that as this is undefined, it may differ from compiler to compiler and also across different models of exact same compiler.

But this is what “roughly” happens in many cases:

delete consist of 2 primary operations:

  • they phone calls the destructor if it is identified
  • it somehow frees the memory assigned to the thing

So, in the event the destructor includes any code that access any information of class that currently was actually deleted, it might segfault OR (most likely) you may see some nonsense information. If these deleted data include tips this may be will most likely segfault, since you will try to access memory space which contains something else, or doesn’t are part of you.

If your constructor does not touch any data or isn’t current (why don’t we perhaps not think about virtual destructors right here for efficiency), may possibly not feel a real reason for accident in many compiler implementations. But calling a destructor is not necessarily the best process which will occur here.

The memory space needs to be cost-free’d. The way it’s finished depends on execution in compiler, nevertheless might as well execute some complimentary like function, giving they the tip and measurements of the object. Contacting cost-free on storage that was currently erased may freeze, since the mind cannot fit in with you anymore. When it do are part of your, it might not crash right away, it may overwrite memory that has been currently allocated for a few various object of the plan.

That implies several of storage tissues just adopted corrupted along with your program will probably crash sooner or later or this may react very weirdly. The causes won’t be evident in your debugger and you will probably spend weeks figuring out precisely what the hell only taken place.

Very, as rest have said, it is usually an awful idea, but I suppose you know that. Don’t get worried though, simple kitten will most likely not pass away in the event that you erase an object two times.

Listed here is sample code that’s completely wrong but may operate fine and (it functions OK with GCC on linux):

If I never develop advanced case of these lessons between deletes, 2 telephone calls to complimentary in exact same memory space occurs as expected:

To answer the questions you have immediately:

What’s the worst that will occur:

The theory is that, your system causes one thing fatal. It would likely actually arbitrarily attempt to rub their hard disk in some extreme cases. The probabilities relies on exacltly what the plan happens to be (kernel motorist? consumer area system?).

Used, it could probably only freeze with segfault. But something notably worse might happen.

Will be the compiler gonna throw a mistake?

Leave a Reply

Your email address will not be published. Required fields are marked *