avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Wrong excution order in 4.1.1, but not 3.4.5, regress


From: Dave Hansen
Subject: Re: [avr-gcc-list] Wrong excution order in 4.1.1, but not 3.4.5, regression?
Date: Tue, 20 Feb 2007 10:01:48 -0500

From: "Graham Davies" <address@hidden>

David Brown wrote (in part):

... I don't know if the C standard is clear on expressions such as "a = b = c", when some or all of these are volatile ...

It is clear. The assignment operator has right-to-left associativity so b = c is evaluated first. The result of an assignment is the value assigned. So, the value assigned to b is then assigned to a. I can't see how volatile could affect this, but it certainly can't make things less well-defined.

It may be clear to you and me, but I've argued this point unsuccessfully with members of the committee, especially Doug Gwynn. He was emphatic: despite the fact that's what the standard says, it's not what the standard _means_.


At the risk of sounding patronising, I'd say that almost any code with
"a = b = c" expressions is incorrect code because it is far from clear
what you mean, and if volatiles are involved then it is undoubtedly wrong.

You don't sound patronizing, just wrong.

Actually, without volatile, it's pretty clear that the assignments in a=b=c can occur in any order, by the "as-if" rule -- the only sequence point is at the end of the full expression. I have argued that, if b is volatile, not only must a be written after b, but b must be read, and the resulting value stored in a. The exact words in the standard are, "An assignment expression has the value of the left operand after the assignment, but is not an lvalue." (6.5.16p3)


When the code is re-written to actually say what you mean, do you still get a difference?

This is a useful question/suggestion, however, and may shed some light on the problem.

After arguing for some time, I still hold my position, but do not consider it worth fighting for. If I'm in a situation where the order of assignement is important, I'm not going to rely on behavior whose definition is in question to save a line or a couple keystrokes. If I really want the behavior I expect form a=b=c when a and b are volatile, then I make a and b volatile and use "b=c; a=b;"

Moral: If you have volatile variables, treat them with respect.

Regards,
  -=Dave

_________________________________________________________________
The average US Credit Score is 675. The cost to see yours: $0 by Experian. http://www.freecreditreport.com/pm/default.aspx?sc=660600&bcd=EMAILFOOTERAVERAGE





reply via email to

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