sequence-points

Undefined behavior and sequence point

丶灬走出姿态 提交于 2019-12-22 08:24:45
问题 From past few days I was trying to learn about undefined behavior. Few days ago I found a c-faq link. This helps a lot to clear many confusions, but creates an another big confusion when I read the question #3.8. After my lots of efforts to understand the statement (specially second sentence); The Standard states that Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall

Sequence points when calling functions in C and undefined/unspecified behaviour

北城以北 提交于 2019-12-21 17:37:33
问题 I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating the arguments for g and h (so we're not modifying i twice between sequence points), but the order of evaluation of the arguments of f is still unspecified. Is my understanding correct? #include <stdio.h> int g(int i) { return i; } int h(int i) {

Sequencing among a variadic expansion

混江龙づ霸主 提交于 2019-12-21 15:42:33
问题 For this non-variadic example: int Func1(); double Func2(); void MyFunc( int, double ); int main() { MyFunc( Func1(), Func2() ); //... } it's not specified whether Func1() or Func2() is computed first, just that both must be done before MyFunc() is called. How does this sequencing work with the expansion of variadic arguments? template < typename Func, typename ...Args > void MyFunc2( Func &&f, Args&& ...a ) { int b[] = { f( std::forward<Args>(a) )... }; //... } Let's say that f is a function

Sequencing among a variadic expansion

亡梦爱人 提交于 2019-12-21 15:39:28
问题 For this non-variadic example: int Func1(); double Func2(); void MyFunc( int, double ); int main() { MyFunc( Func1(), Func2() ); //... } it's not specified whether Func1() or Func2() is computed first, just that both must be done before MyFunc() is called. How does this sequencing work with the expansion of variadic arguments? template < typename Func, typename ...Args > void MyFunc2( Func &&f, Args&& ...a ) { int b[] = { f( std::forward<Args>(a) )... }; //... } Let's say that f is a function

Is one side of an assignment sequenced before the other in c++?

浪子不回头ぞ 提交于 2019-12-21 11:03:13
问题 I understand that this is undefined behavior: int i = 0; int a[4]; a[i] = i++; //<--- UB here because the order of evaluation of i for the left hand side and the right hand side are undefined (the ; is the only sequence point). Taking that reasoning a step further it seems to me that this would be undefined unspecified behavior: int i = 0; int foo(){ return i++; } int main(){ int a[4]; a[i] = foo(); return 0; } Even though there are a few sequence points on the right hand side of the = as far

Is “*p = ++(*q)” undefined when p and q point to the same object?

℡╲_俬逩灬. 提交于 2019-12-21 03:42:38
问题 after reading about sequence points, I learned that i = ++i is undefined. So how about this code: int i; int *p = &i; int *q = &i; *p = ++(*q); // that should also be undefined right? Let's say if initialization of p and q depends on some (complicated) condition. And they may be pointing to same object like in above case. What will happen? If it is undefined, what tools can we use to detect? Edit: If two pointers are not supposed to point to same object, can we use C99 restrict? Is it what

Sequence point within assignment operators

末鹿安然 提交于 2019-12-19 10:08:28
问题 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.

Sequence point after a return statement?

跟風遠走 提交于 2019-12-19 09:22:36
问题 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

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

旧城冷巷雨未停 提交于 2019-12-18 09:45:25
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 2 years ago . 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

Argument evaluation order between chained static function calls

旧时模样 提交于 2019-12-18 08:59:03
问题 I am curious why there is a difference in the argument evaluation order between chained static functions and member functions. From the answers at this question I can see it is unspecified what the argument evaluation order is between such chained function calls. Take for example the following snippet: #include <iostream> class test { public: static test& chain_s(test& t, int i) { std::cout << i << " "; return t; } test& chain(test& t, int i) { std::cout << i << " "; return *this; } }; int