newbie question: Null pointer

J

junky_fellow

what is a NULL pointer ?
In many C programs, sometimes a variable is equated to NULL.
eg.
int i;
i = NULL;

what does this mean ? Is it the same as i = 0;

thanx for help in advance ....
 
R

Richard Bos

what is a NULL pointer ?

There's no such thing as a NULL pointer. There are null pointers. NULL
is a macro which expands to a null pointer constant, but it's important
to be aware of the difference between a null pointer (a run-time object)
and a null pointer _constant_ (a part of your source code).
In many C programs, sometimes a variable is equated to NULL.
eg.
int i;
i = NULL;

This is broken. You cannot portably assign NULL to non-pointer objects.
what does this mean ? Is it the same as i = 0;

No. That is, maybe; but only by accident. NULL may be defined as an
integral constant with value zero - which would work - _or_ as the same
constant cast to (void *) - which would not work.
In either case, however, NULL is supposed to be used for pointers;
assigning it to an int, even where it works, is a sign of a programmer
who is confused about the whole pointer/integer/null pointer issue.

You could do worse than reading the FAQ's section on null pointers:
<http://www.eskimo.com/~scs/C-faq/s5.html>.

Richard
 
E

Emmanuel Delahaye

junky_fellow wrote on 23/07/04 :
what is a NULL pointer ?

Sounds that the NULL thread srikes again!

NULL is a macro. It's a constant defined 0 or ((void*)0).

a null pointer is a pointer initialized with NULL or equivallent
constant expression, or the value of a null pointer of the same type of
void.

void *pa = 0;
char *pb = NULL;
int *pc = 123-123;
char *pd = pa;

A null pointer can't be dereferenced without invoking an undefined
behaviour. Its actuall a convenient way to specify the status of an
invalid pointer.
In many C programs, sometimes a variable is equated to NULL.
eg.
int i;
i = NULL;

This is a nonsense. The semantic of NULL is only valid in a pointer
context.
what does this mean ? Is it the same as i = 0;

Could be, but better to write 0 in an integer context.
 
D

Dan Pop

In said:
A null pointer can't be dereferenced without invoking an undefined
behaviour. Its actuall a convenient way to specify the status of an
invalid pointer.

Null pointer and invalid pointer are conceptually different things.

Evaluating a null pointer has well defined semantics, while evaluating
an invalid pointer results in undefined behaviour!

Null pointers are useful any time you need a valid pointer value that
doesn't correspond to any C object. The only reason null pointers are
invalid *for certain operations* (indirection, pointer arithmetic) is
because they don't point to any C object. They can be evaluated and
compared for equality with other pointer values.

Dan
 
J

junky_fellow

There's no such thing as a NULL pointer. There are null pointers. NULL
is a macro which expands to a null pointer constant, but it's important
to be aware of the difference between a null pointer (a run-time object)
and a null pointer _constant_ (a part of your source code).


This is broken. You cannot portably assign NULL to non-pointer objects.


No. That is, maybe; but only by accident. NULL may be defined as an
integral constant with value zero - which would work - _or_ as the same
constant cast to (void *) - which would not work.
In either case, however, NULL is supposed to be used for pointers;
assigning it to an int, even where it works, is a sign of a programmer
who is confused about the whole pointer/integer/null pointer issue.

You could do worse than reading the FAQ's section on null pointers:
<http://www.eskimo.com/~scs/C-faq/s5.html>.

Richard

thanx.. The link is indeed useful. Still i have certain doubts.
1)The Null pointer has a special meaning that it is not pointing anywhere yet.
How this is implemented. I mean to say it has to point somewhere.
Even if we represent it by all bits zeros then it is pointing to memory
location zero which a valid location.
Please dont get annoyed. i am a newbie and is very much confused about the
null pointers.
 
M

Mark A. Odell

(e-mail address removed) (junky_fellow) wrote in

1)The Null pointer has a special meaning that it is not pointing
anywhere yet.
How this is implemented. I mean to say it has to point somewhere.
Even if we represent it by all bits zeros then it is pointing to
memory location zero which a valid location.

Zero is not a valid location, usually. It happens to be on an embedded
system I'm working on but we just don't use it. Most systems are set to
trap on accesses to address zero.
 
D

Dan Pop

In said:
1)The Null pointer has a special meaning that it is not pointing anywhere yet.
How this is implemented. I mean to say it has to point somewhere.
Even if we represent it by all bits zeros then it is pointing to memory
location zero which a valid location.

It doesn't point to *any* C object allocated by your program. Thus, you
can always distinguish between a null pointer and a pointer to one of
your objects.

Furthermore, some implementations make the address pointed to by the null
pointer inaccessible to the program, so attempting to dereference the
null pointer crashes your program. Others allow reading, but no writing.
However, if the underlying hardware has no concept of memory protection,
both reading and writing through null pointers may appear to work
correctly, making the debugging more difficult. See also question 5.20
in the FAQ.

Dan
 
M

Mike Wahler

junky_fellow said:
(e-mail address removed) (Richard Bos) wrote in message

thanx.. The link is indeed useful. Still i have certain doubts.
1)The Null pointer has a special meaning that it is not pointing anywhere
yet.

Right. But it specifically *means* 'nowhere', whereas an uninitialized
pointer doesn't point anywhere meaningful either (except perhaps by
accident),
but has no guarantee about 'nowhere' frrm the language, as does a pointer
with
value == NULL.
How this is implemented.

That's implementation-specific, not defined by the language.
I mean to say it has to point somewhere.

No it does not. As a matter of fact, it *must* not.

Assigning the value NULL to a pointer is specifically defined
by the language to *mean* 'points nowhere'.
Even if we represent it by all bits zeros then it is pointing to memory
location zero which a valid location.

Memory location zero might or might not be valid for a given
platform. If the actual value of NULL happens to cooincide with
a 'real' valid (from the language's perspective) memory location,
the the implementation of NULL is broken. IOW if all-bits-zero is
a valid memory location for a given platform, then it is not valid
for a C implementation for that platform to define NULL as that
same pattern. NULL *must* mean 'no address'.

Please dont get annoyed. i am a newbie and is very much confused about the
null pointers.

Your confusion can be easily eliminated by quality textbooks.
See www.accu.org for reviews and recommendations.

-Mike
 
K

kyle york

Greetings,

Mike said:
Memory location zero might or might not be valid for a given
platform. If the actual value of NULL happens to cooincide with
a 'real' valid (from the language's perspective) memory location,
the the implementation of NULL is broken. IOW if all-bits-zero is
a valid memory location for a given platform, then it is not valid
for a C implementation for that platform to define NULL as that
same pattern. NULL *must* mean 'no address'.

Um, no, NULL *must* mean 0 (6.3.2.3 paragraph 3). How the implementation
maps this integer to a pointer is implementation defined. I've often
wondered how one might get around this, especially in an embedded
environment where every location might be legal. I suppose a strictly
conforming implementation could extend the pointer such that it contains
a ``valid'' bit but that wouldn't be an option on a small machine.
 
C

Christopher Benson-Manica

Mike Wahler said:
Your confusion can be easily eliminated by quality textbooks.

With all due respect, Mike, I don't know that that's necessarily the
case. If everyone who wanted to learn C (or specifically those
aspects that involved null pointers) could do so by reading a
textbook, there'd be a lot of college and high school instructors out
of a job.
 
D

Dave Vandervies

With all due respect, Mike, I don't know that that's necessarily the
case.

'Tis. There are a lot of things that can't be gotten from a textbook,
but a good understanding of the language isn't in that set.
If everyone who wanted to learn C (or specifically those
aspects that involved null pointers) could do so by reading a
textbook, there'd be a lot of college and high school instructors out
of a job.

While this is true, there's no danger of the college and high school
instructors being out of a job any time soon, since there's no shortage
of people looking for a piece of paper saying they've learned C to replace
(or, occasionally, to supplement) actually learning it.


dave
 
M

Mike Wahler

kyle york said:
Greetings,



Um, no, NULL *must* mean 0 (6.3.2.3 paragraph 3).

Yes, I misstated that, should have said 'null pointer' instead of
'NULL'.
How the implementation
maps this integer to a pointer is implementation defined.

Yes.

I've often
wondered how one might get around this, especially in an embedded
environment where every location might be legal. I suppose a strictly
conforming implementation could extend the pointer such that it contains
a ``valid'' bit but that wouldn't be an option on a small machine.

That's one way.

-Mike
 
K

kal

Null pointer and invalid pointer are conceptually different things.

Evaluating a null pointer has well defined semantics, while evaluating
an invalid pointer results in undefined behaviour!

Null pointers are useful any time you need a valid pointer value that
doesn't correspond to any C object. The only reason null pointers are
invalid *for certain operations* (indirection, pointer arithmetic) is
because they don't point to any C object. They can be evaluated and
compared for equality with other pointer values.

Merci beaucoup! Subtle but worth learning, I think.

Can one increment a pointer whose value is NULL?

<code>
#include <stdio.h>

int main ()
{
int *p = NULL;

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

return 0;
}
</code>

<output>
00000004

</output>
 
C

Chris Torek

... How the implementation maps [the source-code null-pointer
construct(s)] to a pointer is implementation defined. I've often
wondered how one might get around this, especially in an embedded
environment where every location might be legal. ...

The original PDP-11 implementation used address 0, which never
occurred in data because data came after code, and never occurred
in code because location 0 contained the a.out header.

Interestingly, when the Unix systems were ported to separate-I&D
PDP-11 systems, things broke. Now NULL *was* the address of
some valid data item, because while code-location-0 still
contained the a.out header, there was a separate data-location-0
and the C compiler put whatever data object happened to be first
in data-location-0. So translating NULL to address 0 resulted in
sometimes having the expression:

p == NULL && p == &var

be true!

The fix used was to insert a "shim" at data-location-zero, so that
the first C-code variable had data-address 2 rather than data-address
0.

Note that a C system could legitimately do this "the other way":
instead of choosing a particular *fixed* location (such as 0 or 0xffff)
and stuffing a secret object into that address, the compiler could
compile "p = NULL" as if it read:

p = (void *)&__runtime_nil_object;

The linker would then insert the "__runtime_nil_object" object
somewhere -- anywhere! -- in memory, and all tests for NULL pointers
would just compare the pointer value with (void *)&__runtime_nil_object.

Fixed locations are generally easier to handle, and if 0 is for
some reason not a good choice, (char *)(-1) is often just as good:
"p == NULL" tests can compile to "ld reg,p; inc reg" on conventional
inc-sets-condition-code systems, or "addcc p_reg,1,%g0" on SPARC-like
systems, for instance. Note that this is often the same number of
instructions that are required to test whether p==0!
 
J

junky_fellow

Mike Wahler said:
yet.

Right. But it specifically *means* 'nowhere', whereas an uninitialized
pointer doesn't point anywhere meaningful either (except perhaps by
accident),
but has no guarantee about 'nowhere' frrm the language, as does a pointer
with
value == NULL.


That's implementation-specific, not defined by the language.


No it does not. As a matter of fact, it *must* not.

Assigning the value NULL to a pointer is specifically defined
by the language to *mean* 'points nowhere'.


Memory location zero might or might not be valid for a given
platform. If the actual value of NULL happens to cooincide with
a 'real' valid (from the language's perspective) memory location,
the the implementation of NULL is broken. IOW if all-bits-zero is
a valid memory location for a given platform, then it is not valid
for a C implementation for that platform to define NULL as that
same pattern. NULL *must* mean 'no address'.



Your confusion can be easily eliminated by quality textbooks.
See www.accu.org for reviews and recommendations.

-Mike

So, you mean to say that if try to access the memory location pointed
to by a NULL pointer on a system valid for C implementation i MUST
get
some error ( may be segmentation fault ).

Is this possible, that a some system may define some other location
( instead of location 0 ) as INVALID and then use that location for a
NULL pointer.

I have read some articles on C which say that one should never test
for
a pointer to be NULL by testing it with integer 0. If you say that
NULL
pointer always points to location 0, then why can't we test whetehr a
pointer is null pointer or not by comparing it with integer 0.

i may sound stupid to you, but i need your help.
 
K

kevin.bagust

junky_fellow said:
So, you mean to say that if try to access the memory location pointed to
by a NULL pointer on a system valid for C implementation i MUST get
some error ( may be segmentation fault ).

No, if you try to access the memory pointed to by the null pointer you
invoke undefined behaviour. This meens that from that point onwards the
program is free to do anything at all, this incudes producing an error,
formating your harddrive, or even worse seeming to work fine.
Is this possible, that a some system may define some other location (
instead of location 0 ) as INVALID and then use that location for a NULL
pointer.

In C source code setting a pointer to 0, always sets the pointer to the
null pointer, but in runable program the actual representation of the null
pointer is not necessarly zero.
I have read some articles on C which say that one should never test for
a pointer to be NULL by testing it with integer 0. If you say that NULL
pointer always points to location 0, then why can't we test whetehr a
pointer is null pointer or not by comparing it with integer 0.

Partly it is a style thing. If you compare a pointer with the NULL macro
it tells anyone who reads the program that you are comparing a pointer. It
also allows for future changes in the C standard, which could change the
definition of the NULL macro.

Kevin.
 
B

Barry Schwarz

Merci beaucoup! Subtle but worth learning, I think.

Can one increment a pointer whose value is NULL?

Go back and read the sentence containing "invalid". What kind of
operation is increment? What kind of object are you trying to operate
on?


<<Remove the del for email>>
 
R

Richard Bos

Merci beaucoup! Subtle but worth learning, I think.

Can one increment a pointer whose value is NULL?

No; pointer addition is only defined on pointers that do in fact point
to an object or array of objects, or one element past the end of one.
Since a null pointer does not point at any object, doing arithmetic with
it is undefined.

Richard
 
J

junky_fellow

kevin.bagust said:
No, if you try to access the memory pointed to by the null pointer you
invoke undefined behaviour. This meens that from that point onwards the
program is free to do anything at all, this incudes producing an error,
formating your harddrive, or even worse seeming to work fine.

that means memory pointed to by the NULL pointer is a valid location
in the process address space, but it is not pointing to any valid
object ?
In C source code setting a pointer to 0, always sets the pointer to the
null pointer, but in runable program the actual representation of the null
pointer is not necessarly zero.

could you please give me some hints about what happens in runnable program ?
what happens internally ?
 

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,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top