C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do you tell if a pointer doesn't point to anything...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Narf the Mouse
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: How do you tell if a pointer doesn't point to anything... Reply with quote



....Without using NULL? There must be a way; people keep talking about
storing pointers to objects in different locations in the program.

Thanks.
Back to top
kwikius
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote



Narf the Mouse wrote:
Quote:
On Nov 25, 11:15 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
"Narf the Mouse" <largemo...@gmail.com> wrote in messagenews:1164523708.650762.23570 (AT) j44g2000cwa (DOT) googlegroups.com...

...Without using NULL?

If your program does not initialize or assign a value
to a pointer, then it doesn't point to anything.
That's how you tell.

At which point it crashes, which isn't what I want.

There must be a way; people keep talking about
storing pointers to objects in different locations in the program.

That doesn't really have anything to do with
your question.

-Mike

Can't I be an optimist on an unrelated tangent?

In other news, if I just make the pointer equal to an object of class
NullObjectClass, then include a 'bool isNull ();' function in all the
classes, I can write the program so that a relatively equivalent thing
will happen. I can even reuse the same type of NullObject for each type
of pointer.

You can do that but you presumably need a way to remember what type of
object it points to. You could also wrap your pointer in some class and
keep the raw pointer private, but what you end up with looks quite
similar to a smart pointer:

This one is pretty good and is destined to become part of the C++
standard:

http://www.boost.org/libs/smart_ptr/shared_ptr.htm

It manages the raw pointers life and deletes it when the last reference
is destroyed. Can also be checked for null. Also takes care of
downcasting and dynamic up casting and so on.

regards
Andy Little
Back to top
Narf the Mouse
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote



On Nov 25, 11:15 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
Quote:
"Narf the Mouse" <largemo...@gmail.com> wrote in messagenews:1164523708.650762.23570 (AT) j44g2000cwa (DOT) googlegroups.com...

...Without using NULL?

If your program does not initialize or assign a value
to a pointer, then it doesn't point to anything.
That's how you tell.

At which point it crashes, which isn't what I want.

Quote:
There must be a way; people keep talking about
storing pointers to objects in different locations in the program.

That doesn't really have anything to do with
your question.

-Mike

Can't I be an optimist on an unrelated tangent?

In other news, if I just make the pointer equal to an object of class
NullObjectClass, then include a 'bool isNull ();' function in all the
classes, I can write the program so that a relatively equivalent thing
will happen. I can even reuse the same type of NullObject for each type
of pointer.

So, if anyone wants to know how to accomplish something along those
lines, that's one way.
Back to top
Ian Collins
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

Narf the Mouse wrote:
Quote:

On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@gmail.com> wrote:

Narf the Mouse wrote:

...Without using NULL? There must be a way;

There isn't.


Er...Then what do people do with all those pointers scattered around?

What pointers? If you're that concerned, either set them to NULL after

delete or avoid them altogether and use smart pointer objects.

--
Ian Collins.
Back to top
Wayne Marsh
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

Jim Langston wrote:
Quote:
"Kai-Uwe Bux" <jkherciueh (AT) gmx (DOT) net> wrote in message
news:ekbg7j$ofb$1 (AT) murdoch (DOT) acc.Virginia.EDU...
Narf the Mouse wrote:


On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@gmail.com> wrote:
Narf the Mouse wrote:
...Without using NULL? There must be a way;
There isn't.
Er...Then what do people do with all those pointers scattered around?
They think through the program and prove for every single line where they
use a pointer that it is valid. That is hard (in fact, it is equivalent to
the halting problem, so you cannot leave this kind of deduction to the
compiler). It gets even harder in the presence of code that might throw
exceptions (since every new has to pair with exactly one delete along each
execution path). All of this being so difficult is one of the main reasons
that pointers are best avoided.

Actually, I find it just as hard as ensuring my integer variables have valid
data. If you think about what you're doing, it's not necessarily as hard as
anything else.

Except that you don't have to worry about cleaning up an integer.
Everything is nice and automatic.

Of course, more robust code would probably be using the RAII paradigm.
Back to top
Kai-Uwe Bux
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

Narf the Mouse wrote:

Quote:


On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@gmail.com> wrote:
Narf the Mouse wrote:
...Without using NULL? There must be a way;

There isn't.

Er...Then what do people do with all those pointers scattered around?

They think through the program and prove for every single line where they
use a pointer that it is valid. That is hard (in fact, it is equivalent to
the halting problem, so you cannot leave this kind of deduction to the
compiler). It gets even harder in the presence of code that might throw
exceptions (since every new has to pair with exactly one delete along each
execution path). All of this being so difficult is one of the main reasons
that pointers are best avoided.


Best

Kai-Uwe Bux
Back to top
Jim Langston
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

"Kai-Uwe Bux" <jkherciueh (AT) gmx (DOT) net> wrote in message
news:ekbg7j$ofb$1 (AT) murdoch (DOT) acc.Virginia.EDU...
Quote:
Narf the Mouse wrote:



On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@gmail.com> wrote:
Narf the Mouse wrote:
...Without using NULL? There must be a way;

There isn't.

Er...Then what do people do with all those pointers scattered around?

They think through the program and prove for every single line where they
use a pointer that it is valid. That is hard (in fact, it is equivalent to
the halting problem, so you cannot leave this kind of deduction to the
compiler). It gets even harder in the presence of code that might throw
exceptions (since every new has to pair with exactly one delete along each
execution path). All of this being so difficult is one of the main reasons
that pointers are best avoided.

Actually, I find it just as hard as ensuring my integer variables have valid
data. If you think about what you're doing, it's not necessarily as hard as
anything else.

Just make sure a couple of things and you'll be fine.
1. When you create a pointer, intialize it. Either to NULL to some valid
location.
2. When you change a pointer, think about what it was pointing to before, is
it now a dangling pointer (nothing pointing to it) so has to be deleted? Or
something else?
3. When you're done using a pointer either delete it or assign it to NULL.

Most of the cases I deal with pointers are for containers of polymorphic
objects. It's usually very simple in that case, I new it and push it onto
the container.

When ever I remove it from a contaer, I delete it.

When I"im doing with a container I iteratre though it and delete all the
pointers.

For this I find I hardly ever have problems with my pointers pointing to
invalid data.
Back to top
Noah Roberts
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

Narf the Mouse wrote:
Quote:
...Without using NULL? There must be a way;

There isn't.
Back to top
Narf the Mouse
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@gmail.com> wrote:
Quote:
Narf the Mouse wrote:
...Without using NULL? There must be a way;

There isn't.

Er...Then what do people do with all those pointers scattered around?
Back to top
Mike Wahler
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

"Narf the Mouse" <largemouse (AT) gmail (DOT) com> wrote in message
news:1164523708.650762.23570 (AT) j44g2000cwa (DOT) googlegroups.com...
Quote:
...Without using NULL?


If your program does not initialize or assign a value
to a pointer, then it doesn't point to anything.
That's how you tell.

Quote:
There must be a way; people keep talking about
storing pointers to objects in different locations in the program.

That doesn't really have anything to do with
your question.

-Mike
Back to top
David Harmon
Guest





PostPosted: Sun Nov 26, 2006 10:11 am    Post subject: Re: How do you tell if a pointer doesn't point to anything.. Reply with quote

On 25 Nov 2006 23:31:06 -0800 in comp.lang.c++, "Narf the Mouse"
<largemouse (AT) gmail (DOT) com> wrote,
Quote:


On Nov 25, 11:15 pm, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
"Narf the Mouse" <largemo...@gmail.com> wrote in messagenews:1164523708.650762.23570 (AT) j44g2000cwa (DOT) googlegroups.com...

...Without using NULL?

If your program does not initialize or assign a value
to a pointer, then it doesn't point to anything.
That's how you tell.

At which point it crashes, which isn't what I want.

No, it doesn't crash unless you try to _access_ that pointer value
somehow, either to examine it or to use it. Which you DO NOT do,
because that would be wrong.

Seriously, don't write your code in such a way as to have invalid
pointers littered around all over, and avoid the problem before it
happens. You probably shouldn't be using very many raw pointers anyway.
What are you trying to accomplish?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.