Re: Null pointers
From: Jack Klein (jackklein_at_spamcop.net)
Date: 08/04/04
- Next message: Jack Klein: "Re: Optimisation techniques"
- Previous message: Keith Thompson: "Re: How to know that stack is growing upward/downward?"
- In reply to: Malcolm: "Re: Null pointers"
- Next in thread: RCollins: "Re: Null pointers"
- Reply: RCollins: "Re: Null pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jack Klein: "Re: Optimisation techniques"
- Previous message: Keith Thompson: "Re: How to know that stack is growing upward/downward?"
- In reply to: Malcolm: "Re: Null pointers"
- Next in thread: RCollins: "Re: Null pointers"
- Reply: RCollins: "Re: Null pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|