Python Success stories

Bob Woodham woodham at cs.ubc.ca
Fri Apr 25 20:01:48 EDT 2008


On 2008-04-24, Istvan Albert <istvan.albert at gmail.com> wrote:
> On Apr 23, 2:08 pm, Bob Woodham <wood... at cs.ubc.ca> wrote:
>
>> x = x++;
>>
>> has unspecified behaviour in C.  That is, it is not specified
>> whether the value of x after execution of the statement is the
>> old value of x or one plus the old value of x.
>
> unspecified means that the result could be anything: old value, old
> value+1, -2993882, "trallalla", core dump, stack overflow etc...

One would certainly hope there are only two possible results, the old value of
x or the incremented value of x.

I first encountered this issue with a C compiler that produced one of those
two results differently depending on the level of optimization requested.
(Ultimately, it boiled down to the issue of whether the compiler allocated x
to a register or as a standard memory reference).  Rather than it being a bug,
I was surprised to discover that the C compiler had not, in fact, violated the
ANSI C standard.

Note that x can be a pointer of arbitrary type.  Thus, it is not beyond the
realm of possibilty that a result different from what the programmer expected
might indeed produce, in the end, -2993882, "trallalla", core dump, stack
overflow etc...

I don't have a copy of the ISO/ANSI C spec at hand.  Harbison and Steele, Jr.,
"C a Reference Manual (4th ed)," section 7.12.1, page 228, state, "In ISO C,
if a single object is modified more than once between successive sequence
points, the result is undefined."  Assuming Harbison and Steele quote the 1990
spec correctly, the word I should have used is "undefined."  Can you live with
that?

Aside:  Yes, the issue is that x = x++; modifies the single object x more than
once between successive sequence points.



More information about the Python-list mailing list