473,708 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL pointer internal representation.

Hi,

I read that pointer representation can non-zero bit pattern, machine
specific.Compil er 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 3766
khan wrote:
I read that pointer representation can non-zero bit pattern, machine
specific.Compil er 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...@h p.comwrote:
khan wrote:
I read that pointer representation can non-zero bit pattern, machine
specific.Compil er 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*********@gm ail.comwrote:
On Apr 27, 3:59 pm, Chris Dollin <chris.dol...@h p.comwrote:
khan wrote:
I read that pointer representation can non-zero bit pattern, machine
specific.Compil er 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...@gm ail.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************ **********@r30g 2000prh.googleg roups.com>,
khan <mu*********@gm ail.comwrote:
>I read that pointer representation can non-zero bit pattern, machine
specific.Compi ler 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
--
"Considerat ion 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...@gm ail.comwrote:
Hi,

I read that pointer representation can non-zero bit pattern, machine
specific.Compil er 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...@h p.comwrote:
>khan wrote:
I read that pointer representation can non-zero bit pattern, machine
specific.Compil er 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.Compil er 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.securityfoc us.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************ *********@t39g2 000prd.googlegr oups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

102
6009
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" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
29
3758
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 compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
42
5923
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 printed will it be all 0's or 0x12345678 int main(void) { char *ptr; ptr = 0;
64
3919
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")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
69
5580
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 assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
0
8788
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9159
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7925
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2097
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.