Re: Null pointers
From: RCollins (rcoll_at_nospam.theriver.com)
Date: 08/04/04
- Next message: Mabden: "Re: Null pointers"
- Previous message: Kenny McCormack: "O/T: Dead female pop stars (Was: Re: write this program)"
- In reply to: Jack Klein: "Re: Null pointers"
- Next in thread: Malcolm: "Re: Null pointers"
- Reply: Malcolm: "Re: Null pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Mabden: "Re: Null pointers"
- Previous message: Kenny McCormack: "O/T: Dead female pop stars (Was: Re: write this program)"
- In reply to: Jack Klein: "Re: Null pointers"
- Next in thread: Malcolm: "Re: Null pointers"
- Reply: Malcolm: "Re: Null pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|