NULL pointer internal representation.

K

khan

Hi,

I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.

My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?.

Thanks,
-Khan
 
C

Chris Dollin

khan said:
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.

Yes: the internal representation of the null pointer is whatever the
C implementation finds convenient, with the compiler replacing the
null pointer constant (a literal zero value in pointer context) with
its convenient value.
My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?

(fx:rhetorical) How would the program manage to do that while
staying inside the Standard?

[It can't, so the compiler doesn't need to do anything about it.]
 
K

khan

khan said:
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.

Yes: the internal representation of the null pointer is whatever the
C implementation finds convenient, with the compiler replacing the
null pointer constant (a literal zero value in pointer context) with
its convenient value.
My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?

(fx:rhetorical) How would the program manage to do that while
staying inside the Standard?

[It can't, so the compiler doesn't need to do anything about it.]

--
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN



I want to confirm my understanding on these:

A proessor will have the representation of null pointer to some
specific value, which is not in the address space of the processor and
which processor treats as a null pointer that points nowhere.

Thanks,
-Mushtaq
 
R

Richard Tobin

khan said:
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.
My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?.

Suppose the null pointer is represented by the 32-bits word
0x12345678. How could a program refer to it it (except by using a
null pointer)? One way would be if some variable happens to have that
as its address, or if malloc() returns it. The compiler has to make
sure that doesn't happen. It could, for example, always allocate a
dummy variable at that address. Most likely it would not use
0x12345678 for the null pointer, but some value that it knows can't be
a valid address on the system in question. 0xffffffff perhaps.

The other way a program might refer to it is by casting an integer to
a pointer. Just as there's no guarantee that the null pointer will
have the bit pattern 0, there also no guarantee that (char
*)0x12345678 will produce a pointer with bit pattern 0x12345678. It
could produce something completely different - it's up to the
compiler. But even if it does produce 0x12345678, this is not a
problem, since casting arbitrary integers to pointers is not
guaranteed to be useful.

-- Richard
 
R

Richard Bos

khan said:
khan said:
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.

Yes: the internal representation of the null pointer is whatever the
C implementation finds convenient, with the compiler replacing the
null pointer constant (a literal zero value in pointer context) with
its convenient value.
My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?

(fx:rhetorical) How would the program manage to do that while
staying inside the Standard?

[It can't, so the compiler doesn't need to do anything about it.]
I want to confirm my understanding on these:

A proessor will have the representation of null pointer to some
specific value,
Yes;

which is not in the address space of the processor and

no, but actually not relevant to the other points (although I wonder how
any processor could refer to a pointer which is not in its own address
space);
which processor treats as a null pointer that points nowhere.

and yes.

Richard
 
C

christian.bau

A proessor will have the representation of null pointer to some
specific value, which is not in the address space of the processor and
which processor treats as a null pointer that points nowhere.

Processor? What is a processor? There is no such thing as a processor
in the C language. There is no such thing as an address space in the C
language.

Any address of an object that you take, like the address of a
variable, or the result of a successful malloc call, is never a null
pointer. Any address that you calculate in a legal way from such an
address is also never a null pointer. This has nothing to do with any
"address space".
 
C

christian.bau

Hi,

I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.

My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?.

Lets say on my machine a null pointer of type char* has the same
representation in memory as an unsigned long with a value of
0xdeadbeef. Lets also assume that sizeof (char *) = sizeof (unsigned
long) = sizeof (float).

unsigned long zero = 0;
unsigned long deadbeef = 0xdeadbeef;
char* p;
float f;

memcpy (&p, &zero, sizeof (p)); // p is now garbage
memcpy (&p, &deadbeef, sizeof (p)); // p is now a null pointer.
p = NULL; // p is now a null pointer
p = 0; // p is now a null pointer
p = 0xdeadbeef; // Doesn't compile
p = zero; // Doesn't compile
p = deadbeef; // Doesn't compile
p = (char *) 0xdeadbeef; // Implementation defined, could be null
pointer or garbage
p = (char *) 0; // In C99 it is guaranteed to be a null pointer
p = (char *) zero; // In C99 it is guaranteed to be a null pointer
p = (char *) deadbeef; // Implementation defined, could be null
pointer or garbage.
f = 0; // f has a value of 0
f = 0xdeadbeef; // f has a value quite close to 0xdeadbeef, but likely
not exactly the same
f = zero; // f has a value of 0
f = deadbeef; // f has a value quite close to 0xdeadbeef
memcpy (&f, &zero, sizeof (f)); // f could have any value, but in many
implementations it is zero.
memcpy (&f, &deadbeef, sizeof (f)); // f could have any value, most
lkely very different from 0xdeadbeef.

Remember: Pointers are not numbers. Pointers and numbers are very
different things. A "null pointer" is completely different from a
number zero, it is a pointer pointing to nothing, a number zero is a
number. Numbers have a representation (that is the bit pattern that
can be found in memory), and so have pointers; these representations
are completely unrelated. Just like float and unsigned long having
very different representations, so have char* and unsigned long. The
compiler identifies the particular construction called "null pointer
constant" and replaces it with a null pointer in certain situations
(but not in others). C99 also included the rule that an integer with a
value of zero, when cast to a pointer, always produces a null pointer.
Casting from integer to pointer can change the representation, just as
casting from integer to float can change the representation.

Cases where a null pointer constant is not replaced with a null
pointer:
int i = 0; // Thank heavens 0 is not a null pointer here.
char *p = "x"; int i = (p >= 0); // Doesn't compile.

These are not null pointer constants, but are null pointers:
(char *) 0
(char *) NULL
(char *) (void *) 0
(void *) (void *) 0
(void *) (void *) NULL
(void *) NULL // on some implementations, not on others.
 
C

Chris Dollin

khan said:
khan said:
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.

Yes: the internal representation of the null pointer is whatever the
C implementation finds convenient, with the compiler replacing the
null pointer constant (a literal zero value in pointer context) with
its convenient value.
My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?

(fx:rhetorical) How would the program manage to do that while
staying inside the Standard?

[It can't, so the compiler doesn't need to do anything about it.]

(fx:hint (fx:snip signature))
I want to confirm my understanding on these:

A proessor will have the representation of null pointer to some
specific value,

A /C implementation/ will pick some convenient value [1], yes.
which is not in the address space of the processor

for "processor": not a necessary restriction, which is fortunate,
because there may be no such values.

for "implementation": by definition, since the null pointer
doesn't refer to any object, and hence isn't in something one
might call an "address space".
and
which processor treats as a null pointer that points nowhere.

(fx:s/processor/implementation/) Yes.

Did you have a particular problem which gave rise to your question?
Because addressing /that/ might be more useful to you.

[1] Or possibly values.
 
D

Drew Lawson

Any address of an object that you take, like the address of a
variable, or the result of a successful malloc call, is never a null
pointer. Any address that you calculate in a legal way from such an
address is also never a null pointer.

Now you have me curious.

char c;
void *ptr = &c;
unsigned long offset = (ptr - NULL);
char *ptr2 = ptr - offset;

First, I'll acknowledge that 'offset' isn't (as I understand things)
guaranteed to be large enough to hold the expression.

Is any of that not legal?
Does ptr2 not end up being a null pointer?

(Not being a compiler implementor, I find some of the implications
of the standard fascinating, even though they have no practical
value to me.)
 
W

Walter Roberson

Drew Lawson said:
Now you have me curious.
char c;
void *ptr = &c;
unsigned long offset = (ptr - NULL);
char *ptr2 = ptr - offset;
First, I'll acknowledge that 'offset' isn't (as I understand things)
guaranteed to be large enough to hold the expression.
Is any of that not legal?
Does ptr2 not end up being a null pointer?

There are no defined pointer operations on void* except for
casting to other pointer types.

The - operation applied to pointers is only defined for pointers
into the same object (or one past the object); NULL never points
into any object (or one past an object), so subtracting NULL
from any pointer is not defined (not even NULL - NULL). The
C89 standard lists the allowed operations and says specifically
that all other cases are undefined behaviour.
 
C

christian.bau

Now you have me curious.

char c;
void *ptr = &c;
unsigned long offset = (ptr - NULL); Undefined behavior.
char *ptr2 = ptr - offset;
Undefined behavior if offset had any value other than zero.
 
K

khan

Hi christian,

Thanks for reply.You mean to say NULL pointer has no relation to the
Processor, then what it means by NULL pointer values are specific to
machine(Processor).

Thanks,
-Mushtaq
 
C

Chris Dollin

khan said:
Hi christian,

Please don't top-post (fixed).
Thanks for reply.You mean to say NULL pointer has no relation to the
Processor, then what it means by NULL pointer values are specific to
machine(Processor).

He means what he says: no C object has an address equal to the null
pointer. This is arranged by the various C implementations. Whether
or not the underlying processor (about which C does not speak) has
"address spaces" doesn't matter; whether or not that processor has
a special null pointer value /also/ doesn't matter, although it may
be /convenient/ for the C null pointer and the processor's null
pointer to be the same.
 
P

Peter Nilsson

khan said:
Hi,

I read that pointer representation can [be?] non-zero bit
pattern, machine specific.Compiler when comes accross value
'0' in pointer context, converts it to machine specific null
pointer bit-pattern.

My question is if a program refers to that specific value
which is used by the machine to refer to null pointer, how
compiler treats that?.

How does a program refer to that specific value?

Note that different pointer types can have different sizes.
Note that even if all pointers have the same size, there
may still be more than one representation for a null
pointer.

For instance, imagine a machine with a 24-bit address space
that uses 32-bit address registers. If the the 'compare'
instruction on such a machine ignores the top 8 bits of
its operand, then there are at least 256 different null
pointer representations, even if there is only 1 24-bit
representation. [The use of 68000 on early Macs came very
close to this.]
 
C

CBFalconer

khan wrote: *** AND top-posted - fixed ***
Thanks for reply.You mean to say NULL pointer has no relation to
the Processor, then what it means by NULL pointer values are
specific to machine(Processor).

The C language has no specific reference to a processor (I think),
but the implementation must take into account what does the
processing, etc. This is the layer that creates the NULL pointer,
defines it, etc. The standard specifies ways in which it may be
specified, and its meaning.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 

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

No members online now.

Forum statistics

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

Latest Threads

Top