473,379 Members | 1,190 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,379 software developers and data experts.

De-referencing NULL

#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
Aug 4 '08 #1
28 1817
>#include <stdio.h>
>
int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP.
Whatever the implementation does or doesn't do here, it's not a bug
per the C standard. You may argue that it is a quality-of-implementation
issue that the program fails to delete its own source code.
>NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?
A C compiler is not required to diagnose a de-reference of NULL at
either compile time or run time. Undefined behaviour can include
fetching zero or ignoring write attempts, or it can include fetching
zero or ignoring write attempts except when the boss or customer
is watching during a demonstration, then catching his tie on fire.
>P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
No matter what happens in this situation, it's OK with the C specs.
No possible behavior is forbidden, even if it violates the laws of
physics.

Aug 4 '08 #2
rahul wrote:
#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
The behaviour is defined to be undefined. Here is the relevant bit from
n1256.pdf:

6.5.3.2

4 The unary * operator denotes indirection. If the operand points to a
function, the result is a function designator; if it points to an
object, the result is an lvalue designating the object. If the operand
has type ??pointer to type??, the result has type ??type??. If an
invalid value has been assigned to the pointer, the behavior of the
unary * operator is undefined.87)

Part of the indicated footnote (which is not normative):

Among the invalid values for dereferencing a pointer by the unary *
operator are a null pointer, [ ... ]

Since no constraint is violated, the implementation is not obliged to
emit a diagnostic, though one would be useful. But it's hard to do this
in all possible cases.

Aug 4 '08 #3
rahul <ra*********@gmail.comwrites:
#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
The behavior is undefined, which means that any behavior is ok as far
as the standard is concerned. If your implementation produced no
diagnostics but made demons fly out of your nose, it might violate the
laws of physics, but it wouldn't violate the standard.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 4 '08 #4
rahul said:
#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP.
It isn't. It's a bug in the program. It dereferences NULL.
NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?
Removing the programmer's ability to parade his stupidity necessarily
entails removing his ability to be clever when necessary.
P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
Yes. The Standard doesn't mandate what must happen when the programmer does
this particular kind of stupid thing, so any behaviour is acceptable.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 4 '08 #5
rahul wrote:
#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?
I have already provided the reason why no compile-time diagnostics are
required. As for runtime diagnostics/exceptions, note that the
segfaults you are getting from Windows XP (compiled with MSVC) and
Linux are a response of the operating system. DJGPP on the other hand
is designed to produce programs that run under DOS, which does not have
memory protection and hence fails to trap the access to memory address
zero.

All the above is information beyond the scope of the C Standard, which
by categorising the behaviour as undefined, allows different
implementations to do whatever makes the most sense for them and avoids
defining this complex issue. If it had defined the behaviour in any
manner, there would have been some systems which would then have had to
emulate this behaviour and hence suffer considerable performance and
implementation problems.

This is merely a special case of C allowing access beyond an object.
P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
Aug 4 '08 #6
rahul <ra*********@gmail.comwrote:
char *p = NULL;
printf ("%c\n", *p);
This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP.
It isn't. Dereferencing a null pointer has undefined behaviour. Crashing
is legal; printing a message and then crashing is legal; and so is
printing any random value, as if it were a normal dereference.

Richard
Aug 4 '08 #7
On 4 Aug, 07:24, rahul <rahulsin...@gmail.comwrote:
#include <stdio.h>

int
main (void) {
* char *p = NULL;
* printf ("%c\n", *p);
* return 0;

}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
I've even used a compiler that made it a command line option.
You could choose if dereferencing NULL crashed or not.
Why you would want this option is a good question...

--
Nick Keighley
Aug 4 '08 #8
Nick Keighley <ni******************@hotmail.comwrites:
On 4 Aug, 07:24, rahul <rahulsin...@gmail.comwrote:
#include <stdio.h>

int
main (void) {
* char *p = NULL;
* printf ("%c\n", *p);
* return 0;

}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?

I've even used a compiler that made it a command line option.
You could choose if dereferencing NULL crashed or not.
Why you would want this option is a good question...
The most probable reason is compatibility with a previous implementation
for which it was the (perhaps even documented) behaviour?

Yours,

--
Jean-Marc
Aug 4 '08 #9
Nick Keighley wrote:
On 4 Aug, 07:24, rahul <rahulsin...@gmail.comwrote:
>#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;

}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this
behaviour is OK with C specs?

I've even used a compiler that made it a command line option.
You could choose if dereferencing NULL crashed or not.
Why you would want this option is a good question...
To be notified of invalid deferences (as NULL always is) when the
underlying OS/hardware does not catch it?

Aug 4 '08 #10
santosh wrote:
rahul wrote:
>#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

I have already provided the reason why no compile-time diagnostics are
required. As for runtime diagnostics/exceptions, note that the
segfaults you are getting from Windows XP (compiled with MSVC) and
Linux are a response of the operating system. DJGPP on the other hand
is designed to produce programs that run under DOS, which does not
have memory protection and hence fails to trap the access to memory
address zero.

All the above is information beyond the scope of the C Standard, which
by categorising the behaviour as undefined, allows different
implementations to do whatever makes the most sense for them and
avoids defining this complex issue. If it had defined the behaviour in
any manner, there would have been some systems which would then have
had to emulate this behaviour and hence suffer considerable
performance and implementation problems.

This is merely a special case of C allowing access beyond an object.
And the above is subtly wrong and highly misleading.

A null pointer *cannot* by definition point to anywhere and thus a
deference of a null pointer is always invalid. This has nothing to do
with the hardware or with memory addresses, but is dictated by the
semantics of C. It just so happens that on the vast majority of
implementations the value that a null pointer contains is the same as
the value for memory address zero at a bit level. Thus when a null
pointer is deferenced, the implementation interprets the value as a
memory address and attempts to read it. This is caught under some
systems and not under others.

Conceptually a null pointer is distinct from a pointer containing the
address value zero, but under many flat memory model architectures
their representations are identical and hence, in the absence of any
special interpretation (which becomes cumbersome), deferencing a null
pointer performs the same action as deferencing a pointer containing
address zero.

Recently Harald van Dijk gave an example of an implementation (I think
it was the Tendra C compiler) that interprets a null pointer as having
a value other than zero.

Aug 4 '08 #11
santosh <sa*********@gmail.comwrites:
[...]
Conceptually a null pointer is distinct from a pointer containing the
address value zero, but under many flat memory model architectures
their representations are identical and hence, in the absence of any
special interpretation (which becomes cumbersome), deferencing a null
pointer performs the same action as deferencing a pointer containing
address zero.
[...]

Sort of -- except that there's not really such a thing as "the address
value zero" in C, at least not in the sense that you mean.

You can *convert* an integer value zero to a pointer type. If the
converted value happens to be a constant expression, the result is a
null pointer value, due to the special-case rule that C uses to define
null pointer constants. If it's a non-constant expression whose
current run-time value is zero, the result of the conversion is
implementation-defined, and may or may not correspond to "the address
value zero" on the underlying system, assuming such a thing is even
meaningful.

This means that converting a value of zero to a pointer type might
yield different results depending on whether the zero value is a
constant expression or not, which is a bit bizarre. But most systems
don't make this distinction because, as santosh said in text I've
snipped, most systems choose to use all-bits-zero as the null pointer
representation, avoiding the need for special-case code to handle the
special-case rule for null pointer constants. On most (but not all)
such systems, attempts to dereference a null pointer are caught with
no additional effort, since 00000000 is not a valid address.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 4 '08 #12
Keith Thompson schrieb:
>P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?

The behavior is undefined, which means that any behavior is ok as far
as the standard is concerned.
I've started wondering whether this is a good idea - on architectures
like the AVR, there are indeed memory-mapped I/O registers in the
address space at address 0x00. For instance on an ATmega32, writing to

*((volatile unsigned char*)0x00) = 0x00;

Would set the TWBR (Two Wire Serial Interface Bit Rate Register). Is
this undefined behaviour, too? This would then mean the language is in
some way incompatible with the architecture, which is a really weird thing.

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Aug 4 '08 #13
>Keith Thompson schrieb:
>The behavior is undefined, which means that any behavior is ok as far
as the standard is concerned.
In article <i6************@joeserver.homelan.net>
Johannes Bauer <df***********@gmx.dewrote:
>I've started wondering whether this is a good idea -
It is, in fact, a great idea, precisely because of what you say next.
>on architectures like the AVR, there are indeed memory-mapped I/O
registers in the address space at address 0x00. For instance on an
ATmega32, writing to

*((volatile unsigned char*)0x00) = 0x00;

Would set the TWBR (Two Wire Serial Interface Bit Rate Register). Is
this undefined behaviour, too?
Undefined by the C Standard, yes.

By being left undefined in the C Standard, implementors are allowed
to define it themselves. Implementors working on the ATmega32 can
define (*(volatile unsigned char *)0) as "access the TWBR".

Had the C Standard said "this must trap" or "this must access RAM",
implementors writing implementations for the ATmega32 would *not*
have been able to give you access to the TWBR in this obvious and
simple manner.

Undefined behavior is "bad" in that, if you use it, you lose the
guarantees of the C Standard. But it is "good" in that, by leaving
it undefined, you can use non-"Standard C" in ways guaranteed by
something *other* than the C Standard. There is nothing wrong with
doing this: you just need to know that your guarantees are coming
from "Frobozz Inc ATmega32 C" and not "ISO Standard C".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Aug 4 '08 #14
Chris Torek schrieb:
>I've started wondering whether this is a good idea -

It is, in fact, a great idea, precisely because of what you say next.
Okay, alright, I understood what you wrote. Thank you for your
comprehensive answer!

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Aug 4 '08 #15
rahul wrote:
>
#include <stdio.h>

int main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++
6.0 compiles the program with diagnostics and the program crashes
when ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing
NULL without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this
behaviour is OK with C specs?
No, it is not allowable to dereference the NULL pointer. Behaviour
is undetermined. The fact that DJGPP doesn't detect this fault is
a Quality of Implementation detail.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Aug 4 '08 #16
In article <i6************@joeserver.homelan.net>,
Johannes Bauer <df***********@gmx.dewrote:
>Keith Thompson schrieb:
>>P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
The behavior is undefined, which means that any behavior is ok as far
as the standard is concerned.

I've started wondering whether this is a good idea - on architectures
like the AVR, there are indeed memory-mapped I/O registers in the
address space at address 0x00. For instance on an ATmega32, writing to

*((volatile unsigned char*)0x00) = 0x00;

Would set the TWBR (Two Wire Serial Interface Bit Rate Register). Is
this undefined behaviour, too?
It is undefined behavior.
>This would then mean the language is in
some way incompatible with the architecture, which is a really weird thing.
Not necessarily incompatible. It is not saying it must be rejected
or that it must fail, it is "just" not telling you what happens.
What it is doing then is leaving the door open to allow you to do it,
of course, along with a compiler for the platform in question.
Since C is not defining it, it's giving you a "fighting chance"
of giving you the rope on machines where you need that rope.
On those where you don't need the rope, it is still there though.
But as in the thread, on those other machines, compilers etc
can set it up for a trap, and so on.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 5 '08 #17
On Mon, 04 Aug 2008 20:42:58 +0200, Johannes Bauer
<df***********@gmx.dewrote in comp.lang.c:
Keith Thompson schrieb:
P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?
The behavior is undefined, which means that any behavior is ok as far
as the standard is concerned.

I've started wondering whether this is a good idea - on architectures
like the AVR, there are indeed memory-mapped I/O registers in the
address space at address 0x00. For instance on an ATmega32, writing to

*((volatile unsigned char*)0x00) = 0x00;

Would set the TWBR (Two Wire Serial Interface Bit Rate Register). Is
this undefined behaviour, too? This would then mean the language is in
some way incompatible with the architecture, which is a really weird thing.
As others have already mentioned, it is of course undefined behavior,
which is not necessarily a bad thing in all contexts.

But if Atmel moved that register from address 0x00 to 0x01, or 0x08,
or indeed any other address, the result of reading from or writing to
that address would still be undefined. Any access to any hardware
device is completely undefined under the C standard, and that is
exactly as it should be.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 5 '08 #18
On 5 Aug 2008 at 1:24, Jack Klein wrote:
On Mon, 04 Aug 2008 20:42:58 +0200, Johannes Bauer
<df***********@gmx.dewrote in comp.lang.c:
>*((volatile unsigned char*)0x00) = 0x00;

Would set the TWBR (Two Wire Serial Interface Bit Rate Register). Is
this undefined behaviour, too? This would then mean the language is in
some way incompatible with the architecture, which is a really weird thing.

As others have already mentioned, it is of course undefined behavior,
which is not necessarily a bad thing in all contexts.

But if Atmel moved that register from address 0x00 to 0x01, or 0x08,
or indeed any other address, the result of reading from or writing to
that address would still be undefined. Any access to any hardware
device is completely undefined under the C standard, and that is
exactly as it should be.
This is complete nonsense, as you are well aware. The whole point of the
OP's query is that 0 in a pointer context is a null pointer constant,
whereas 1 and 8 are not.

Aug 5 '08 #19
In article <i6************@joeserver.homelan.net>,
Johannes Bauer <df***********@gmx.dewrote:
>I've started wondering whether this is a good idea - on architectures
like the AVR, there are indeed memory-mapped I/O registers in the
address space at address 0x00. For instance on an ATmega32, writing to

*((volatile unsigned char*)0x00) = 0x00;

Would set the TWBR (Two Wire Serial Interface Bit Rate Register). Is
this undefined behaviour, too? This would then mean the language is in
some way incompatible with the architecture, which is a really weird thing.
I'm just going to address the last sentence.

You might consider the language incompatible with the architecture if
it said anything about what happened when address 0 was accessed, but
C doesn't do that. There is no necessary connection between C's null
pointer and the architecture's address 0, though they are the same in
most implementations. In particular, there's no reason that

(volatile unsigned char*)0x00

produce the architecture address 0. It could produce, say, 0xffffffff.
The conversion of integers to pointers can involve arbitrary computation,
but null pointers can only be generated from constants, so the compiler
could do or insert code for the necessary conversion.

int x = 0;
(char *)x

is not guaranteed to produce a null pointer.

I doubt that this would be a good idea on balance, though such a compiler
might be useful for certain kinds of debugging.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 6 '08 #20
In article <9N******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>This looks like a bug with DJGPP.
>It isn't. It's a bug in the program. It dereferences NULL.
It may not be a bug in the compiler, but it's certainly a deficiency
that it does not detect the error.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 6 '08 #21
Richard Tobin wrote, On 06/08/08 13:09:
In article <9N******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>This looks like a bug with DJGPP.
>It isn't. It's a bug in the program. It dereferences NULL.

It may not be a bug in the compiler, but it's certainly a deficiency
that it does not detect the error.
Or a feature, providing compatibility with old compilers which did not
detect the error. Specifically with programs that are playing about with
the interrupt vector table in DOS!
--
Flash Gordon
Aug 6 '08 #22
In article <k2************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>It may not be a bug in the compiler, but it's certainly a deficiency
that it does not detect the error.
>Or a feature, providing compatibility with old compilers which did not
detect the error. Specifically with programs that are playing about with
the interrupt vector table in DOS!
So it's a backward-compatible deficiency.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 6 '08 #23
In article <g7**********@pc-news.cogsci.ed.ac.uk>, ri*****@cogsci.ed.ac.uk
(Richard Tobin) wrote:
[...]
You might consider the language incompatible with the architecture if
it said anything about what happened when address 0 was accessed, but
C doesn't do that. There is no necessary connection between C's null
pointer and the architecture's address 0, though they are the same in
most implementations. In particular, there's no reason that

(volatile unsigned char*)0x00

produce the architecture address 0. It could produce, say, 0xffffffff.
[...]
If one needed a pointer of address 0 in such a case, say where an
architecture had a register at 0 and whose null pointer was non-zero, one
subtract 1 from a char* of address 1:

char* addr_0 = (char*) 1 - 1;
Aug 7 '08 #24
bl********@gishpuppy.com (blargg) writes:
In article <g7**********@pc-news.cogsci.ed.ac.uk>, ri*****@cogsci.ed.ac.uk
(Richard Tobin) wrote:
>[...]
You might consider the language incompatible with the architecture if
it said anything about what happened when address 0 was accessed, but
C doesn't do that. There is no necessary connection between C's null
pointer and the architecture's address 0, though they are the same in
most implementations. In particular, there's no reason that

(volatile unsigned char*)0x00

produce the architecture address 0. It could produce, say, 0xffffffff.
[...]

If one needed a pointer of address 0 in such a case, say where an
architecture had a register at 0 and whose null pointer was non-zero, one
subtract 1 from a char* of address 1:

char* addr_0 = (char*) 1 - 1;
That's one approach. Another is to use a non-constant expression:

int non_constant_zero = 0;
char *addr_0 = (char*)non_constant_zero;

(You could even declare non_constant_zero as const if you wanted to
confuse your readers.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 7 '08 #25
Richard Tobin wrote:
In article <k2************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>>It may not be a bug in the compiler, but it's certainly a deficiency
that it does not detect the error.
>>Or a feature, providing compatibility with old compilers which did not
detect the error. Specifically with programs that are playing about
with the interrupt vector table in DOS!

So it's a backward-compatible deficiency.
This discussion is similar to the "putc and EOF" one going on elsewhere.
The fundamental problem is NULL should ideally be an "out-of-band"
value, but because on the vast majority of architectures pointers are
implemented with the semantics of unsigned integers, and "out-of-band"
value is impossible, and hence the compulsion to use a perfectly
defined address (zero) as the internal representation of storing NULL
in a pointer. Zero is a perfectly valid memory address.

If pointers are implemented as being identical signed integers then one
could use the value -1 for NULL. Another option would be to use the
largest representable value for NULL. An address like 4294967295 is not
used as often as address zero.

Aug 7 '08 #26
Keith Thompson wrote, On 07/08/08 04:50:
bl********@gishpuppy.com (blargg) writes:
>In article <g7**********@pc-news.cogsci.ed.ac.uk>, ri*****@cogsci.ed.ac.uk
(Richard Tobin) wrote:
>>[...]
You might consider the language incompatible with the architecture if
it said anything about what happened when address 0 was accessed, but
C doesn't do that. There is no necessary connection between C's null
pointer and the architecture's address 0, though they are the same in
most implementations. In particular, there's no reason that

(volatile unsigned char*)0x00

produce the architecture address 0. It could produce, say, 0xffffffff.
[...]
If one needed a pointer of address 0 in such a case, say where an
architecture had a register at 0 and whose null pointer was non-zero, one
subtract 1 from a char* of address 1:

char* addr_0 = (char*) 1 - 1;

That's one approach. Another is to use a non-constant expression:

int non_constant_zero = 0;
char *addr_0 = (char*)non_constant_zero;

(You could even declare non_constant_zero as const if you wanted to
confuse your readers.)
However, I would say the best approach is to read the examples in the
manual for that particular implementation and use whatever method they
use (which might be a non-standard header providing a suitable pointer
constant). After all, you are trying to do something non-portable so
whatever non-portable method the implementation documentation suggests
will not reduce portability.
--
Flash Gordon
Aug 7 '08 #27
Flash Gordon wrote:
) Keith Thompson wrote, On 07/08/08 04:50:
)That's one approach. Another is to use a non-constant expression:
)>
) int non_constant_zero = 0;
) char *addr_0 = (char*)non_constant_zero;
)>
)(You could even declare non_constant_zero as const if you wanted to
)confuse your readers.)
)
) However, I would say the best approach is to read the examples in the
) manual for that particular implementation and use whatever method they
) use (which might be a non-standard header providing a suitable pointer
) constant). After all, you are trying to do something non-portable so
) whatever non-portable method the implementation documentation suggests
) will not reduce portability.

Almost always, there is a header defining all the hardware addresses. This
even makes it a bit more portable should you decide to switch to a platform
with the same registers at different memory locations.

I remember on the atmels there were include files for every single chip.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Aug 7 '08 #28
Willem wrote, On 07/08/08 09:09:
Flash Gordon wrote:
) Keith Thompson wrote, On 07/08/08 04:50:
)That's one approach. Another is to use a non-constant expression:
)>
) int non_constant_zero = 0;
) char *addr_0 = (char*)non_constant_zero;
)>
)(You could even declare non_constant_zero as const if you wanted to
)confuse your readers.)
)
) However, I would say the best approach is to read the examples in the
) manual for that particular implementation and use whatever method they
) use (which might be a non-standard header providing a suitable pointer
) constant). After all, you are trying to do something non-portable so
) whatever non-portable method the implementation documentation suggests
) will not reduce portability.

Almost always, there is a header defining all the hardware addresses. This
even makes it a bit more portable should you decide to switch to a platform
with the same registers at different memory locations.

I remember on the atmels there were include files for every single chip.
I would assume that the examples in the manuals used those header files,
so that is covered by my suggestion :-)

Actually, I've come across similar things and that is one reason why I
suggested referring to the documentation for the best method to solve a
problem that cannot be solved portably.
--
Flash Gordon
Aug 10 '08 #29

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

Similar topics

2
by: grigoo | last post by:
bonjour a tous je me presente a vous::: greg dit le grigoo sur le web ,,etudiant en bioinformatique a montreal et jusqu au cou dans notre language prefere....java. et biojava.. et je suis en un...
0
by: Laurent Pointal | last post by:
Bienvenue sur la liste Python francophone, hébergée par l'AFUL, ou sur le newsgroup fr.comp.lang.python ("fclp"). Votre abonnement ŕ cette liste de diffusion ou votre lecture de fclp montrent...
0
by: Michael Dyson | last post by:
M. Michael DYSON DIRECTEUR-ADJOINT SOCIÉTÉ DE SÉCURITÉ SARL. TEL.00229 20 21 80 Cotonou République du Bénin Email:michaeldyson2005@latinmail.com Bonjour . Je sais que mon message sera d’une...
0
by: Alejandro Scomparin | last post by:
FORMULACION, PREPARACION Y EVALUACION ECONOMICA Y FINANCIERA DE PROYECTOS DE TECNOLOGIA INFORMATICA (IT) en la UTN Inicia: 17 de Mayo. 19 hs Dirigido a Esta preparado para gerentes, jefes,...
0
by: Alejandro Scomparin | last post by:
FORMULACION, PREPARACION Y EVALUACION ECONOMICA Y FINANCIERA DE PROYECTOS DE TECNOLOGIA INFORMATICA (IT) en la UTN Inicia: 17 de Mayo. 19 hs Dirigido a Esta preparado para gerentes, jefes,...
0
by: gandalf | last post by:
Con motivo del enorme esfuerzo realizado para visitar la ciudad argentina de Córdoba, participar en la Reunión del MERCOSUR, en la clausura de la Cumbre de los Pueblos en la histórica Universidad...
1
by: crow | last post by:
http://www.pagina12.com.ar/diario/elpais/1-72984-2006-09-14.html Por Miguel Bonasso Desde La Habana Me había preparado para verlo, pero la realidad fue mucho más fuerte. Incluso le llevaba de...
1
by: gandalf | last post by:
CON LA PASION DE SIEMPRE HABLO DE CHAVEZ, DE LA MEDICINA CUBANA... Y DE SU PROPIA MUERTE Relato de la nueva gran batalla de Fidel El líder cubano mostró cómo evoluciona su recuperación en el...
1
by: Sebastien | last post by:
Bonjour, Je tient d'abort a m'excuser de poster cette demande ici, mais je ne vois pas tres bien ou fair cette demande, Je développe un logiciel de génération de code (Génération de Code VB et...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.