What is the worst that will take place as soon as you unintentionally two fold delete?

What is the worst that will take place as soon as you unintentionally two fold delete?

Does it make a difference? Could be the compiler planning toss a mistake?

8 Responses 8

all adult dating sites

It leads to undefined actions. Things can happen. Used, a runtime accident is most likely everything I’d expect.

Undefined attitude. There are no guarantees at all made by the conventional. Probably their os helps make some ensures, like “you won’t corrupt another processes”, but that doesn’t let your program considerably.

Your regimen could freeze. Your data could be corrupted. The immediate deposit of your subsequent paycheck could instead just take 5 million bucks out of your levels.

It really is undefined behavior, therefore, the genuine outcome vary depending on the compiler & runtime environment.

Normally, the compiler wont notice. A number of, if you don’t a lot of, circumstances, the runtime storage administration library will crash.

Within the bonnet, any memory space supervisor has to preserve some metadata about each block of information they allocates, in a manner that allows it to look up the metadata from the tip that malloc/new returned. Usually this requires the form of a structure at fixed offset ahead of the allocated block. This structure can consist of a “magic quantity” — a consistent this is certainly unlikely to happen by pure potential. When the memory supervisor views the secret quantity in the forecasted location, it understands that the pointer supplied to free/delete is most likely appropriate. Whether or not it does not notice miraculous amounts, or if it sees an alternative quantity this means “this pointer had been recently freed”, it would possibly sometimes quietly ignore the free request, or it may print a helpful content and abort. Either try appropriate under the spec, and there were pro/con arguments to either strategy.

If memories manager doesn’t hold a magic wide variety during the metadata block, or does not usually check the sanity with the metadata, then nothing can happen. Depending on the memory space management are implemented, the result is more than likely a crash without a helpful content, either instantly inside mind management reason, rather after next time the memory space management tries to set aside or free of charge memory space, or a lot after and far off when two various parts of this system each believe obtained ownership of the identical amount of mind.

Let’s give it a try. Become the code into a total regimen in Spiritual dating login 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 in fact finds it was a double erase and is also pretty beneficial before it crashes.

While this is undefined:

this might be well-defined:

Considered i ought to post they since no one more was pointing out it

The compiler may give a warning or something, especially in evident (like inside example) but it’s impossible for this to constantly discover. (you need something similar to valgrind, which at runtime can detect they though). When it comes to habits, it may be anything. Some secure collection might check, and take care of it okay — but additional runtimes (for speeds) will always make the assumption your phone are appropriate (that it’s perhaps not) following freeze or worse. The runtime is permitted to result in the expectation you are not dual deleting (even if double deleting should do things terrible, e.g. crashing up your computer system)

Every person already said that you should not repeat this and this may cause vague actions. Definitely well known, so let’s elaborate on this on a lesser stage and let’s see what really occurs.

Common worldwide response is that things can occur, that is not totally correct. As an example, the pc will likely not make an effort to kill you for doing this (unless you’re programming AI for a robot) 🙂

Exactly why there can’t be any worldwide response is that because this is vague, it might change from compiler to compiler and even across various variations of exact same compiler.

But and this is what “roughly” takes place in most cases:

remove contain 2 main operations:

  • they phone calls the destructor if it is explained
  • they for some reason frees the memory space allocated to the thing

Therefore, in the event your destructor contains any laws that accessibility any data of lessons that currently got removed, it may segfault otherwise (likely) you certainly will browse some nonsense data. If these removed data become pointers this may be will likely segfault, as you will attempt to access mind which has something different, or doesn’t are part of you.

Whether your constructor doesn’t touching any information or isn’t existing (let us maybe not think about digital destructors right here for efficiency), it might not end up being a reason for collision generally in most compiler implementations. But phoning a destructor isn’t the sole procedure that will take place here.

The memories has to be free of charge’d. How it’s finished varies according to execution in compiler, nevertheless might as well execute some complimentary like function, giving it the tip and measurements of your item. Contacting cost-free on storage which was already removed may freeze, considering that the memory space may well not belong to you anymore. If it do belong to your, may possibly not freeze right away, it may overwrite memory space that was currently allocated for a few different object of regimen.

It means several of mind frameworks just adopted corrupted plus program will likely crash at some point or it could behave extremely weirdly. The reasons won’t be apparent inside debugger and you’ll spend days determining precisely what the hell only took place.

Very, as people said, it is generally an awful idea, but perhaps you are already aware that. Don’t be concerned though, simple kitten does not really die should you decide erase an object double.

Listed here is example rule this is certainly incorrect but may work perfectly also (it works OK with GCC on linux):

Basically never build advanced instance of that class between deletes, 2 calls to free on same storage occurs as you expected:

To answer the questions you have immediately:

What’s the worst that can happen:

Theoretically, the program trigger something deadly. It may even randomly try to wipe their hard disk in some extreme situations. The probabilities is dependent upon what your system really is (kernel drivers? user area plan?).

In practice, it could likely simply freeze with segfault. But something even worse might occur.

Could be the compiler likely to place an error?

Deixe um comentário