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
22
votes
1 answer

What should the result be when assigning a variable to a reference to itself, in-between modified and then returned by a function call?

#include int& addOne(int& x) { x += 1; return x; } int main() { int x {5}; addOne(x) = x; std::cout << x << ' ' << addOne(x); } I'm currently in the middle of learning about lvalues and rvalues and was experimenting…
Dappster
  • 322
  • 2
  • 8
21
votes
2 answers

is i=f(); defined when f modifies i?

Related question: Any good reason why assignment operator isn't a sequence point? From the comp.lang.c FAQ I would infer that the program below is undefined. Strangely, it only mentions the call to f as a sequence point, between the computation of…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
21
votes
2 answers

Does *&++i cause undefined behaviour in C++03?

In another answer it was stated that prior to C++11, where i is an int, then use of the expression: *&++i caused undefined behaviour. Is this true? On the other answer there was a little discussion in comments but it seems unconvincing.
M.M
  • 138,810
  • 21
  • 208
  • 365
20
votes
4 answers

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

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…
gjain
  • 4,468
  • 5
  • 39
  • 47
19
votes
2 answers

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

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; …
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
18
votes
5 answers

Behaviour of arr[i] = i++ and i = i + 1 statements in C and C++

In the C and C++ languages, the arr[i] = i++; statement invokes undefined behavior. Why does the statement i = i + 1; not invoke undefined behavior?
Jayesh
  • 4,755
  • 9
  • 32
  • 62
18
votes
1 answer

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

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…
17
votes
2 answers

Bit-fields and sequence points

For an implementation that packs f0 and f1 into the same byte, is the program below defined? struct S0 { unsigned f0:4; signed f1:4; } l_62; int main (void) { (l_62.f0 = 0) + (l_62.f1 = 0); return 0; } I am interested…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
17
votes
1 answer

Shift operands sequenced in C++17

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, every value computation and side-effect of E1 is sequenced…
mch
  • 9,424
  • 2
  • 28
  • 42
16
votes
1 answer

Is moving twice in a single full expression allowed

Assume one has a function with the following prototype template std::unique_ptr process_object(std::unique_ptr ptr); The function may return (a moved version of) the object that was passed to it, or a completely different…
levzettelin
  • 2,600
  • 19
  • 32
16
votes
3 answers

C++11 without sequence point?

Wikipedia says that sequence points are deprecated in C++11. What does that mean? Does that mean that undefined behaviors due to sequence points has no effects?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
15
votes
6 answers

Is there a sequence point between these assignments?

Is there a sequence point between the two assignments in the following code: f(f(x=1,1),x=2);
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
15
votes
2 answers

Is there a sequence point between a function call returning an object and a method call on that object?

If I write f(x)->g(args, ...) can I rely on a sequence point after f(x) before the evaluation of args, ...? I can see arguments both ways: §1.9.17 "When calling a function (whether or not the function is inline), there is a sequence point after…
Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
14
votes
4 answers

Sequence points and side effects: Quiet change in C11?

C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between…
mafso
  • 5,433
  • 2
  • 19
  • 40
14
votes
1 answer

Why I got "operation may be undefined" in Statement Expression in C++?

to describe the problem simply, please have a look at the code below: int main() { int a=123; ({if (a) a=0;}); return 0; } I got this warning from [-Wsequence-point] Line 4: warning: operation on 'a' may be undefined my g++ version is…
FLanS39
  • 143
  • 1
  • 6
1
2
3
12 13