By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

NULL pointer internal representation.

P: n/a
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

Apr 27 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
khan wrote:
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

Apr 27 '07 #2

P: n/a
On Apr 27, 3:59 pm, Chris Dollin <chris.dol...@hp.comwrote:
khan wrote:
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

Apr 27 '07 #3

P: n/a
khan <mu*********@gmail.comwrote:
On Apr 27, 3:59 pm, Chris Dollin <chris.dol...@hp.comwrote:
khan wrote:
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
Apr 27 '07 #4

P: n/a
On Apr 27, 12:38 pm, khan <mushtaqk...@gmail.comwrote:
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".

Apr 27 '07 #5

P: n/a
In article <11**********************@r30g2000prh.googlegroups .com>,
khan <mu*********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 27 '07 #6

P: n/a
On Apr 27, 11:50 am, khan <mushtaqk...@gmail.comwrote:
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.

Apr 27 '07 #7

P: n/a
khan wrote:
On Apr 27, 3:59 pm, Chris Dollin <chris.dol...@hp.comwrote:
>khan wrote:
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.

--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.

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

Apr 27 '07 #8

P: n/a
khan wrote:
>
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?.
If it is a pointer value, it is NULL. If it is an integral value,
it is something else. You don't need to worry about it.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 27 '07 #9

P: n/a
In article <11*********************@t39g2000prd.googlegroups. com>
"christian.bau" <ch***********@cbau.wanadoo.co.ukwrites:
>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.)

--
Drew Lawson | What is an "Oprah"?
dr**@furrfu.com | -- Teal'c
http://www.furrfu.com/ |
Apr 27 '07 #10

P: n/a
In article <f0***********@home.ecynic.com>,
Drew Lawson <dr**@furrfu.comwrote:
>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.
--
All is vanity. -- Ecclesiastes
Apr 27 '07 #11

P: n/a
On Apr 27, 6:56 pm, d...@furrfu.com (Drew Lawson) wrote:
In article <1177675507.172634.92...@t39g2000prd.googlegroups. com>
"christian.bau" <christian....@cbau.wanadoo.co.ukwrites:
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);
Undefined behavior.
char *ptr2 = ptr - offset;
Undefined behavior if offset had any value other than zero.

Apr 27 '07 #12

P: n/a
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

On Apr 27, 5:05 pm, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
On Apr 27, 12:38 pm,khan<mushtaqk...@gmail.comwrote:
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".

May 15 '07 #13

P: n/a
khan wrote:
Hi christian,
Please don't top-post (fixed).
On Apr 27, 5:05 pm, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
>On Apr 27, 12:38 pm,khan<mushtaqk...@gmail.comwrote:
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".

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.

--
There' no hortage of vowel on Uenet.

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

May 15 '07 #14

P: n/a
khan <mushtaqk...@gmail.comwrote:
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.]

--
Peter

May 16 '07 #15

P: n/a
khan wrote: *** AND top-posted - fixed ***
"christian.bau" <christian....@cbau.wanadoo.co.ukwrote:
>khan<mushtaqk...@gmail.comwrote:
>>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".

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)

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.