sequence-points

In C99, is f()+g() undefined or merely unspecified?

女生的网名这么多〃 提交于 2019-11-27 04:09:48
I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines the functions (which the compiler may decide to do even if the functions are not declared inline ) and then reorders instructions? May one get a result different of the above two? In other words, is this undefined behavior? This is not because I intend to write

Why does the expression a = a + b - ( b = a ) give a sequence point warning in c++?

て烟熏妆下的殇ゞ 提交于 2019-11-27 01:57:50
Following is the test code: int main() { int a = 3; int b = 4; a = a + b - (b = a); cout << "a :" << a << " " << "b :" << b << "\n"; return 0; } Compiling this gives the following warning: > $ g++ -Wall -o test test.cpp test.cpp: In function ‘int main()’: > test.cpp:11:21: warning: operation on ‘b’ may be undefined > [-Wsequence-point] Why can the operation be undefined? According to my understanding, first the subexpression (b = a) should be evaluated because of higher precedence of (), thus setting b = a. Then, since '+' and '-' have same precedence, the expression would be evaluated left

Any good reason why assignment operator isn't a sequence point?

前提是你 提交于 2019-11-26 22:23:54
问题 Is there any good reason for operator = not being a sequence point? Both in C and C++. I have trouble thinking about an counter-example. 回答1: By request: In general, things need a reason to be a sequence point. They don't need a reason not to be a sequence point; that's the default. For example, && must be a sequence point because of short-circuiting behaviour: if the left-hand side is false, the right-hand side must not be evaluated . (This is not just about optimization; the right-hand side

How do Prefix (++x) and Postfix (x++) operations work?

走远了吗. 提交于 2019-11-26 20:17:23
Can someone tell me how prefix / postfix operators really work? I've been looking online a lot but haven't found anything. From what I can tell prefex first increments, then does the operation and then assigns. Postfix would do the operation first, then assign and then increment. But I'm having a bit of trouble with my code: int x, y; x = 1; y = x + x++; // (After operation y = 2)(x=2) However when I do: y = x++ + x; // (After operation y = 3)(x=2) I'm not sure why these operations would be any different. I have two questions: Could you explain the difference? How does this apply to the other

Unsequenced value computations (a.k.a sequence points)

喜你入骨 提交于 2019-11-26 18:58:44
Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior. Given int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; //Expr3 i = v[i++]; //Expr4 I think of the above expressions (in that order) as operator=(i, operator++(i)) ; //Expr1 equivalent operator=(i, operator++(i, 0)) ; //Expr2 equivalent operator++(operator++(i)) ; //Expr3 equivalent operator=(i, operator[](operator++(i, 0)); //Expr4 equivalent Now coming to behaviors here are the important quotes from C++ 0x .

Is the comma in a variable list a sequence point?

孤人 提交于 2019-11-26 16:41:33
问题 In the following type of code is there a sequence point between each variable construction, or is the result undefined? int a = 0; int b = a++, c = a++; I wasn't able to find in the standard a specific reference to a sequence point here. Does that mean it is undefined, or just that I failed in my search? The completion of an expression is a sequence point, but does the above initialization also count? 回答1: I believe behavior is well-defined because of 8[dcl.decl]/3 Each init-declarator in a

Is this code well-defined?

本秂侑毒 提交于 2019-11-26 16:22:31
问题 This code is taken from a discussion going on here. someInstance.Fun(++k).Gun(10).Sun(k).Tun(); Is this code well-defined? Is ++k in Fun() evaluated before k in Sun()? What if k is user-defined type, not built-in type? And in what ways the above function calls order is different from this: eat(++k);drink(10);sleep(k); As far as I know, in both situations, there exists a sequence point after each function call . If so, then why can't the first case is also well-defined like the second one?

Why does the expression a = a + b - ( b = a ) give a sequence point warning in c++?

心不动则不痛 提交于 2019-11-26 09:50:30
问题 Following is the test code: int main() { int a = 3; int b = 4; a = a + b - (b = a); cout << \"a :\" << a << \" \" << \"b :\" << b << \"\\n\"; return 0; } Compiling this gives the following warning: > $ g++ -Wall -o test test.cpp test.cpp: In function ‘int main()’: > test.cpp:11:21: warning: operation on ‘b’ may be undefined > [-Wsequence-point] Why can the operation be undefined? According to my understanding, first the subexpression (b = a) should be evaluated because of higher precedence of

How do Prefix (++x) and Postfix (x++) operations work?

安稳与你 提交于 2019-11-26 07:33:40
问题 Can someone tell me how prefix / postfix operators really work? I\'ve been looking online a lot but haven\'t found anything. From what I can tell prefex first increments, then does the operation and then assigns. Postfix would do the operation first, then assign and then increment. But I\'m having a bit of trouble with my code: int x, y; x = 1; y = x + x++; // (After operation y = 2)(x=2) However when I do: y = x++ + x; // (After operation y = 3)(x=2) I\'m not sure why these operations would

Unsequenced value computations (a.k.a sequence points)

主宰稳场 提交于 2019-11-26 06:44:06
问题 Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior. Given int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; //Expr3 i = v[i++]; //Expr4 I think of the above expressions (in that order) as operator=(i, operator++(i)) ; //Expr1 equivalent operator=(i, operator++(i, 0)) ; //Expr2 equivalent operator++(operator++(i)) ; //Expr3 equivalent operator=(i, operator[]