Will delete operator throw any exception?

J

Jack Klein

If I am deleting a pointer to an object, excption occurs, what will happen?

There is something wrong with your code. If you are looking at it and
can't see the problem, how do you expect anyone else to find it
without seeing the code?

Most likely possibilities are:

Using delete[] on a pointer allocated with new.

Using delete on a pointer allocated with new[].

Deleting the same pointer more than once.

Deleting a pointer that was never allocated.

Deleting a pointer that was changed after it was allocated.

Writing past the end of allocated memory.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Rolf Magnus

Jack said:
There is something wrong with your code. If you are looking at it and
can't see the problem, how do you expect anyone else to find it
without seeing the code?

I think the OP asked what happens if an exception is thrown during a
delete, probably by the destructor.
Anyway, throwing in a destructor is best avoided.
Most likely possibilities are:

Using delete[] on a pointer allocated with new.

Using delete on a pointer allocated with new[].

Deleting the same pointer more than once.

Deleting a pointer that was never allocated.

Deleting a pointer that was changed after it was allocated.

Writing past the end of allocated memory.

None of those have anything to do with exceptions.
They just invoke undefined behavior (though that might also be throwing
an exception).
 
J

Jack Klein

Jack said:
There is something wrong with your code. If you are looking at it and
can't see the problem, how do you expect anyone else to find it
without seeing the code?

I think the OP asked what happens if an exception is thrown during a
delete, probably by the destructor.
Anyway, throwing in a destructor is best avoided.
Most likely possibilities are:

Using delete[] on a pointer allocated with new.

Using delete on a pointer allocated with new[].

Deleting the same pointer more than once.

Deleting a pointer that was never allocated.

Deleting a pointer that was changed after it was allocated.

Writing past the end of allocated memory.

None of those have anything to do with exceptions.
They just invoke undefined behavior (though that might also be throwing
an exception).

You're right, I misread the question (or misthought). Must still be
dazzled by tonight's fireworks display. Thanks for the correction.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
A

Alexander Terekhov

Klaus Eichner wrote:
[...]
Before 'exception-d' is thrown, you can use the standard library
uncaught_exception() to find out whether context-1 or context-2 applies,

That won't necessarily work. The current uncaught_exception() is
totally brain-damaged. What's really needed is something ala "bool
std::unwinding<T>(T *) throw()" or something like that.

http://groups.google.com/[email protected]
(Subject: Re: Alexandrescu on error handling at ACCU conference 2002)
i.e. uncaught_exception() returns true if an exception has been thrown but
hasn't yet been caught.

Stay away from uncaught_exception(). To the OP: make your dtor
throw()-nothing (add empty ES) and see what will happens. Note that
the upcoming C++ standard is likely to impose the implicit throw()-
nothing ES on ALL dtors. Be aware.

regards,
alexander.
 
C

cai

Rolf Magnus said:
I think the OP asked what happens if an exception is thrown during a
delete, probably by the destructor.
Anyway, throwing in a destructor is best avoided.

Yes, that is what I mean. I am afraid that if delete will throw exception or
not, just like new ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top