tags:

views:

558

answers:

8

I read about 'side effect' from this website:

but still not understand why f = f++ considered unsafe ?

Can somebody explain?

A: 

Asked yesterday: yesterday's instance of the same question, which is full of good insight for you, and you should follow along that discussion.

Heath Hunnicutt
this is not a question about pre/post increment.
joshperry
not same question
tsubasa
It is just as much a question about pre/post-increment as the question I linked: both are about the effect of sequence points in regard to pre/post increment.
Heath Hunnicutt
+13  A: 

The problem is Sequence Points. There are two operations in this statment with no sequence point, so there is no defined order to the statement, is the assignment happening first or the increment?

Nothing says it's unsafe, it's just undefined, which means that different implementations may have different results or it may format your hard drive...

joshperry
a person with enough points should close this question as duplicate.
Heath Hunnicutt
@Heath: point to a duplicate (usually in the question comments) and someone will...
dmckee
I think most people would consider formatting a drive an unsafe consequence of `f = f++`. Though I think most people say 'unsafe' not meaning that it may destroy something, but that you can't depend on what it'll do.
Michael Burr
The C and C++ standard quite explicitly use the term "Undefined Behavior" throughout, and specifically in the section that details sequence point execution order. I was merely using hyperbole to accentuate the difference between unsafe and undefined.
joshperry
undefied behavior implies unsafe code
hasen j
In fact it doesn't, it merely specifies undefined. Think about the difference between NULL and 0 (not in C++ obviously). Zero is a value, NULL is the lack of a value. Undefined means _anything_ could result; yes, including unsafe things, but not necessarily.
joshperry
+3  A: 

Using x and x++ (or ++x) within the same statement is undefined behaviour in C. The compiler is free to do whatever it wants: either increment x before doing the assignment, or after that. Taking Ólafur's code, it might yield f == 5 or f == 6, depending on your compiler.

Arthur Reutenauer
+4  A: 

The article at the (cleaned up) link you provided gives the answer. "C makes almost no promise that side effects will occur in a predictable order within a single expression." This means that you don't know in what order the = and the ++ will occur. It's compiler dependent.

If you follow the link from that article to the article about sequence points on the same site, you'll see that the compiler can optimize what and when it writes values back from the registers into the variables.

shoover
One additional bit of clarification - it's not just compiler dependent. What the compiler can do might make no sense at all - even to the point that 2 instances of the same statement (with the same starting value for `f`) could produce entirely different results.
Michael Burr
A: 

Because that expression does not do what you think it will do.

Loadmaster
In other words, you cannot predict what the compiler will do because the operations are unordered.
Loadmaster
A: 

I support Arthur's answer in this respect. Though the implementation of the post incrementing operator i.e f++ is confusing, it is not considered unsafe. U should first understand how the compiler interprets it. whether it will increment f after it encounters a sentence termination (;) or immediately after using the value of f.

Aditya
It is *undefined*, in the sense of the C standard. This one is not a case of "you have to know what the compiler does". The compiler may do anything, including giving a value to f that does not correspond to any of the orderings of the atomic instructions you think it has to generate.
Pascal Cuoq
A: 

Pre-increment is always preffered as it takes one intruction less then Post-increment operator. So in for and while loops mostly pre-increment is preffered. Although there may be case where post increment will be handy but that will depend on program logic, say you want value of variable changes after certain execution of variable on previous value.

Vivek
A: 

From the standard

6.5 (2) If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.74)

74) This paragraph renders undefined statement expressions such as

             i = ++i + 1;
             a[i++] = i;

while allowing

             i = i + 1;
             a[i] = i;
John Bode