On Fri, 18 Jun 2010, flameice9 wrote:
> On Jun 18, 4:31*am, Ian Collins <ian-n...@hotmail.com> wrote:
>> On 06/18/10 09:28 PM, flameice9 wrote:
>>
>>> Does the following program invoke undefined behaviour?
>>
>>> int x;
>>> int f(void) {
>>> * *x = 1;
>>> * *return 2;
>>> }
>>> int main(void) {
>>> * *x = f();
>>> }
>>
>> No.
>
> The standard says only that the updating of the stored value of the left
> operand of the = operator is performed between the previous and next
> sequence point. What stops an implementation from noticing f always
> returns 2 and deciding to perform this update prior to the call to f?
> Worse, what stops an implementation from deciding to perform this update
> "at the same time" as the x = 1 expression inside f, thus modifying x
> twice between sequence points?
An implementation must comply with the workings of the abstract machine.
In main(), x can't be assigned to until f() is evaluated (= until f()
returns). When f() returns, the "x = 1" assignment is complete. (There is
a sequence point after that expression in f().) In main(), you need the
value returned by f() to evaluate the assignment operator. At that time,
"x = 1" is already complete.
Somewhat related:
http://www.open-std.org/Jtc1/sc22/wg...cs/dr_087.html
lacos