sequence-points

Undefined behavior in c/c++: i++ + ++i vs ++i + i++ [duplicate]

风格不统一 提交于 2019-11-29 17:53:48
This question already has an answer here: Why are these constructs using pre and post-increment undefined behavior? 14 answers Imagine that we have the code below: int i = 1; int j = i++ + ++i; I know that this is a Undefined Behavior, because before the semicolon, which is a sequence point, the value of i has been changed more than once. It means that the compiler may have two possibilities even if the precedence of operator plus is Left-to-Right: case 1) take the value of i++ --- value of i is 1 take the value of ++i --- value of i is 2 do the operator plus and assign the result which is 3

Where do sequence points come from?

不打扰是莪最后的温柔 提交于 2019-11-29 10:01:05
I know that writing something like ++a = a++; Is not only unreadable but also violates the c/c++ sequence points. Where do these limitations come from? How can one see those 'problems' before finding them as bugs? Cheers and hth. - Alf Basically there is a C++03 sequence point between each statement. For more information see the SO C++ FAQ . For even more information do consult the C++ standard, and keep in mind that in the C++11 standard sequence points were replaced with sequenced before and sequenced after relations. To avoid problems, simply don't try to be too clever about doing a lot in

Why does increment operation like “a[i] = i++;” result in undefined behavior? [duplicate]

为君一笑 提交于 2019-11-29 08:24:30
Possible Duplicate: Undefined Behavior and Sequence Points #include <iostream> using namespace std; int main() { int x[3] = {}; int i=0; x[i] = i++; cout << x[0] << " " << x[1] << endl; return 0; } Codepad is giving me this: Line 9: warning: operation on 'i' may be undefined Why is the operation undefined? Clearly explained here: C-Faq Why doesn't this code: a[i] = i++; work? The subexpression i++ causes a side effect--it modifies i' s value--which leads to undefined behavior since i is also referenced elsewhere in the same expression. There is no way of knowing whether the reference will

Ternary operator and Sequence Points in C

耗尽温柔 提交于 2019-11-29 07:16:32
I've an expression of the form shown below :- while (count) { ... ... index = ((count == 20)? 0 : index++); ... ... } Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part. Is this understanding correct and as such will this statement lead to undefined behaviour ? Right. There's a sequence point after the evaluation of the condition, but the next sequence point is the semicolon terminating the statement. So whenever count != 20 , you have the undefined behaviour index = index++; since index is modified twice without intervening sequence

Which compilation flags should I use to avoid run time errors

≯℡__Kan透↙ 提交于 2019-11-29 02:12:18
Just learned here that -Wsequence-point comiplation flag will pop a warning when the code can invoke UB. I tried it on a statement like int x = 1; int y = x+ ++x; and it worked very nicely. Until now I have compiled with gcc or g++ only using -ansi -pedantic -Wall . Do you have any other helpful flags to make the code more safe and robust? gsamaras As alk summed up, use these flags: -pedantic -Wall -Wextra -Wconversion First, I think you don't want to use the -ansi flag, as suggested in Should I use "-ansi" or explicit "-std=..." as compiler flags? Secondly, -Wextra seems to be quite useful

Why is a = (a+b) - (b=a) a bad choice for swapping two integers?

依然范特西╮ 提交于 2019-11-28 16:48:20
I stumbled into this code for swapping two integers without using a temporary variable or the use of bitwise operators. int main(){ int a=2,b=3; printf("a=%d,b=%d",a,b); a=(a+b)-(b=a); printf("\na=%d,b=%d",a,b); return 0; } But I think this code has undefined behavior in the swap statement a = (a+b) - (b=a); as it does not contain any sequence points to determine the order of evaluation. My question is: Is this an acceptable solution to swap two integers? haccks No. This is not acceptable. This code invokes Undefined behavior . This is because of the operation on b is not defined. In the

Are there sequence points in the expression a^=b^=a^=b, or is it undefined?

谁说我不能喝 提交于 2019-11-28 13:20:16
The allegedly "clever" (but actually inefficient) way of swapping two integer variables, instead of using temporary storage, often involves this line: int a = 10; int b = 42; a ^= b ^= a ^= b; /*Here*/ printf("a=%d, b=%d\n", a, b); But I'm wondering, compound assignment operators like ^= are not sequence points, are they? Does this mean it's actually undefined behavior? a ^= b ^= a ^= b; /*Here*/ It is undefined behavior. You are modifying an object ( a ) more than once between two sequence points. (C99, 6.5p2) "Between the previous and next sequence point an object shall have its stored value

Where do sequence points come from?

前提是你 提交于 2019-11-28 03:16:09
问题 I know that writing something like ++a = a++; Is not only unreadable but also violates the c/c++ sequence points. Where do these limitations come from? How can one see those 'problems' before finding them as bugs? 回答1: Basically there is a C++03 sequence point between each statement. For more information see the SO C++ FAQ. For even more information do consult the C++ standard, and keep in mind that in the C++11 standard sequence points were replaced with sequenced before and sequenced after

Post Increment with respect to Sequence Points

喜夏-厌秋 提交于 2019-11-28 02:27:43
When does the post increment operator affect the increment? I have come across two opinions: 1) From http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_015.htm : POST means do the operation after any assignment operation. 2) Closer home, an answer on SO (albeit on C++) says: ... that delays the increment until the end of the expression (next sequence point). So does the post increment operation... A) wait until a sequence point is reached or B) happen post an assignment operator or C) happen anytime before the sequence point? The correct interpretation is C, ie. the increment happens

Why does increment operation like “a[i] = i++;” result in undefined behavior? [duplicate]

元气小坏坏 提交于 2019-11-28 01:48:29
问题 Possible Duplicate: Undefined Behavior and Sequence Points #include <iostream> using namespace std; int main() { int x[3] = {}; int i=0; x[i] = i++; cout << x[0] << " " << x[1] << endl; return 0; } Codepad is giving me this: Line 9: warning: operation on 'i' may be undefined Why is the operation undefined? 回答1: Clearly explained here: C-Faq Why doesn't this code: a[i] = i++; work? The subexpression i++ causes a side effect--it modifies i' s value--which leads to undefined behavior since i is