What is the worst that may happen when you unintentionally double remove?

What is the worst that may happen when you unintentionally double remove?

Does it make a difference? Is the compiler planning to toss a mistake?

8 Responses 8

dating two girls

It produces undefined behavior. Any such thing can occur. Used, a runtime crash might be the thing I’d expect.

Undefined behavior. There are not any guarantees whatsoever made by the conventional. Probably your operating-system makes some guarantees, like “you won’t corrupt another procedure”, but that does not help your own plan definitely.

Their plan could crash. Your computer data might be corrupted. The drive deposit of the subsequent income could instead need 5 million bucks from the accounts.

Its undefined attitude, so that the actual benefit will vary according to compiler & runtime surroundings.

Typically, what does sugar daddy mean the compiler don’t notice. In lot of, if not a lot of, circumstances, the runtime mind administration collection will freeze.

Under the cover, any storage management must maintain some metadata about each block of information they allocates, such that allows it to lookup the metadata through the tip that malloc/new came back. Typically this takes the type of a structure at fixed offset before the allocated block. This structure can include a “magic amounts” — a constant definitely not likely that occurs by pure possibility. In the event the storage supervisor views the miraculous number for the forecasted put, it understands that the pointer supplied to free/delete may perhaps be good. Whether it does not begin to see the magic number, or if they sees a different sort of amounts this means “this pointer was not too long ago freed”, could sometimes quietly overlook the no-cost consult, or it would possibly print a helpful message and abort. Either try legal according to the spec, there include pro/con arguments to either method.

When the memory manager does not keep a magic amounts for the metadata block, or does not if not look at the sanity for the metadata, next any such thing can occur. Dependent on the way the memory manager is actually implemented, the result is more than likely a crash without a helpful information, either right away into the mind manager reasoning, somewhat later the next time the storage supervisor tries to allocate or free memories, or much later and far away whenever two various areas of this system each imagine they’ve got possession of the identical amount of memories.

Let’s test it. Become your code into an entire system in so.cpp:

Compile it (I’m making use of gcc 4.2.1 on OSX 10.6.8, but YMMV):

Lookie truth be told there, the gcc runtime really finds it absolutely was a double remove and it is pretty useful before it crashes.

While this is undefined:

this really is well defined:

Believed i will posting they since no-one else was mentioning they

The compiler may give a caution or something, particularly in obvious (like in your sample) but it’s difficult for this to always discover. (You can utilize something similar to valgrind, which at runtime can identify it though). When it comes to actions, it can be something. Some secure collection might check always, and handle it fine — but different runtimes (for rate) could make the expectation you call are proper (which it’s not) immediately after which freeze or worse. The runtime try allowed to make presumption you aren’t two fold deleting (even if dual deleting should do anything bad, e.g. crashing your desktop)

Anyone already said that you shouldn’t do that and this will cause vague conduct. Definitely widely known, very let us elaborate about this on a lesser levels and let us see what in fact takes place.

Common universal response is that things can occur, that isn’t completely real. Eg, the pc wont try to destroy you for this (unless you will be programming AI for a robot) 🙂

The key reason why there can’t be any worldwide answer is that because this is vague, it would likely vary from compiler to compiler as well as across various models of exact same compiler.

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

erase consist of 2 major functions:

  • it calls the destructor whether it’s defined
  • it in some way frees the mind allotted to the item

Therefore, in case the destructor have any signal that accessibility any facts of class that already got removed, it might segfault otherwise (probably) you’ll study some nonsense information. If these erased facts become suggestions then it will in all probability segfault, because you will make an effort to access storage that contains something else, or does not participate in you.

In the event the constructor does not touching any data or perhaps isn’t present (let us maybe not start thinking about digital destructors right here for simpleness), it may not be a real reason for collision in most compiler implementations. However, calling a destructor isn’t the just operation that is going to result here.

The memory space must be cost-free’d. The way it’s completed depends on implementation in compiler, nevertheless might as well carry out some free like features, giving they the tip and size of the item. Calling no-cost on memories that was already removed may crash, because the memories cannot belong to your any longer. When it really does are part of you, it may not freeze instantly, nonetheless it may overwrite memory space that has been currently allocated for most different item of your program.

This means one or more of the memories buildings just got corrupted plus plan will probably freeze in the course of time or it might behave incredibly weirdly. The reason why will never be evident in your debugger and you will spend weeks determining precisely what the hell merely occurred.

So, as rest have said, it is typically an awful idea, but I suppose you already know that. Don’t get worried though, innocent kitten does not really pass away in the event that you delete an object double.

Is instance laws which completely wrong but may work fine too (it truly does work okay with GCC on linux):

Basically do not make advanced instance of these class between deletes, 2 phone calls to release on same memory space happens needlessly to say:

To answer the questions you have right:

What is the worst which can take place:

In theory, your regimen causes one thing deadly. It would likely actually randomly attempt to wash your own hard disk in a few extreme cases. The probabilities depends on exacltly what the regimen really is (kernel motorist? user space program?).

In practice, it would probably just crash with segfault. But something worse might happen.

Is the compiler browsing put one?

Download Our App

app-store-badge
play-store-badge
Order Online Now

Browse Our Menu

Our Most Popular Dishes

[fusion_products_slider picture_size="auto" cat_slug="most-popular" number_posts="3" carousel_layout="title_below_image" autoplay="no" columns="3" column_spacing="20" scroll_items="" show_nav="no" mouse_scroll="no" show_cats="no" show_price="yes" show_buttons="no" hide_on_mobile="small-visibility,medium-visibility,large-visibility" class="" id="" /]