473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Thanks.

Nov 26 '06 #1
20 2141

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

Nov 26 '06 #2


On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@g mail.comwrote:
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?

Nov 26 '06 #3

"Narf the Mouse" <la********@gma il.comwrote in message
news:11******** *************@j 44g2000cwa.goog legroups.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.
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
Nov 26 '06 #4


On Nov 25, 11:15 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"Narf the Mouse" <largemo...@gma il.comwrote in messagenews:11* *************** *****@j44g2000c wa.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.

So, if anyone wants to know how to accomplish something along those
lines, that's one way.

Nov 26 '06 #5
Narf the Mouse wrote:
>
On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@g mail.comwrote:
>>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.
Nov 26 '06 #6
Narf the Mouse wrote:
>

On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@g mail.comwrote:
>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
Nov 26 '06 #7

Narf the Mouse wrote:
On Nov 25, 11:15 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"Narf the Mouse" <largemo...@gma il.comwrote in messagenews:11* *************** *****@j44g2000c wa.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

Nov 26 '06 #8

"Kai-Uwe Bux" <jk********@gmx .netwrote in message
news:ek******** **@murdoch.acc. Virginia.EDU...
Narf the Mouse wrote:
>>

On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@g mail.comwrote:
>>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.
Nov 26 '06 #9
Jim Langston wrote:
"Kai-Uwe Bux" <jk********@gmx .netwrote in message
news:ek******** **@murdoch.acc. Virginia.EDU...
>Narf the Mouse wrote:
>>>
On Nov 25, 10:50 pm, "Noah Roberts" <roberts.n...@g mail.comwrote:
Narf the Mouse wrote:
...Withou t 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.
Nov 26 '06 #10

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

Similar topics

303
17706
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
10
2063
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << " and &s = " << &s << "\n";
110
9945
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
52
5651
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every time I get the textbooks out end up throwing them against the wall in rage. Thats been going on for 10 years now. Anyway, I have: typedef struct _record { int age;
35
2898
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
22
4812
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
9
56195
by: weaselboy1976 | last post by:
Hello How can I print a pointer's value directly (without assigning the value to another variable)? For example: #include <stdio.h> int main() { int *zp;
26
3060
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
49
2724
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
8983
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.