sequence-points

Is there a sequence point between these assignments?

喜你入骨 提交于 2019-12-03 10:37:17
Is there a sequence point between the two assignments in the following code: f(f(x=1,1),x=2); The relevant quote from the (draft) standard [6.5.2.2, 10] is: The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call. So for your expression, the first argument (in particular the call to f ) could be evaluated before the second argument; e.g.: (x = 1, 1), f <sp> call, (x = 2), f <sp> call Or, it could be evaluated after the second argument; e.g.: (x = 2), (x = 1, 1),

Which issues have you encountered due to sequence points in C and C++?

前提是你 提交于 2019-12-03 10:12:50
问题 Below are two common issues resulting in undefined behavior due to the sequence point rules: a[i] = i++; //has a read and write between sequence points i = i++; //2 writes between sequence points What are other things you have encountered with respect to sequence points? It is really difficult to find out these issues when the compiler is not able to warn us. 回答1: A variation of Dario's example is this: void Foo(shared_ptr<Bar> a, shared_ptr<Bar> b){ ... } int main() { Foo(shared_ptr<Bar>(new

Does int a=1, b=a++; invoke undefined behavior?

冷暖自知 提交于 2019-12-03 01:42:06
Does int a=1, b=a++; invoke undefined behavior? There is no sequence point intervening between the initialization of a and its access and modification in the initializer for b , but as far as I can tell, initialization is not "modification" of the object; an initializer is specified to give the "initial value" of the object. Per 6.7.8 Initialization, paragraph 8: An initializer specifies the initial value stored in an object. and it seems reasonable to take "initial" as being sequenced before any access to the object. Has this issue been considered before, and is there an accepted

Is foo(i++) + foo(i++) undefined in ANSI C?

↘锁芯ラ 提交于 2019-12-01 23:10:20
问题 Here's an example snippet: int i = 4,b; b = foo(i++) + foo(i++); I'm pretty certain it's not undefined, because there is a sequence point before the invocation of foo . However, if I compile the code with the -Wall flag a compiler warning is generated which says warning: operation on 'i' may be undefined . I realize it says may , but I'd just like to double check if I'm correct. 回答1: The behavior is undefined. b = foo(i++) + foo(i++); As you say, there's a sequence point between the

Is foo(i++) + foo(i++) undefined in ANSI C?

和自甴很熟 提交于 2019-12-01 21:22:22
Here's an example snippet: int i = 4,b; b = foo(i++) + foo(i++); I'm pretty certain it's not undefined, because there is a sequence point before the invocation of foo . However, if I compile the code with the -Wall flag a compiler warning is generated which says warning: operation on 'i' may be undefined . I realize it says may , but I'd just like to double check if I'm correct. The behavior is undefined. b = foo(i++) + foo(i++); As you say, there's a sequence point between the evaluation of the first i++ and the call to foo , and likewise between the evaluation of the second i++ and the call

Shift operands sequenced in C++17

大憨熊 提交于 2019-12-01 15:12:44
I read in the C++17 Standard $8.5.7.4: The expression E1 is sequenced before the expression E2. for shift operators. Also cppreference rule 19 says: In a shift operator expression E1<<E2 and E1>>E2, every value computation and side-effect of E1 is sequenced before every value computation and side effect of E2 But when I try to compile the following code with gcc 7.3.0 or clang 6.0.0 #include <iostream> using namespace std; int main() { int i = 5; cout << (i++ << i) << endl; return 0; } I get the following gcc warning: ../src/Cpp_shift.cpp: In function ‘int main()’: ../src/Cpp_shift.cpp:6:12:

Shift operands sequenced in C++17

て烟熏妆下的殇ゞ 提交于 2019-12-01 14:07:42
问题 I read in the C++17 Standard $8.5.7.4: The expression E1 is sequenced before the expression E2. for shift operators. Also cppreference rule 19 says: In a shift operator expression E1<<E2 and E1>>E2, every value computation and side-effect of E1 is sequenced before every value computation and side effect of E2 But when I try to compile the following code with gcc 7.3.0 or clang 6.0.0 #include <iostream> using namespace std; int main() { int i = 5; cout << (i++ << i) << endl; return 0; } I get

Sequence point within assignment operators

青春壹個敷衍的年華 提交于 2019-12-01 10:34:12
Let's just take for example the specific compound assignment operator ^= . This stackoverflow page says modification of the left operand may have not been done after the evaluation of ^= , and thus making the code a ^= b ^= a ^= b undefined behaivor. But this does not seem to be the case. The standard says in 5.17 [expr.ass] that In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. There are two keypoints in this statement. 1) What does the subject assignment refers to? In my opinion

Sequence point after a return statement?

北战南征 提交于 2019-12-01 08:42:31
In my answer to a question here I explained what happened when postfix ++ was used on a global variable on the same line as a return statement. The informative appendix C of C11 states that there is a sequence point immediately after a return and refers to normative chapter 6.8.6.4, where no text regarding sequence points can be found. Where in the C standard can I find normative text stating that there is a sequence point after a return statement? (I only found normative text stating this for library functions, as a special case, at 7.1.4/3.) C 2011 (draft n1570) 6.8 4: “Each of the following

Why is this Undefined Behavior?

泄露秘密 提交于 2019-12-01 05:42:00
Why does the following given expression invoke undefined behavior? int i = 5; i = (i,i++,i) + 1 My question is influenced by Als' question here Steve Jessop It isn't undefined. Answered here for C, Sequence points and partial order I think the same applies in C++ (and here's my response before I saw that link): The comma operator introduces a sequence point (and constrains to some extent the order in which the expression must be evaluated - left before right), so: the two modifications of i are separated by a sequence point (the second comma). the modification of i in i++ is separated from