httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexei Kosut <ako...@leland.Stanford.EDU>
Subject Re: [Fwd: Problem 2534]
Date Mon, 03 Aug 1998 22:54:56 GMT
On Mon, 3 Aug 1998, Ben Laurie wrote:

> Dirk-Willem van Gulik wrote:
> > I believe that according to the ANSI standard (page 91)
> > if I read it correctly a compiler _may_
> > 
> >         char foo[] = "hello";
> >         char some[] = "one";
> >         char bar[] = "hello";
> > 
> > end up having foo == bar. Or even if you are unlucky

Waitaminute. If this was *foo and *bar, then I could buy this; it makes
sense. But foo[] and bar[] are supposed to allocate arrays on the stack
(six bytes each), and are supposedly writeable. Any compiler that makes
foo == bar is severely wrong, IMHO. Unless you're allowing your optimizing
compiler to play with your stack arrangement, and it should only do that
in this case if it's absofragginglutely sure you're not modifying either
foo or bar. Which isn't a case covered by the ANSI/ISO spec. 

(Recall that arrays and pointers are not the same thing in C, no matter
how alike they may act)

> >         char foo[] = "hello world";
> >         char bar[] = "world";
> > 
> > can lead to foo+6 == bar. When working with Acorn's compile this sort of
> > entertainment could give hours of fun; as the compile would not complain
> > loudly when you did a bar = "indian" and ended up with a foo == "hello
> > indian". :-(.
> 
> Maybe a strcpy(bar,"indian") would do that, but even so, IMO the
> compiler is broken. But there's no way even a broken compiler could do
> what you describe, surely?

Agreed. For one thing, you can't assign a pointer to an array, so bar =
"indian" isn't legal C; bar is not a legal lvalue, it's just another way
of writing &bar[0]. Even presuming that foo* and bar* were meant instead
of foo[] and bar[], we just made the assumption that "world"  == "hello "
+ 6, so "indian" can't possibly be at the same location as "world". With a
strcpy, I could buy it. (although string constants aren't supposed to be
writeable.)

-- Alexei Kosut <akosut@stanford.edu> <http://www.stanford.edu/~akosut/>
   Stanford University, Class of 2001 * Apache <http://www.apache.org> *



Mime
View raw message