Hello

I know this is not good code, but i was wondering why dubble delete gives an error (not an exception!!) and dubble free doesn't??
If new/delete works like malloc/free. Is the error caused by the destructor?? and if so why???

Integer *m = (Integer*)malloc(sizeof(*m));
    free(m);
    free(m);

   Integer *m = new Integer();
    delete m;
    delete m;

thx

>I know this is not good code, but i was wondering
They're both undefined, so your question can't be answered. Undefined means anything can happen, from nothing at all to the increased spin rate of any proton related to the change in state that is effected by your use of undefined behavior, which according to quantum theory is every proton in existence, thus causing the immediate heat death of the universe.

>dubble
Dubble isn't a word. If you didn't know that then now you do. If you did know that then shame on you for butchering an already brittle language.

super typo

double is the word


sorry :mrgreen:

The reason that double delete gives an error is not that inside the delete there will be some kind of check??
Just for extra safety???
(double free gives the possibility of an exploit)

>there will be some kind of check??
No, you're wrong. Even if you're right, you're still wrong because every implementation can do whatever it wants in the case of undefined behavior. So one implementation's behavior is not representative of another implementation's behavior.

For example, any time I free a pointer twice, there's a segmentation fault due to corruption of the memory manager. Any time I delete a pointer twice, there's a segmentation fault due to the destructor being called on memory that was not properly constructed or corruption of the memory manager or both. I consider myself lucky that the program crashes and dies almost immediately because I've had to deal with memory errors before where the symptom is very far down the road from the cause.

>Just for extra safety???
There is no extra safety check, ever. C++ is a very efficient language and the standard libraries typically assume that you know what you're doing and don't try to double check you. So neither free nor delete will attempt checking for extra safety, and if they do then they probably don't do it extensively enough to be of benefit, for performance reasons.

>(double free gives the possibility of an exploit)
Why are you so prejudiced against free? ;) delete has all of the same problems, the only differences are that it calls destructors first and you have the added problem of using the wrong type of delete.

Ok thanks but i heard on other forum that in c++ there could be an extra check.
Soo i just asked it to an expert ;)

thx

>i heard on other forum that in c++ there could be an extra check.
Yes, there could be. I wouldn't hold my breath though. Dynamic allocation and deallocation is already a bottleneck for programs. Adding an extra sanity check would only serve to make those operations slower, so most (if not all) vendors don't add it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.