Topic: Auto-nullifiying delete.


Author: kuehl@ramsen.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 2000/10/10
Raw View
Hi,
Ken Walter (kgw-zamboni-news@stiscan.com) wrote:
: This pointer could be the result of a function call
: and reside in a register.  What does delete nullify?

Actually, this is the key question showing that a nullifying delete
gives a false sense of safety anyway:

  T* p1 = new T;
  T* p2 = p1;
  delete p1;
  if (p2 != 0)
    std::cout << *p2 << "\n";

Just because one pointer is set to null, others still have to be
maintained. In addition, if you still want a nullifying release of
pointer, just implement it:

  template <typename T>
  void nullifying_delete(T*& ptr) {
    delete ptr;
    ptr = 0;
  }

Next, use 'nullifying_delete(p)' rather than 'delete p'.
--
<mailto:dietmar_kuehl@yahoo.de>
<http://www.fmi.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Balog Pal" <pasa@lib.hu>
Date: 2000/10/10
Raw View
"David R Tribble" <david@tribble.com> wrote

>A good optimizing compiler would omit the nullification of pointers
that were immediately reassigned or that go out of scope.

Better say any compiler calling itself optimizing. :-)

>OTOH, we may want the pointer to remain unchanged if the object isn't
completely destructed.

That is an interesting idea. The thread about dttors should never throw the
main argument was you can't handle a ghost object, likely not even detect
is.
With nullifying delete that not nullifies if throw happened there's better
chance for recovery. Not if I'd advocate throwing from dtor.

Paul


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: David R Tribble <david@tribble.com>
Date: 2000/10/08
Raw View
[was: Re: NULL "this" pointers:]

Andrei Alexandrescu wrote:
>> The thing with delete nullifying its argument is that it promises
>> more that it can actually do...

David R Tribble <david@tribble.com> wrote
>> Such as?  "Delete the object pointed to by a pointer, then invalidate
>> the pointer."  What more should it do?

J=F6rg Barfurth wrote:
> Why should a language primitive do multiple things in this way. If I
> need an object deleted, I say 'delete', if I need a visibly invalid
> pointer, I set it to null.

>[...]
> If I wish to reuse a pointer after deleting the object pointed to, I
> have to manually nullify it

But what if you forget to set the pointer to null?  This is the crux
of the whole argument.

> ... (unless I reassign it immediately, in which case I don't want to
> pay for a nullification).

A good optimizing compiler would omit the nullification of pointers
that were immediately reassigned or that go out of scope.

> If the pointer doesn't stay around, why should anyone nullify it.

Exactly my point about optimization.

> (BTW: If the dtor throws, should the pointer still be nullified ?)

Probably.  This is not difficult if we mandate that the pointer object
is set to null just prior to calling the object's destructor.

OTOH, we may want the pointer to remain unchanged if the object isn't
completely destructed.

> The C++ language (this is inherited from C) does not ensure that
> non-null pointers point to valid objects only.

No, but they do guarantee that null pointers don't point to any valid
objects.  A pointer whose object has just been deleted doesn't point
to any valid object, so it should be set to null.

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: John Kewley <kewley@cscs.ch>
Date: 2000/10/09
Raw View
David R Tribble wrote:
>
> [was: Re: NULL "this" pointers:]
> > The C++ language (this is inherited from C) does not ensure that
> > non-null pointers point to valid objects only.
>
> No, but they do guarantee that null pointers don't point to any valid
> objects.  A pointer whose object has just been deleted doesn't point
> to any valid object, so it should be set to null.

To reword slightly:

(1) C++ language guarantees that null pointers do not point to valid objects
(2) A just-deleted pointer doesn't point to a valid object
therefore
(3) just-deleted pointers should point to null

This sounds a but like the old argument:

(1) All Socrates' friends are philosophers
(2) I am a philosopher
therefore
(3) I should be one of Socrate's friends

:-)

OK, so the argument isn't quite the same, but you see what I mean

Cheers,

JK

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: kgw-zamboni-news@stiscan.com (Ken Walter)
Date: 2000/10/09
Raw View
On Sun, 9 Oct 2000 13:06:23, John Kewley <kewley@cscs.ch> wrote:

#David R Tribble wrote:
#>
#> [was: Re: NULL "this" pointers:]
#> > The C++ language (this is inherited from C) does not ensure that
#> > non-null pointers point to valid objects only.
#>
#> No, but they do guarantee that null pointers don't point to any valid
#> objects.  A pointer whose object has just been deleted doesn't point
#> to any valid object, so it should be set to null.
#
#To reword slightly:
#
#(1) C++ language guarantees that null pointers do not point to valid objects
#(2) A just-deleted pointer doesn't point to a valid object
#therefore
#(3) just-deleted pointers should point to null
#
#This sounds a but like the old argument:
#
#(1) All Socrates' friends are philosophers
#(2) I am a philosopher
#therefore
#(3) I should be one of Socrate's friends
#
#:-)
#
#OK, so the argument isn't quite the same, but you see what I mean
#
#Cheers,
#
#JK
#

As defined, delete takes a pointer, not a pointer to a pointer.
This pointer could be the result of a function call
and reside in a register.  What does delete nullify?
If a const pointer is the argument to delete, should delete
violate constness by changing the pointer?
How about if the pointer is volitile?




Ken Walter

Remove -zamboni to reply
All the above is hearsay and the opinion of no one in particular

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]