Re: Null pointers

From: Jack Klein (jackklein_at_spamcop.net)
Date: 08/04/04


Date: Tue, 03 Aug 2004 21:25:56 -0500

On Tue, 3 Aug 2004 23:35:14 +0100, "Malcolm"
<malcolm@55bank.freeserve.co.uk> wrote in comp.lang.c:

>
> "Old Wolf" <oldwolf@inspire.net.nz> wrote
> >
> > > No. Zero is never a valid location in C.
> >
> > Chapter and verse? AFAIK there are several architectures where
> > zero is a valid location, and you can safely read and write from it.
> >
> This code is undefined
>
> char *ptr = 0;
>
> *ptr = 1;
>
> it may segfault, or it may write the value 1 to location 0 in memory, or it
> may cause little green eleves to appear on screen.
>
> This code is OK
>
> void foo(char *ptr)
> {
> assert(ptr != 0);
> }
>
> char ch;
>
> foo(&ch);
>
> as long as ch is a valid object, the address may never evaluate to 0 (NULL).
>
> This code is implementation defined
>
> int x = 0;
> char *ptr = (char *) x;
>
> *ptr = 1;
>
> if your platform allows writes to a location represented by all bits zero,
> then it is correct. However most platforms won't allow this.

No, actually several common desk top operating systems that you happen
to be familiar with do not allow this. Unfortunately your desk top
chauvinism is totally incorrect as far as the vast majority of
platforms that run C is concerned.

On most processors, microcontrollers, and DSPs, that have C
implementations, which number orders of magnitude more than your desk
top platforms, you can access memory at address "all bits 0". Whether
there is any utility to doing so, and what might happen if you try to
write there, is another story. But a read access is totally benign,
the memory location exists, and the result is whatever it contains.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: Null pointers
    ... However most platforms won't allow this. ... Unfortunately your desk top ... there are also machines where all-bits-0 is an invalid address, ...
    (comp.lang.c)
  • Re: Turn off ZeroDivisionError?
    ... platforms do exactly what I want for division by zero: ... Python doesn't generate exceptions for other floating point ... case of division by zero. ...
    (comp.lang.python)
  • Re: DateSerial question
    ... "JMorrell" wrote in message ... How does a zero in the last ... It really is the single-most versatile date function I've ever run across. ... How I wish a few of the other platforms I work with had a similar one. ...
    (microsoft.public.access.queries)
  • Re: alignment
    ... union {char a; int i;} u; ... Besides, if it does "work," how can you check that it does the same thing on all the platforms of interest to you? ... On a long-ago platform I learned on, it would zero out ...
    (comp.lang.c)
  • Re: udp.c
    ... >> it may be necessary to have something mapped at page zero ... > Are there any existing platforms that do that in kernel mode? ... doesn't handle this properly and this usually hangs the machine. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)