Re: Null pointers

From: RCollins (rcoll_at_nospam.theriver.com)
Date: 08/04/04


Date: Tue, 03 Aug 2004 19:59:31 -0700


Jack Klein wrote:
> 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.
>

Er .. did't you just say the same thing Malcolm said? Or am I reading
you wrong?

> 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.
>

True; there are also machines where all-bits-0 is an invalid address,
and even machines (extinct, to my knowledge) where very low addresses
(including all-bits-0) represented actual register locations. However,
on all of these machines, it is _still_ up to the compiler to map a
constant 0 value to the NULL address (however it may be represented
internally).

Cheers,
Ron

-- 
Ron Collins


Relevant Pages

  • Re: Reddit rewritten in Python
    ... >> Or a different version of CL, that has MP on both machines! ... > use different operating systems for development and deployment. ... is that by running your software on lots of different platforms from ... specifications of the language or library they are using. ...
    (comp.lang.lisp)
  • Re: Reddit rewritten in Python
    ... >>> Or a different version of CL, that has MP on both machines! ... >> use different operating systems for development and deployment. ... > is that by running your software on lots of different platforms from ... > the very beginning you smooth out portability problems early. ...
    (comp.lang.lisp)
  • Re: Question about platform independent code and ANSI C compliant code
    ... exclude the plethora of implementations that don't offer ... int b=123456; ... 'a' may end up as 57920 on some machines, ... Which brings me to the point that even on platforms that ...
    (comp.programming)
  • Re: [Linux-fbdev-devel] Generic VESA framebuffer driver and Video card BOOT?
    ... Many embedded platforms do not map VGA resources in, ... Then there are Macintosh machines that also do not map VGA resources. ... graphics mode within 1-2 seconds from poweron, ...
    (Linux-Kernel)
  • Re: Null pointers
    ... >> zero is a valid location, and you can safely read and write from it. ... However most platforms won't allow this. ... actually several common desk top operating systems that you happen ... you can access memory at address "all bits 0". ...
    (comp.lang.c)