Questions tagged [sequence-points]

Points in a program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation. (from Wikipedia)

This tag should be used whenever the question is related to problems that are (eventually found to be) caused by ambiguous execution of subexpressions, which results in the difference of the expected result by novice programmers.

189 questions
1066
votes
5 answers

Undefined behavior and sequence points

What are "sequence points"? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i;, to make myself feel better. Why should I stop using them? If you've read this, be sure…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
906
votes
15 answers

Why are these constructs using pre and post-increment undefined behavior?

#include int main(void) { int i = 0; i = i++ + ++i; printf("%d\n", i); // 3 i = 1; i = (i++); printf("%d\n", i); // 2 Should be 1, no ? volatile int u = 0; u = u++ + ++u; printf("%d\n", u); // 1 u = 1; …
PiX
  • 9,705
  • 4
  • 19
  • 11
85
votes
5 answers

Undefined behavior and sequence points reloaded

Consider this topic a sequel of the following topic: Previous installment Undefined behavior and sequence points Let's revisit this funny and convoluted expression (the italicized phrases are taken from the above topic *smile* ): i += ++i; We…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
57
votes
10 answers

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

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…
ashfaque
  • 522
  • 4
  • 8
54
votes
3 answers

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

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…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
37
votes
3 answers

Order of evaluation in v != std::exchange(v, predecessor(v))

I keep finding more idioms that lend themselves to std::exchange. Today I found myself writing this in an answer: do { path.push_front(v); } while (v != std::exchange(v, pmap[v])); I like it a lot more than, say do { path.push_front(v); …
sehe
  • 374,641
  • 47
  • 450
  • 633
37
votes
4 answers

sequence points in c

A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have…
Jagan
  • 4,649
  • 19
  • 60
  • 70
35
votes
6 answers

Is value of x*f(x) unspecified if f modifies x?

I've looked at a bunch of questions regarding sequence points, and haven't been able to figure out if the order of evaluation for x*f(x) is guaranteed if f modifies x, and is this different for f(x)*x. Consider this code: #include int…
Navin
  • 1,401
  • 10
  • 16
33
votes
3 answers

Is the `this` argument evaluated before or after other member function arguments?

In the following code a member function set() is called on a model, which is a null pointer. This would be undefined behavior. However, the parameter of the member function is a result of another function call that checks whether the model is a null…
mentalmushroom
  • 2,261
  • 1
  • 26
  • 34
31
votes
3 answers

Does the definition int a = 0, b = a++, c = a++; have defined behavior in C?

Does the definition int a = 0, b = a++, c = a++; have defined behavior in C? Or almost equivalently, does the comma in an object definition introduce a sequence point as for the comma operator in expressions? Similar questions have been asked for…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
30
votes
7 answers

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

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,…
GodsCrimeScene
  • 1,260
  • 2
  • 17
  • 35
27
votes
6 answers

Is this code well-defined?

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…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
24
votes
4 answers

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

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.
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
23
votes
2 answers

Is the comma in a variable list a sequence point?

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…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
23
votes
4 answers

Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?

Given the following program: #include int main(void) { int i = 1, j = 2; int val = (++i > ++j) ? ++i : ++j; printf("%d\n", val); // prints 4 return 0; } The initialization of val seems like it could be hiding some…
max1000001
  • 314
  • 1
  • 13
1
2 3
12 13