Re: decrement past beginning is valid?
From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 11/05/04
- Next message: Chris \( Val \): "Re: how to cut a char out of a string"
- Previous message: Mike Wahler: "Re: (C) missing semi-colon"
- In reply to: Watson Davis: "Re: decrement past beginning is valid?"
- Next in thread: Mike Wahler: "Re: decrement past beginning is valid?"
- Reply: Mike Wahler: "Re: decrement past beginning is valid?"
- Reply: Watson Davis: "Re: decrement past beginning is valid?"
- Reply: Mark P: "Re: decrement past beginning is valid?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Chris \( Val \): "Re: how to cut a char out of a string"
- Previous message: Mike Wahler: "Re: (C) missing semi-colon"
- In reply to: Watson Davis: "Re: decrement past beginning is valid?"
- Next in thread: Mike Wahler: "Re: decrement past beginning is valid?"
- Reply: Mike Wahler: "Re: decrement past beginning is valid?"
- Reply: Watson Davis: "Re: decrement past beginning is valid?"
- Reply: Mark P: "Re: decrement past beginning is valid?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|