473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

NULL pointer internal representation.

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
15 3707
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
29
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's...
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.