Re: decrement past beginning is valid?

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 11/05/04


Date: Fri, 05 Nov 2004 10:10:55 +0100

Watson Davis wrote:
>
> >
> > What do you mean by 'legal'?
>
> What I meant by legal is that a compiler will compile it.

A compiler will also compile:

   int i = 0;

   i = ++i;

yet, the above os not legal. Your definition of 'legal' is flawed.

To the subject: you are still wrong. The language standard (the only
place what defines what C or C++ is), says clearly that incrementing
or decrementing a pointer outside of an array (with the exception of
the one past end element) is illegal. There is nothing more to say
about it, even if it works on most systems.

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

No wonder.
An array is not a pointer. A pointer is not an array.

In certain circumstances an array decays to a pointer to its first
element and array indexing is defined in terms of pointer arithmetic.

I know this is confusing but the simple fact is: arrays and pointers
are different things.

>
> BUT I would never, ever say that anything would work on "all conceivable
> platforms." Ever.

All things given a specific semantic in the language standards document
behave the same way on all platforms. That is: minus compiler errors.

> About anything. I don't care if it's the standard or
> not.

You should care.

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

On all platforms. It is defined that way. Guess what tells us this.
Right: the language standard.

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

Right. But this doesn't change the fact: decrementing the pointer out of the
array is still illegal. Just because you can drive your car over a road
crossing with red lights on at 2am in the morning and nothing special happens
it doesn't mean it is legal to do so.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: Need help to port VAX code to Alpha and to Itaninum
    ... Not really, the original code was wrong, and the compiler was not ... pointer to the start of the array. ... So you are trying to pass a pointer to a pointer to an array where you ... Also start looking at where you can add the "const" modifier to function ...
    (comp.os.vms)
  • Re: gdb not catching out-of-bounds pointer
    ... is also not defined by the C-standard, IOW: writing code in anything ... provided the library writer knows what the compiler writer guarantees ... etc.) that don't point into the same array than I would about the sort ... of pointer aliasing issue that started this sub-thread. ...
    (comp.unix.programmer)
  • Re: lockless file descriptor lookup
    ... It isn't clear whether you want to refresh the fd_ofiles pointer to the ... the array, or the fd'th element. ... This is actually intended to catch cases where the descriptor array has expanded and the pointer to fd_ofiles has changed, or the file has been closed and the pointer at the fd'th element has changed. ... I'm attempting to force the compiler to reload the fd_ofiles array pointer from the fdp structure. ...
    (freebsd-arch)
  • Re: null terminated strings
    ... pointer + 1 will point to the next element in the array. ... What I don't understand is why such a thing was included in any language that's more than an assembler. ... of structures that are not 'natural' the compiler need to generate ADD instructions using sizeof. ...
    (comp.os.vms)
  • 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)