checking if pointer is NULL allowed?

A

atv

Hi,
Just to check, if i set a pointer explicitly to NULL, i'm not allowed
to dereference it? Why is that, it's not
like it's pointing to any garbage right? Why else set it to NULL. I can
remember some instances where
i did just that and printf reported something like : (null).

I'm asking because i read somewhere that it is not valid. I thought it
simply wasn't valid to dereference
a pointer who is _not_ set to point to anything.

But i can check if it is NULL or not right ? ( i hope ) :)

Rgds,
-alef
 
W

Walter Roberson

Just to check, if i set a pointer explicitly to NULL, i'm not allowed
to dereference it?
Right.

Why is that, it's not
like it's pointing to any garbage right?

It isn't pointing to anything, so I guess from that point of view
it isn't pointing to any garbage. ;-)
Why else set it to NULL. I can
remember some instances where
i did just that and printf reported something like : (null).
I'm asking because i read somewhere that it is not valid. I thought it
simply wasn't valid to dereference
a pointer who is _not_ set to point to anything.

But a NULL pointer -has- been set not to point to anything. The NULL
pointer is -defined- as not pointing to any valid object. What would
it mean to dereference it? The situation is philosophically similar
to dereferencing a void* pointer -- there is no "is" there!
 
S

santosh

atv said:
Hi,
Just to check, if i set a pointer explicitly to NULL, i'm not allowed
to dereference it?

You _can_, but the behaviour is undefined, since in C, a NULL pointer
is guaranteed to not point to any valid object.
Why is that, it's not
like it's pointing to any garbage right? Why else set it to NULL.

Setting an uninitialised pointer to NULL is a good practise. Also some
functions require a null pointer. Many programming constructs require a
null pointer as a sentinel value. Many functions return a null pointer
as an indication of failure.

I'm asking because i read somewhere that it is not valid. I thought it
simply wasn't valid to dereference
a pointer who is _not_ set to point to anything.
Yes.

But i can check if it is NULL or not right ? ( i hope ) :)

Yes, you can.
 
A

Alef.Veld

Well, if it is not pointing to anything anyway, couldn't my compiler
just printf out "null". And i swear i thought i have seen this.
and if i can check for it to be NULL (i'm assuming that's why you set
it to NULL in the first place, like you said, good practice) how does
it do this. Does it keep track of memory assignments?
 
J

Jens Thoms Toerring

Well, if it is not pointing to anything anyway, couldn't my compiler
just printf out "null". And i swear i thought i have seen this.

Yes, some libc implemetations (that's where printf() is located)
check if a pointer passed to printf() is NULL and then print out
"(null)" instead of crashing. But don't rely on that.
and if i can check for it to be NULL (i'm assuming that's why you set
it to NULL in the first place, like you said, good practice) how does
it do this. Does it keep track of memory assignments?

Setting uninitialized pointers (or pointers on which you have called
free()) to NULL is good for _you_ since then can check if a pointer
is valid or not. When you e.g. have

char *x = malloc( 10 );
if ( x == NULL ) {
exit( 0 );
}
free(x);

you can't see by simply looking at the value of 'x' if it's a pointer
that points to any memory you own (actually, just checking the value
of a pointer after a call of free() is forbidden). But if you always
set such pointer to NULL then _you_ can test if it's a valid pointer
or not and _you_ can thus avoid using bad pointers (moreover, if the
pointer ist invalid but not NULL and accidentaly points to some memory
you can access it might look as dereferencing it works as expected
while in reality there's a big fat bug in your code).

Regards, Jens
 
R

Richard Tobin

atv said:
Just to check, if i set a pointer explicitly to NULL, i'm not allowed
to dereference it? [...]
But i can check if it is NULL or not right ? ( i hope ) :)

Perhaps you misunderstand "dereference"? You're allowed to test the
pointer itself:

if(p == null) /* good */

but not "follow" it:

if(*p == 'a') /* bad */

printf()s that print "(null)" for NULL pointers are doing something
like:

if(p == NULL)
puts("(null)");
else
..whatever..;

-- Richard
 
D

Default User

Well, if it is not pointing to anything anyway, couldn't my compiler
just printf out "null". And i swear i thought i have seen this.

It's undefined behavior. There IS NO DEFINED BEHAVIOR for undefined
behavior. Say that over and over until you get it. That means that if
your implentation wants to crash it can crash. There is no defined
behavior for undefined behavior. If your implementation wants to print
(null), it can. There is no defined behavior for undefined behavior.

PS:

There is no defined behavior for undefined behavior.





Brian
 
B

Ben Pfaff

Well, if it is not pointing to anything anyway, couldn't my compiler
just printf out "null". And i swear i thought i have seen this.

Are you confusing dereferencing an invalid pointer with passing a
null pointer to printf in place of a pointer argument? Both are
undefined behavior. The former is usually difficult or expensive
for an implementation to detect, whereas the latter is handled
gracefully by better-quality C libraries (which often do print
"null" or "(null)").
 
I

Ian Collins

Ben said:
Are you confusing dereferencing an invalid pointer with passing a
null pointer to printf in place of a pointer argument? Both are
undefined behavior. The former is usually difficult or expensive
for an implementation to detect, whereas the latter is handled
gracefully by better-quality C libraries (which often do print
"null" or "(null)").

That depend on one's idea of graceful. Passing a null pointer as a
parameter to printf is never, a good thing to do.
 
G

Guest

Ian said:
That depend on one's idea of graceful. Passing a null pointer as a
parameter to printf is never, a good thing to do.

Never say never :) There's nothing wrong with printf("%p", (void *) 0);
 
B

Bob Martin

in 716374 20070124 230752 Ben Pfaff said:
Are you confusing dereferencing an invalid pointer with passing a
null pointer to printf in place of a pointer argument? Both are
undefined behavior. The former is usually difficult or expensive
for an implementation to detect, whereas the latter is handled
gracefully by better-quality C libraries (which often do print
"null" or "(null)").

Surely all the OP needed to know is that inspecting the value of a pointer
does not constitute "dereferencing" it?
 
J

jaysome

Never say never :) There's nothing wrong with printf("%p", (void *) 0);

And oddly enough, there may be something wrong with the following,
understandble, mistake:

/*let's print the value of the NULL pointer*/
printf("%p\n", NULL);
 
A

Alef.Veld

Are you confusing dereferencing an invalid pointer with passing a
null pointer to printf in place of a pointer argument?

I'm not quite sure what you mean here. I just wanted to know why i
sometimes noticed that printf printed out a nice "null" when i gave it
a pointer i explicitly set to NULL. I'm not talking about 'bad'
pointers here. But now i have my answer, the better libc libraries do
print this out. Thanks everyone.

Surely all the OP needed to know is that inspecting the value of a
pointer
does not constitute "dereferencing" it?

No. I know that :)

And oddly enough, there may be something wrong with the following,
understandble, mistake:
/*let's print the value of the NULL pointer*/
printf("%p\n", NULL);
 
C

Clever Monkey

Ben said:
Are you confusing dereferencing an invalid pointer with passing a
null pointer to printf in place of a pointer argument? Both are
undefined behavior. The former is usually difficult or expensive
for an implementation to detect, whereas the latter is handled
gracefully by better-quality C libraries (which often do print
"null" or "(null)").

It is common practice in many shops I've worked at to have a function or
macro used in debugging routines that can take a pointer and return the
pointer or "null", depending on if the pointer is valid to dereference
or not.
 
B

Ben Pfaff

Clever Monkey said:
It is common practice in many shops I've worked at to have a function
or macro used in debugging routines that can take a pointer and return
the pointer or "null", depending on if the pointer is valid to
dereference or not.

What kind of code do you work on?
 
S

santosh

Clever said:
It is common practice in many shops I've worked at to have a function or
macro used in debugging routines that can take a pointer and return the
pointer or "null", depending on if the pointer is valid to dereference
or not.

Maybe, but that's not possible in standard C.
 
K

Keith Thompson

I'm not quite sure what you mean here. I just wanted to know why i
sometimes noticed that printf printed out a nice "null" when i gave it
a pointer i explicitly set to NULL. I'm not talking about 'bad'
pointers here. But now i have my answer, the better libc libraries do
print this out. Thanks everyone.

Surely all the OP needed to know is that inspecting the value of a
pointer
does not constitute "dereferencing" it?

No. I know that :)

And oddly enough, there may be something wrong with the following,
understandble, mistake:
/*let's print the value of the NULL pointer*/
printf("%p\n", NULL);

It's very difficult to tell which parts of your followup are quoted
from previous articles, since you didn't use "> " markers. You're
also missing some attribution lines, so it's hard to tell who you're
quoting. Google Groups does these for you automatically, so I'm not
sure what went wrong here.

jay wrote the last bit:
| And oddly enough, there may be something wrong with the following,
| understandble, mistake:
| /*let's print the value of the NULL pointer*/
| printf("%p\n", NULL);

One legal definition for the NULL macro is just 0, which is a null
pointer constant, but is only treated as a pointer in the right
context. Since printf is a variadic function (it takes a variable
number and type(s) of arguments), the compiler doesn't know that that
argument is supposed to be a pointer; it just passes a value of type
int with value 0, which may or may not look like a null pointer by the
time printf sees it.

The correct way to print the value of a null pointer is:

printf("%p\n", (void*)NULL);

The output is implementation-defined.

Section 5 of the comp.lang.c FAQ, <http://www.c-faq.com/> has a lot
more information on this.
 

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

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top