Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deleting a pointer

Reply
Thread Tools

Deleting a pointer

 
 
CplusplusNewbie
Guest
Posts: n/a
 
      06-26-2009
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.
 
Reply With Quote
 
 
 
 
Ron
Guest
Posts: n/a
 
      06-26-2009
On Jun 26, 7:56*am, CplusplusNewbie <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      06-26-2009
On 26 juin, 13:56, CplusplusNewbie <(E-Mail Removed)> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2009
On Jun 26, 1:56 pm, CplusplusNewbie <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Anand Hariharan
Guest
Posts: n/a
 
      06-26-2009
On Jun 26, 6:56*am, CplusplusNewbie <(E-Mail Removed)> 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_f...ml#delete-zero

Also, delete is not a "function".

- Anand
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-27-2009
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.


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-27-2009
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.
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      06-27-2009
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-30-2009
On Jun 29, 12:06 pm, Hendrik Schober <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments