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
13
votes
4 answers

Are there sequence points in the expression a^=b^=a^=b, or is it undefined?

The allegedly "clever" (but actually inefficient) way of swapping two integer variables, instead of using temporary storage, often involves this line: int a = 10; int b = 42; a ^= b ^= a ^= b; /*Here*/ printf("a=%d, b=%d\n", a, b); But I'm…
Medinoc
  • 6,577
  • 20
  • 42
12
votes
5 answers

Does placement-new introduce a sequence point?

Consider the following line of code: new (p++) T(); If the constructor T() throws an exception, is p guaranteed to have already been incremented?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
12
votes
1 answer

Chained compound assignments with C++17 sequencing are still undefined behaviour?

Originally, I presented a more complicated example, this one was proposed by @n. 'pronouns' m. in a now-deleted answer. But the question became too long, see edit history if you are interested. Has the following program well-defined behaviour in…
Quimby
  • 17,735
  • 4
  • 35
  • 55
12
votes
3 answers

Sequence Points and Method Chaining

The following expression is often used to demonstrate undefined unspecified behaviour: f() + g() If f() and g() both have side effects on some shared object then the behaviour is undefined unspecified because the order of execution is unknown. f()…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
12
votes
5 answers

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

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…
Nyan
  • 2,360
  • 3
  • 25
  • 38
12
votes
3 answers

Sequence Points between printf function args; does the sequence point between conversions matter?

I read here that there is a sequence point: After the action associated with input/output conversion format specifier. For example, in the expression printf("foo %n %d", &a, 42), there is a sequence point after the %n is evaluated before printing…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
11
votes
2 answers

Will i=i++ be newly well-defined in C17?

After stumbling across the question "Why are these constructs using pre and post-increment undefined behavior?" today I decided to grab the newest draft for the next C standard I could find and read more about it. Shortly after I discovered the…
asynts
  • 2,213
  • 2
  • 21
  • 35
11
votes
2 answers

C++ cout side-effect sequencing

Suppose following piece of code: #include using namespace std; char one() { cout << "one\n"; return '1'; } char two() { cout << "two\n"; return '2'; } int main(int,char**) { // 1: cout << one() << '\n' …
sukhmel
  • 1,402
  • 16
  • 29
10
votes
2 answers

Segfault with strcmp

I am using strcmp in following ways Passing char[] array names Passing pointers to string literals but, the second result in seg fault. even though i have confirmed that pointers point to correct string literals, i am confused as to why i am…
Jimm
  • 8,165
  • 16
  • 69
  • 118
10
votes
2 answers

Post-increment and variable re-use in a single statement, Perl 5 vs Perl 6

A line of code that I naively thought would translate fairly literally between Perl 6 and Perl 5 in fact did not, due to differences in how a post-increment variable is being handled. This Perl 6 yields the desired result, a magic square: [[8, 1,…
David Hoekman
  • 101
  • 1
  • 4
10
votes
1 answer

What's the consequence of a sequence-point "immediately before a library function returns"?

In this recent question, some code was shown to have undefined behavior: a[++i] = foo(a[i-1], a[i]); because even though the actual call of foo() is a sequence point, the assignment is unsequenced, so you don't know whether the function is called…
user2371524
10
votes
3 answers

Does the comma in a declaration for multiple objects introduce a sequence point like the comma operator?

I was reading C Programming Language and found this sentence: The commas that separate ... variables in declarations ... are not comma operators, and do not guarantee left to right evaluation. If so, are they comma operators in this code? int a=1,…
9
votes
2 answers

Are there any situations where code would have a sequence point in c++11 but not c++03?

Now that the new c++11 standard has made changes in how sequence points are described I'm trying to find out exactly what has been changed between c++03 and c++11. In particular, are there any situations where code that looks the same would have a…
shuttle87
  • 15,466
  • 11
  • 77
  • 106
9
votes
4 answers

Can a C/C++ compiler legally cache a variable in a register across a pthread library call?

Suppose that we have the following bit of code: #include #include #include void guarantee(bool cond, const char *msg) { if (!cond) { fprintf(stderr, "%s", msg); exit(1); } } bool do_shutdown…
9
votes
4 answers

Behavior of an expression: Defined or Undefined?

I have the following code int m[4]={1,2,3,4}, *y; y=m; *y = f(y++); // Expression A My friend told me that Expression A has a well defined behavior but I am not sure whether he is correct. According to him function f() introduces a sequence…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
1 2
3
12 13