Re: decrement past beginning is valid?

From: Watson Davis (watson_at_watsonmusic.com)
Date: 11/05/04


Date: 5 Nov 2004 07:55:17 GMT


"David White" <no@email.provided> wrote in
news:x8Bid.129$ob1.2005@nasal.pacific.net.au:

> "Watson Davis" <watson@watsonmusic.com> wrote in message
> news:Xns9597AC377962Athepencilneckyahooco@206.66.12.201...
>> "David White" <no@email.provided> wrote in
>> news:KExid.113$ob1.1782@nasal.pacific.net.au:
>>
>> > Hmmm. Suppose that on certain machines, including yet-to-be-built
>> > ones if necessary, an automatic pointer is stored in a CPU register
>> > intended specifically for holding a memory address, and suppose the
>> > mere act of storing it there generates an exception if it's an
>> > invalid address, which below the beginning of an array could be,
>> > then I suggest that it would be unwise to increment and decrement
>> > it all over the place.
>>
>> I'm not saying that it's an intelligent thing to do or that it won't
>> cause holy havoc. I'm just saying that it's legal from the
>> compiler's standpoint.
>
> What do you mean by 'legal'?

What I meant by legal is that a compiler will compile it.

>
> I refer to your earlier statement: "It's just a pointer and you can
> pretty much increment and decrement that thing all over the place
> without a problem". Did you mean that this is a guarantee imposed on
> all compilers by the language standard? If not, then what did you
> mean? Anything the standard does not guarantee is obviously not
> necessarily going to work on all conceivable platforms "without a
> problem".

Yes. That's pretty much what I meant. I was wrong in my understanding
that an array is the same as a pointer. In most cases it behaves the
same as a pointer, but it does have differences (that I was unaware of.)

BUT I would never, ever say that anything would work on "all conceivable
platforms." Ever. About anything. I don't care if it's the standard or
not. I've worked on too many different types of machines to believe
that.

Although subscripting out of the bounds of the array is undefined
behavior, the array reference is going to decay into pointer arithmetic
on MOST platforms. If you find yourself doing something like that, then
you're probably being too slick for your own good and you're probably
writing code that is totally not maintainable and should be taken out and
shot.

If you go back to the original poster's question, the code is obviously
being generated on a platform that supports negative subscripting because
the code is working with it.

Watson (the pencil neck) Davis



Relevant Pages

  • Re: decrement past beginning is valid?
    ... > What I meant by legal is that a compiler will compile it. ... > that an array is the same as a pointer. ... behave the same way on all platforms. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: decrement past beginning is valid?
    ... >> I know that incrementing a pointer one past the end of the array is ... It's just a pointer and you can pretty much increment and decrement ... cause your program to abort on some platforms. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Clearing a custom array
    ... This seems to work but is it not the same as saying here is a null pointer ... clean an array using a custom structure? ... The .clear isn't a member of it by default. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: pass an array throuh a function
    ... > I also get a syntax error at this line: ... the array to stand for the array (but actually is is converted to a pointer ... C++ book and read it carefully, remember its probably not saying what you ...
    (comp.lang.cpp)
  • Re: Thou shalt have no other gods before the ANSI C standard
    ... Because of these two platforms ... > The C standard does allow memcpy of pointer objects. ... New compiler development for such platforms is not a poor economic ...
    (sci.crypt)