Deleting a pointer

Discussion in 'C++' started by CplusplusNewbie, Jun 26, 2009.

  1. I wonder why the authors of C++ didn't write the delete function so
    that, for example, the code delete p; stands for

    delete p; p = 0;

    That way, there would be no problem if pointers were deleted twice.

    Is there ever a situation where it would be problematic to set a
    pointer to 0 after deletion?

    Thank you.
     
    CplusplusNewbie, Jun 26, 2009
    #1
    1. Advertising

  2. CplusplusNewbie

    Ron Guest

    On Jun 26, 7:56 am, CplusplusNewbie <> wrote:
    > I wonder why the authors of C++ didn't write the delete function so
    > that, for example, the code    delete p;   stands for
    >
    > delete p;   p = 0;
    >
    > That way, there would be no problem if pointers were deleted twice.
    >
    > Is there ever a situation where it would be problematic to set a
    > pointer to 0 after deletion?
    >


    Delete takes an rvalue for an operand. It's not generally possible
    for it to modify it.

    In actuality, this does hardly much good. Inadvertant twice deletion
    is but yet one of
    the problems in pointer management. What if that pointer is stored
    multiple places?
    What happens if someone still dereferences p after it is set to zero?

    This only solves one little tiny problem.

    Generally, well designed C++ programs don't have need for handling
    pointers to dynamic memory directly.
    People who write a lot of programs that way are "C" thinking.
     
    Ron, Jun 26, 2009
    #2
    1. Advertising

  3. On 26 juin, 13:56, CplusplusNewbie <> wrote:
    > I wonder why the authors of C++ didn't write the delete function so
    > that, for example, the code    delete p;   stands for
    >
    > delete p;   p = 0;
    >
    > That way, there would be no problem if pointers were deleted twice.


    Except the pointer may not be meant to be deleted twice and if that
    happen, it is interesting to know about it. The issue is rather that
    delete may be called twice but raise no error. That makes nasty bug to
    locate.

    > Is there ever a situation where it would be problematic to set a
    > pointer to 0 after deletion?


    None. It may even be desirable if the pointer may be initialised/
    destroyed in multiple places (or in multiple thread/call).
    Or to raise an error if the pointer is deferenced after being
    destroyed.

    delete of a NULL pointer is safe.

    --
    Michael
     
    Michael Doubez, Jun 26, 2009
    #3
  4. CplusplusNewbie

    James Kanze Guest

    On Jun 26, 1:56 pm, CplusplusNewbie <> wrote:
    > I wonder why the authors of C++ didn't write the delete
    > function so that, for example, the code delete p; stands
    > for


    > delete p; p = 0;


    In the few cases where this buys you something, it's simple to
    do yourself. Since delete doesn't require an lvalue, it can't
    be required from a simple delete expression, and most of the
    time, it doesn't buy you anything anyway.

    > That way, there would be no problem if pointers were deleted
    > twice.


    Nonsense.

    > Is there ever a situation where it would be problematic to set
    > a pointer to 0 after deletion?


    No, since any use of the pointer would be undefined behavior
    anyway. On the other hand, there aren't many situations where
    it would be an advantage, either.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 26, 2009
    #4
  5. On Jun 26, 6:56 am, CplusplusNewbie <> wrote:
    > I wonder why the authors of C++ didn't write the delete function so
    > that, for example, the code    delete p;   stands for
    >
    > delete p;   p = 0;
    >
    > That way, there would be no problem if pointers were deleted twice.
    >
    > Is there ever a situation where it would be problematic to set a
    > pointer to 0 after deletion?
    >
    > Thank you.


    FAQ:

    http://www.research.att.com/~bs/bs_faq2.html#delete-zero

    Also, delete is not a "function".

    - Anand
     
    Anand Hariharan, Jun 26, 2009
    #5
  6. CplusplusNewbie

    Rolf Magnus Guest

    CplusplusNewbie wrote:

    > I wonder why the authors of C++ didn't write the delete function so
    > that, for example, the code delete p; stands for
    >
    > delete p; p = 0;
    >
    > That way, there would be no problem if pointers were deleted twice.


    Deleting a pointer twice is the problem. It means there is a logical error
    in the code, so setting p to 0 would rather hide that error instead of
    removing it.

    > Is there ever a situation where it would be problematic to set a
    > pointer to 0 after deletion?


    I would say just the double deletion scenario is. If the pointer is set to
    0, the second deletion attempt will simply do nothing. If the pointer isn't
    set to 0, you usually get a crash and a backtrace that will help you locate
    the source of the double-deletion.
     
    Rolf Magnus, Jun 27, 2009
    #6
  7. Rolf Magnus wrote:
    > Deleting a pointer twice is the problem. It means there is a logical error
    > in the code, so setting p to 0 would rather hide that error instead of
    > removing it.


    I'm not sure I fully agree with that. Consider, for example, a smart
    pointer class which can point to null or to some allocated object. When
    the smart pointer decides that it's ok the delete the object it can
    simply perform a 'delete' without worrying whether it's deleting a null
    pointer or an actual object.

    One could ask "ok, but where's the *double* deletion?" Fair question,
    but the situation may well arise if this smart pointer of yours supports
    deleting the object before the smart pointer instance goes out of scope.
    In that case there will be two deletions: When it's explicitly asked,
    and when it falls out of scope. In the latter case situation it will
    delete "again", but it's not a symptom of an error.
     
    Juha Nieminen, Jun 27, 2009
    #7
  8. CplusplusNewbie

    Lie Ryan Guest

    Juha Nieminen wrote:
    > Rolf Magnus wrote:
    >> Deleting a pointer twice is the problem. It means there is a logical error
    >> in the code, so setting p to 0 would rather hide that error instead of
    >> removing it.

    >
    > I'm not sure I fully agree with that. Consider, for example, a smart
    > pointer class which can point to null or to some allocated object. When
    > the smart pointer decides that it's ok the delete the object it can
    > simply perform a 'delete' without worrying whether it's deleting a null
    > pointer or an actual object.
    >
    > One could ask "ok, but where's the *double* deletion?" Fair question,
    > but the situation may well arise if this smart pointer of yours supports
    > deleting the object before the smart pointer instance goes out of scope.
    > In that case there will be two deletions: When it's explicitly asked,
    > and when it falls out of scope. In the latter case situation it will
    > delete "again", but it's not a symptom of an error.


    If such smartness is needed, then perhaps it should be encapsulated with
    a class and there is a .delete() method that ensures double deletion
    doesn't happen.
     
    Lie Ryan, Jun 27, 2009
    #8
  9. CplusplusNewbie

    James Kanze Guest

    On Jun 29, 12:06 pm, Hendrik Schober <> wrote:
    > CplusplusNewbie wrote:
    > > I wonder why the authors of C++ didn't write the delete function so
    > > that, for example, the code delete p; stands for


    > > delete p; p = 0;


    > > That way, there would be no problem if pointers were deleted twice.


    > > Is there ever a situation where it would be problematic to set a
    > > pointer to 0 after deletion?


    > Why would you invoke 'delete' yourself?


    Because the program logic has determined that the logical
    lifetime of the object has ended.

    > In the last ten years, I've rarely written any 'delete' in
    > my code. When I did (count it on your fingers), it was in
    > the dtor of a class that owned the pointer, so it made no
    > sense to zero it.


    Most of the delete's at the application level in my code are
    "delete this". And you obviously can't assign 0 to this.
    Another frequent case is the one you describe, where the delete
    is in the destructor. And there are cases where the pointer is
    in a map or something similar---something like:
    T* tmp = someMap[ key ] ;
    someMap.erase( key ) ;
    delete tmp ;
    Since the actual pointer goes immediately out of scope, there's
    no point in setting it to zero either. (On the other hand, if
    you don't remove the entry from the map, there's likely to be
    other problems down the line: either a dangling pointer or a
    memory leak.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 30, 2009
    #9
  10. CplusplusNewbie wrote:
    > I wonder why the authors of C++ didn't write the delete function so
    > that, for example, the code delete p; stands for
    >
    > delete p; p = 0;
    >
    > That way, there would be no problem if pointers were deleted twice.


    Pointers are not deleted by 'delete'. What's deleted is the object the
    pointer is pointing to. And in 99 cases out of 100 problems with
    "multiple deletion" take place when the deletion is attempted through
    two (or more) _different_ pointers pointing to the same object.
    Obviously, setting pointer to 0 after 'delete' would not solve anything
    at all, contrary to your naive "there would be no problem" claim.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Sep 9, 2009
    #10
  11. Andrey Tarasevich wrote:

    > CplusplusNewbie wrote:
    >> I wonder why the authors of C++ didn't write the delete function so
    >> that, for example, the code delete p; stands for
    >>
    >> delete p; p = 0;
    >>
    >> That way, there would be no problem if pointers were deleted twice.

    >
    > Pointers are not deleted by 'delete'. What's deleted is the object the
    > pointer is pointing to. And in 99 cases out of 100 problems with
    > "multiple deletion" take place when the deletion is attempted through
    > two (or more) _different_ pointers pointing to the same object.
    > Obviously, setting pointer to 0 after 'delete' would not solve anything
    > at all, contrary to your naive "there would be no problem" claim.
    >
    > --
    > Best regards,
    > Andrey Tarasevich


    The standard guarantees deleting a NULL pointer is same as doing nothing.
    (ISO/IEC 14882 Section 5.3.2)
     
    Michael Tsang, Sep 9, 2009
    #11
  12. In message <h88e4v$k60$-september.org>, Michael Tsang
    <> writes
    >Andrey Tarasevich wrote:
    >
    >> CplusplusNewbie wrote:
    >>> I wonder why the authors of C++ didn't write the delete function so
    >>> that, for example, the code delete p; stands for
    >>>
    >>> delete p; p = 0;
    >>>
    >>> That way, there would be no problem if pointers were deleted twice.

    >>
    >> Pointers are not deleted by 'delete'. What's deleted is the object the
    >> pointer is pointing to. And in 99 cases out of 100 problems with
    >> "multiple deletion" take place when the deletion is attempted through
    >> two (or more) _different_ pointers pointing to the same object.
    >> Obviously, setting pointer to 0 after 'delete' would not solve anything
    >> at all, contrary to your naive "there would be no problem" claim.
    >>

    >
    >The standard guarantees deleting a NULL pointer is same as doing nothing.
    >(ISO/IEC 14882 Section 5.3.2)


    Which would be fine if that were what you were doing.

    What does it say about deleting a copy of the pointer?

    --
    Richard Herring
     
    Richard Herring, Sep 9, 2009
    #12
    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. Replies:
    10
    Views:
    741
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    875
    Jordan Abel
    Mar 28, 2006
  3. Harry Barker
    Replies:
    2
    Views:
    544
    Alf P. Steinbach
    Apr 19, 2006
  4. Replies:
    4
    Views:
    1,329
    Fred Zwarts
    Jul 2, 2009
  5. crea
    Replies:
    2
    Views:
    441
    Nobody
    Dec 28, 2012
Loading...

Share This Page