bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ast-users] Arithmetic assignment side-effects


From: Roland Mainz
Subject: Re: [ast-users] Arithmetic assignment side-effects
Date: Tue, 6 Aug 2013 04:31:03 +0200

On Sun, Aug 4, 2013 at 12:30 AM, Roland Mainz <roland.mainz@nrubsig.org> wrote:
> On Sun, Aug 4, 2013 at 12:04 AM, Dan Douglas <ormaaj@gmail.com> wrote:
>> Is it specified what the value of x should be after this expression?
>>
>> x=0; : $((x+=x=1))
>>
>> Bash, ksh93, mksh, posh say 1. zsh, dash, busybox say 2. Clang and gcc both
>> throw warnings about it, but both plus icc agree on 2.
>
> Just curious: Is that x86-specific or is the result always the same on
> other architectures, too ? Maybe there is something in ISO C1X/C99
> which actually defines or recommends a specific compiler behaviour.
> Maybe we should ask the "clang" people about the "why ?" ...

pcc (Portable C Compiler) peole answered first:

---------- Forwarded message ----------
From: Szabolcs Nagy <nsz@port70.net>
Date: Sun, Aug 4, 2013 at 5:01 AM
Subject: Re: [Pcc] Fwd: Why do all compilers return |x==2| for |x=0;
x+=x=1; | despite being undefined behaviour ?
To: Roland Mainz <roland.mainz@nrubsig.org>
Cc: pcc@lists.ludd.ltu.se


* Roland Mainz <roland.mainz@nrubsig.org> [2013-08-04 01:11:47 +0200]:
> Does any know why { gcc4.6, Sun Studio, icc, pcc } always return
> |x==2| for |x=0; x+=x=1;| (see forwarded email below) despite gcc's
> warning that it may be undefined behaviour ? Why does none of the
> compilers return |1| ? Is there anything in the ISO C standard which
> mandates this ?

no, it is undefined behaviour in iso c, hence the warning
(c99 6.5p2)

the compilers happen to sequence the evaluation of x=1 and
x on the left side of += the same way

the program could of course output anything or crash or
have the entire thing optimized away
(i suspect a recent compiler with sufficiently high
optimization level would do the later)

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.mainz@nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]