473,499 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoiding dangling pointers.

I read that you should assign null (0) to all pointers that you call
delete on.

Does that mean:

*p=0(set the value pointed to to 0).

or

p=0(set the address held to zero).

The last one is awkward for none would want a pointer pointing
to address zero(even though it compiles fine, you can´t assign
anything to it).

I find the first still awkward because you are assigning a value to a
place
you no longer use.

Thanks

Rafael
Jun 27 '08 #1
8 2015
On Apr 11, 12:41 pm, Rafael Anschau <rafael.ansc...@gmail.comwrote:
I read that you should assign null (0) to all pointers that you call
delete on.

Does that mean:

*p=0(set the value pointed to to 0).

or

p=0(set the address held to zero).

The last one is awkward for none would want a pointer pointing
to address zero(even though it compiles fine, you can´t assign
anything to it).

I find the first still awkward because you are assigning a value to a
place
you no longer use.

Thanks

Rafael
The latter is pretty standard. You should always check if a pointer is
valid (not pointing to zero) before using it, and be sure to have it
set to zero when it is invalid (after releasing the memory it pointed
to).
Jun 27 '08 #2

"Rafael Anschau" wrote:
I read that you should assign null (0) to all pointers
that you call delete on.
Ummm, no. You read that you should assign NULL (not "null",
whatever that is) to pointers WHICH HAVE BEEN DELETED.
That's not at all the same as what you said.
Does that mean:

*p=0(set the value pointed to to 0).
No. That assigns 0 to the thing being pointed to, not the
pointer. If this is immediately BEFORE calling delete, it's
harmless, but accomplishes nothing. If this is AFTER calling
delete, it's an illegal memory access, and may crash your
program or your OS.
or

p=0(set the address held to zero).
Yes. Do this immediately after delete'ing any pointer.

I prefer "p=NULL;", myself. "NULL" is usually typedefed
in the compiler's headers to "(void*)0" or "0". I use
it because it makes it visually clear that a pointer
is being invalidated.
The last one is awkward for none would want a pointer
pointing to address zero
Why not??? *ALL* pointers should be set to 0 when not
in use! Then before using them, always test them to
make sure they're not NULL:

if (NULL != pAardvark)
{
*pAardvark = 37.94;
}

Otherwise, if pAardvark was never pointed at a valid
lvalue, when you do "*pAardvark = 37.94", you'll likely
crash your program, or even your OS.
(even though it compiles fine, you can´t assign
anything to it).
Of course you can! Like so:

int *PGreg = NULL; // pGreg is set to 0 here
... // some code
pGreg = &Susan; // assign address of Susan to pGreg
... // some code
if (NULL != pGreg) // PLAY IT SAFE!!!!!
{
Frank = Sam + (*pGreg); // dereference pGreg
}
I find the first still awkward because you are assigning
a value to a place you no longer use.
It's not just awkward, it's illegal memory access, aka
"general protection fault", aka "have to push reset button".

How to crash your system in 2 easy steps:
1. delete pGreg; // pGreg no points to who knows where
2. *pGreg = 0; // write to unknown memory location

What the programmer should have done instead:
1. delete pGreg; // pGreg no points to who knows where
2. pGreg = 0; // zero the pointer

But if you always assign 0 or NULL to pointers immediately
after delete'ing them, and test for (NULL != pointer) before
trying to read or write what they point to, that will help
you keep out of trouble.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Jun 27 '08 #3
Rafael Anschau wrote:
I read that you should assign null (0) to all pointers that you call
delete on.
Controversial, at best. I never do that.
Does that mean:

*p=0(set the value pointed to to 0).
This would undefined behavior, as you would be deferencing a pointer to
deleted storage.
or

p=0(set the address held to zero).

The last one is awkward for none would want a pointer pointing
to address zero(even though it compiles fine, you can?t assign
anything to it).
You seem confused. You can't dereference it, but you couldn't before.
You can certainly assign something to p. That is:

delete[] p;
p = 0;
p = new int[20]; // p is int* for this example

Is perfectly fine.
I find the first still awkward because you are assigning a value to
place
you no longer use.
Far more than awkward, it's undefined behavior. You must not do that.


Brian
Jun 27 '08 #4
If you set p=0, you are pointing nowhere, and I found that weird. But
it actualy makes sense, it´s better to have the program crash
right away if something tries to write on it, than to have it write
fine
on some dark zone of heapland which you will never be able to reach
again.
Reduces debugging time.

Thanks folks.
On Apr 11, 4:56 pm, "Default User" <defaultuse...@yahoo.comwrote:
Rafael Anschau wrote:
I read that you should assign null (0) to all pointers that you call
delete on.

Controversial, at best. I never do that.
Does that mean:
*p=0(set the value pointed to to 0).

This would undefined behavior, as you would be deferencing a pointer to
deleted storage.
or
p=0(set the address held to zero).
The last one is awkward for none would want a pointer pointing
to address zero(even though it compiles fine, you can?t assign
anything to it).

You seem confused. You can't dereference it, but you couldn't before.
You can certainly assign something to p. That is:

delete[] p;
p = 0;
p = new int[20]; // p is int* for this example

Is perfectly fine.
I find the first still awkward because you are assigning a value to
place
you no longer use.

Far more than awkward, it's undefined behavior. You must not do that.

Brian


Jun 27 '08 #5
On 11 avr, 19:41, Rafael Anschau <rafael.ansc...@gmail.comwrote:
I read that you should assign null (0) to all pointers that you call
delete on.
I'd suggest you throw out whatever book said something as stupid
as that. I almost never set pointers to null after delete, and
I don't know of any good programmers that do.
Does that mean:
*p=0(set the value pointed to to 0).
After a delete, that would be undefined behavior. Of the worst
kind, since it will almost always work. (The most frequent
result of undefined behavior is for it to work in all of your
tests, and then cause a program crash during the important demo
in front of your most important client.)
or
p=0(set the address held to zero).
The last one is awkward for none would want a pointer pointing
to address zero(even though it compiles fine, you can´t assign
anything to it).
You're not making the pointer point to the address zero here
(despite appearances). You're making it point to nowhere, but
with a testable and copiable value. It's useful in the rare
cases where you have a pointer which sometimes will point to
something, and othertimes won't. When it doesn't point to
something, you set it to null (which you can write NULL or 0).

This is often used in lazy evaluation (the pointer is
initialized to null; at each use, you test for null, and
evalutate it to the usable value if it is null), or for various
caching schemes (in which case, you do set the pointer to null
after a delete, since that's how you recognize that the cache is
invalid). Most deletes, however, occur in destructors, in which
case, there's no sense in setting the pointer to null, since you
can't use it later anyway.
I find the first still awkward because you are assigning a
value to a place you no longer use.
The first is undefined behavior. Don't do it. The second has
some specialized uses, but there's certainly no reason to do it
systematically after a delete.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jun 27 '08 #6
On 11 avr, 21:52, "Robbie Hatley"
<see.my.signat...@for.my.email.addresswrote:
"Rafael Anschau" wrote:
I read that you should assign null (0) to all pointers
that you call delete on.
Ummm, no. You read that you should assign NULL (not "null",
whatever that is) to pointers WHICH HAVE BEEN DELETED.
He probably means a null pointer. A null pointer constant
(any integral constant expression evaluating to 0, but anything
but 0 or NULL would be very strange) will be converted to a null
pointer in pointer contexts.

And of course, there's absolutely no reason to set a pointer to
null after delete, except in a few special cases.
Systematically doing so is generally a sign that the programmer
doesn't really understand what he is doing.
That's not at all the same as what you said.
Does that mean:
*p=0(set the value pointed to to 0).
No. That assigns 0 to the thing being pointed to, not the
pointer. If this is immediately BEFORE calling delete, it's
harmless, but accomplishes nothing.
Actually, of course, in a lot of cases, before or after, it
won't compile. You can't necessarily assign 0 to most types.
If this is AFTER calling delete, it's an illegal memory
access, and may crash your program or your OS.
But probably not during your tests. Only during the demo before
the important client:-).
or
p=0(set the address held to zero).
Yes. Do this immediately after delete'ing any pointer.
Why?
I prefer "p=NULL;", myself. "NULL" is usually typedefed
in the compiler's headers to "(void*)0" or "0".
In C++, it must be a typedef to a "null pointer constant".
Which despite the name, may not have pointer type---it must be
an integral type. "0" is by far the most frequent, although
I've also seen "0L", and something like "(1/2)" would be legal.
"(void*)0" is not legal.
I use it because it makes it visually clear that a pointer is
being invalidated.
I'd say that "delete" makes it pretty clear already. If the
programmer doesn't know that delete invalidates the pointer, I'd
say that that's a problem in itself.
The last one is awkward for none would want a pointer
pointing to address zero
Why not???
Well, you might want it on some embedded systems. But assigning
a constant 0 to a pointer does NOT make it point to the address
zero. It makes it point nowhere.
*ALL* pointers should be set to 0 when not in use!
More to the point, you shouldn't keep pointers not in use
around.
Then before using them, always test them to
make sure they're not NULL:
if (NULL != pAardvark)
{
*pAardvark = 37.94;
}
And what does this buy you (except obfuscation)? Just because a
pointer isn't null doesn't mean that it points to something
valid.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jun 27 '08 #7
On 11 avr, 21:56, "Default User" <defaultuse...@yahoo.comwrote:
Rafael Anschau wrote:
I read that you should assign null (0) to all pointers that
you call delete on.
Controversial, at best. I never do that.
Does that mean:
*p=0(set the value pointed to to 0).
This would undefined behavior, as you would be deferencing a
pointer to deleted storage.
It just occurred to me, but...

How often do you have pointers to a numerical type anyway?
Cases where this would even compile shouldn't be that frequent.

For the rest, I totally agree with you.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jun 27 '08 #8
On 11 huhti, 20:41, Rafael Anschau <rafael.ansc...@gmail.comwrote:
I read that you should assign null (0) to all pointers that you call
delete on.
There is no reason to do that. What you should do is keep track
of created objects. That can be made easier with containers
which own the objects and handle the destruction once the object
is no longer needed.
Or in case of global objects just delete the object in program
exit and that should do the trick.
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3059
by: Aravind | last post by:
I would like to know in what manner dangling pointers affect the security of a application developed using C++.What are the loopholes that are created by dangling pointers and how they could be...
11
1908
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
20
6520
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
5
1957
by: Richard | last post by:
My experience has always been that you're SOL when trying to safely detect and stop references to dangling memory (non-null pointers to free'ed blocks) at runtime (C99, Linux). Maybe somebody...
4
3621
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
3
1796
sicarie
by: sicarie | last post by:
I saw this on Slashdot and thought it was interesting. There's no released POC as far as I know, but i was curious. How do you guys find things like dangling pointers? (Blackbox testing, trusting the...
1
2452
by: sridhard2406 | last post by:
Hi All, I have a doubt on undrestanding Dangling pointers.Below I mentioned sample code. please let me know, my view on Dangling pointers is correct or not? main( ) ...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.