views:

155

answers:

4

Hello

Once again, our best loved "i=i--" -like issues. In C99 we have:

6.5 Expressions #2: Between the previous and next sequence point an object shall have its stored value modified at most once

70) This paragraph renders !!undefined!! statement expressions such as

i = ++i + 1;

But for undefinded behavior there can be variants from random output to "program execution in a documented manner" (c99 3.4.3)

So, the question:

Does gcc document the behavior for i=i++, i=i--, and so on statements?

Actual code is

int main(){int i=2;i=i--;return i;}
+1  A: 

It's left to the back-end implementation to decide what it does. You can use -S and inspect the generated code to determine the exact sequence of events.

Ignacio Vazquez-Abrams
Is there any document about such cases?
osgx
and what is the backend in GCC? Will it differ for x86 and sparc e.g.?
osgx
Undefined behavior means that anything is fair game. The compiler could choose to generate different code on different days of the week.
jamesdlin
@jamesdlin, Yes, complier **may** start the Hanoi game in Emacs on 13th day of month, but it usually doesn't. And for fixed compiler version and platform, the results are the same even for different -O levels
osgx
+5  A: 

GCC does not document this behaviour. The Warning Options page mentions sequence points issues in -Wsequence-point, but does not hint at well-defined sematics for violations.

GCC does have a nice list of C Implementation Defined Behaviour, but I could not find any reference to this issue here either.

schot
I'm not sure how it is now, but in past GCC folks whenever possible were openly pro-standard. IOW, if standard says it is undefined or unspecified so it is in GCC.
Dummy00001
+1  A: 

It is not documented but even it it was, I wouldn't want to read it. You should never rely on what a particular implementation does when running into undefined behavior.

Job
If it will be documented, I can rely, that GCC with some versions will do that , what was documented. Yes, My code will be errnous for any other compiler, but not for GCC
osgx
@osgx: Maybe you could but I think it will cause a lot more trouble than it's worth. It is, for example, very confusing for people reading your code. And why would you want to do something like `i = i--` anyway? You should know what you want it to do so why not just spell it out in proper C++?
Job
this case came to us from our large test set, partly written by our testers. They have ''very'' different level of C language skills.
osgx
@osgx: scary. I'd force them to always compile with `-Wall`, which is a good idea anyhow. This will in particular enable the `-Wsequence-point` that schot mentions.
Jens Gustedt
@osgx Now that I understand the context a little better, I think I can mention the static analyzer that others and I work on (see my bio). It finds a lot of undefined or unspecified behaviors if the analyzed code falls in the right C subset. Conversely, is there a place with more information about the work in which this question occurred to you?
Pascal Cuoq
@Pascal Cuoq, this test pack is private and is under nda. The gcc detects "i=i--" rather good, and I often use "-W -Wall -Wextra -pedantic -ansi" options for gcc. Also.. seems that original test has a data dependence on result of this "i=i--", but, at the same time, will success in both trivial understandings of this construct.
osgx
A: 

why on earth would you want to do that? Seriously. I'm curious.

ddopson
This code is found in compiler test set. This test set works ok for **some** versions of gcc.
osgx
It is undefined behaviour, so any behaviour is valid. If your "compiler test" returns *not valid* for any compiler, then your test is broken, because it should always return *valid*, regardless if the result is 2, 3, 4 or the end of the universe as we know it. We can nit-pick if undefined behaviour has always valid or always invalid results, of course. ;)
Secure
BTW, have you tested that the results are consistent across different optimization levels?
Secure
@Secure, The real test does success for both cases of "i=i--" working. It will do different itteration count, but will success. It is a test for compiler, not for the standard-compliance.In tested gcc versions, result was the same for all tested -O levels.
osgx