Is something wrong with the compiler ?

Peter Holzer hp at vmars.tuwien.ac.at
Wed Oct 10 01:48:41 AEST 1990


chris at mimsy.umd.edu (Chris Torek) writes:

>In article <1903 at tuvie> hp at vmars.tuwien.ac.at (Peter Holzer) writes:
>>Consider ... a = (a = a + 1) + 1
>>This will compile (unoptimized) into:
>>
>>tmp1 = a + 1
>>a = tmp1	*
>>tmp2 = tmp1 + 1
>>a = tmp2	*
>>
>>where the lines marked with * may or may not be deferred until the next
>>sequence point....  [Second example deleted.] ... Any further comments?

>Yes.  The ANSI standard appears% to allow the compiler to compile it
>as

>	tmp0 = a
>	tmp1 = tmp0 + 1
>	a = rand()	*
>	tmp2 = tmp0 + 2
>	a = tmp2	*

Even in this case the the value of the expression (= tmp2) has the expected
value (original value of a) + 2.

But in the meantime I have found the following sentence in the introduction
to chapter 3.3 (Thanks to Henry Spencer who told me to look there):

	Between the previous and next sequence point an object shall
	have its stored value modified at most once by the evaluation of
	an expression.

Together with the sentence in 1.6

	Permissible undefined behaviour ranges from ignoring the
	situation completely with undefined results [...] to terminating
	a translation or execution [...].

this means that the program might not even compile, or do one or more of
the exciting things proposed for undefined behaviour by various people
in this group.

If however, the compiler decides to be nice to the programmer, and
compiles the program to something that has anything to do with the
source code, I think that after executing the statements:

int a = 0, b;
b = (a = (a = a + 1) + 1);

b will have the value 2, and a will have a random value (but any other
value than 1 or 2 is not very propable).

Thanks to all who answered me, I learned something about C again.

Regards,
	Peter.

--
|    _  | Peter J. Holzer                       | Think of it   |
| |_|_) | Technical University Vienna           | as evolution  |
| | |   | Dept. for Real-Time Systems           | in action!    |
| __/   | hp at vmars.tuwien.ac.at                 |     Tony Rand |



More information about the Comp.lang.c mailing list