Will delete operator throw any exception?

Discussion in 'C++' started by cai, Jul 5, 2003.

  1. cai

    cai Guest

    If I am deleting a pointer to an object, excption occurs, what will happen?
    cai, Jul 5, 2003
    #1
    1. Advertising

  2. cai

    Jack Klein Guest

    On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <>
    wrote in comp.lang.c++:

    > 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
    Jack Klein, Jul 5, 2003
    #2
    1. Advertising

  3. cai

    Rolf Magnus Guest

    Jack Klein wrote:

    > On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <>
    > wrote in comp.lang.c++:
    >
    >> 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?


    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).
    Rolf Magnus, Jul 5, 2003
    #3
  4. cai

    Jack Klein Guest

    On Sat, 05 Jul 2003 10:31:22 +0200, Rolf Magnus <>
    wrote in comp.lang.c++:

    > Jack Klein wrote:
    >
    > > On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <>
    > > wrote in comp.lang.c++:
    > >
    > >> 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?

    >
    > 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
    Jack Klein, Jul 5, 2003
    #4
  5. 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/groups?selm=
    (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.
    Alexander Terekhov, Jul 5, 2003
    #5
  6. cai

    cai Guest

    "Rolf Magnus" <>
    ??????:be62oa$bs0$01$-online.com...
    > Jack Klein wrote:
    >
    > > On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <>
    > > wrote in comp.lang.c++:
    > >
    > >> 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?

    >
    > 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 ?
    cai, Jul 6, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    12,992
    Kevin Spencer
    Oct 27, 2003
  2. Replies:
    15
    Views:
    7,480
    Roedy Green
    Sep 8, 2005
  3. Pierre Rouleau
    Replies:
    3
    Views:
    6,758
    Siemel Naran
    Mar 4, 2005
  4. siddhu
    Replies:
    5
    Views:
    286
    Ron Natalie
    Jun 14, 2007
  5. Emanuele D'Arrigo

    To throw or to throw not?

    Emanuele D'Arrigo, Nov 14, 2008, in forum: Python
    Replies:
    6
    Views:
    308
    Emanuele D'Arrigo
    Nov 15, 2008
Loading...

Share This Page