Will delete operator throw any exception?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cai

    Will delete operator throw any exception?

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


  • Jack Klein

    #2
    Re: Will delete operator throw any exception?

    On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <aladdin_sh@asu s.com.cn>
    wrote in comp.lang.c++:
    [color=blue]
    > If I am deleting a pointer to an object, excption occurs, what will happen?[/color]

    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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

    Comment

    • Rolf Magnus

      #3
      Re: Will delete operator throw any exception?

      Jack Klein wrote:
      [color=blue]
      > On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <aladdin_sh@asu s.com.cn>
      > wrote in comp.lang.c++:
      >[color=green]
      >> If I am deleting a pointer to an object, excption occurs, what will
      >> happen?[/color]
      >
      > 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?[/color]

      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.
      [color=blue]
      > 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.[/color]

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

      Comment

      • Jack Klein

        #4
        Re: Will delete operator throw any exception?

        On Sat, 05 Jul 2003 10:31:22 +0200, Rolf Magnus <ramagnus@t-online.de>
        wrote in comp.lang.c++:
        [color=blue]
        > Jack Klein wrote:
        >[color=green]
        > > On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <aladdin_sh@asu s.com.cn>
        > > wrote in comp.lang.c++:
        > >[color=darkred]
        > >> If I am deleting a pointer to an object, excption occurs, what will
        > >> happen?[/color]
        > >
        > > 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?[/color]
        >
        > 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.
        >[color=green]
        > > 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.[/color]
        >
        > None of those have anything to do with exceptions.
        > They just invoke undefined behavior (though that might also be throwing
        > an exception).[/color]

        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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

        Comment

        • Alexander Terekhov

          #5
          Re: Will delete operator throw any exception?


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

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


          (Subject: Re: Alexandrescu on error handling at ACCU conference 2002)
          [color=blue]
          > i.e. uncaught_except ion() returns true if an exception has been thrown but
          > hasn't yet been caught.[/color]

          Stay away from uncaught_except ion(). 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.

          Comment

          • cai

            #6
            Re: Will delete operator throw any exception?


            "Rolf Magnus" <ramagnus@t-online.de>
            ??????:be62oa$b s0$01$3@news.t-online.com...[color=blue]
            > Jack Klein wrote:
            >[color=green]
            > > On Sat, 5 Jul 2003 11:12:09 +0800, "cai" <aladdin_sh@asu s.com.cn>
            > > wrote in comp.lang.c++:
            > >[color=darkred]
            > >> If I am deleting a pointer to an object, excption occurs, what will
            > >> happen?[/color]
            > >
            > > 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?[/color]
            >
            > 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.[/color]

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


            Comment

            Working...