delete

C

Christopher

If I call delete on a pointer is it guarenteed to set that pointer to
NULL on all implementations?

I've also seen "delete this" in a reference counting pointer
implementation and am wondering what the effects of that are.
 
V

Victor Bazarov

Christopher said:
If I call delete on a pointer is it guarenteed to set that pointer to
NULL on all implementations?

Of course not. It's not supposed to do that. I know of no compiler
that would actually do that.
I've also seen "delete this" in a reference counting pointer
implementation and am wondering what the effects of that are.

The effects of 'delete' is calling the destructor and freeing the
memory (usually).

V
 
J

Joe Greer

If I call delete on a pointer is it guarenteed to set that pointer to
NULL on all implementations?

Pretty much, its guaranteed not to set the pointer to NULL.
I've also seen "delete this" in a reference counting pointer
implementation and am wondering what the effects of that are.

Yes, delete calls the destructor on the object and returns the memory to
the available memory pool. It doesn't have any magic way to find all the
pointers to the released memory and NULL them though.

joe
 
S

Scott McPhillips [MVP]

Christopher said:
If I call delete on a pointer is it guarenteed to set that pointer to
NULL on all implementations?


Nope. But it would be a real good idea to follow your delete by setting
that pointer to NULL yourself. This will help you catch (mis)use of a
deleted pointer during debugging.
 
P

Pete Becker

Nope. But it would be a real good idea to follow your delete by
setting that pointer to NULL yourself. This will help you catch
(mis)use of a deleted pointer during debugging.

The benefits from doing this are mostly illusory.

~MyType()
{
delete my_member_ptr;
my_member_ptr = 0; // silly
}

void delete_it(T *ptr)
{
delete ptr;
ptr = 0; // silly, and doesn't affect the pointer that was used to call
this function
}

That's not to say that setting pointers to NULL is never useful, but it
should be done as part of an overall design, not as a hack that may or
may not help during debugging.
 
P

peter koch

Nope. But it would be a real good idea to follow your delete by setting
that pointer to NULL yourself. This will help you catch (mis)use of a
deleted pointer during debugging.
I don't think so. I could imagine setting the pointer to some
(nonportable) value, that when accessed would generate a hardware
exception could be a good idea when debugging, but setting it to a
"legal" value should be done only in cases where it really is the
right decision.

/Peter
 
M

ManicQin

The benefits from doing this are mostly illusory.
~MyType()
{
delete my_member_ptr;
my_member_ptr = 0; // silly

}

void delete_it(T *ptr)
{
delete ptr;
ptr = 0; // silly, and doesn't affect the pointer that was used to call
this function

}

maybe in the dTor it's silly
but beside debug related issues, the code
if (NULL == ptr)
is quite common... IMHO it's always better to nullify the ptr after
deleting him
 
R

Ron Natalie

peter said:
I don't think so. I could imagine setting the pointer to some
(nonportable) value, that when accessed would generate a hardware
exception could be a good idea when debugging, but setting it to a
"legal" value should be done only in cases where it really is the
right decision.
Nothing says that the pointer passed to delete is the only copy of
that value in the program (the argument to delete isn't even necessarily
an l-value).

There are debug systems that do try to poison the memory to detect
future references (they do this by usually by not reissuing the same
memory). The C language folk envisioned the ability to also poison
the pointer value (although I know of no system that does this) which
is using an previously deleted pointer value is undefined behavior even
if you don't dereference it.
 
P

Pete Becker

maybe in the dTor it's silly
but beside debug related issues, the code
if (NULL == ptr)
is quite common... IMHO it's always better to nullify the ptr after
deleting him

As I said in the part you snipped, "That's not to say that setting
pointers to NULL is never useful, but it should be done as part of an
overall design..."
 
T

terminator

If I call delete on a pointer is it guarenteed to set that pointer to
NULL on all implementations?

I thought that pointers are normally passed by value to 'delete',So I
doubt that any delete operator sets its operand to NULL.
I've also seen "delete this" in a reference counting pointer
implementation and am wondering what the effects of that are.

I do not code that way.

regards,
FM.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top