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
1
vote
1 answer

Passing sequence of 2D points as arguments to cvFitLine

I have the following code fragment: int count = (int)sizes.size(); CvSeq* seq = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint2D32f), memStorage); float line[4]; for (int i=0;i
haynar
  • 5,961
  • 7
  • 33
  • 53
1
vote
1 answer

Is assignment operator a sequence point under C++17? and what would be the result of this expression?

It is recommended not to modify an object more than once in a single expression nor using it after modifying it in the same expression. int i = 0; ++++i; // UB ++i = i++; // OK? I think that the last expression was UB before C++17 standard but…
Maestro
  • 2,512
  • 9
  • 24
1
vote
1 answer

C order of evaluation: function designator vs actual arguments

It is well known that the evaluation order of actual arguments varies from one C compiler to the other. But as ISO 9899:1999 states in §6.5.2.2.10: The order of evaluation of the function designator, the actual arguments, and subexpressions within…
Rochus
  • 69
  • 6
1
vote
1 answer

Is the result of indirectly mutating the same value twice per statement undefined?

As far as I know, C++ code like foo(++i, ++i) yields undefined behavior because it mutates i twice per "sequence point" (by the way, what's the new term for it?). But what if the same happens indirectly? Here's an example: #include…
passing_through
  • 1,778
  • 12
  • 24
1
vote
1 answer

Undefined behavior warning on self-assignment comma operator operand in fold expression

I'm getting a warning from GCC 10.1 about possible undefined behavior. Clang 10 does not warn. warning: operation on 'init' may be undefined [-Wsequence-point] | (x = ... = (init = fold_op(init, elements), 0)); | …
1
vote
0 answers

Is i = v[i++] legal in C++17?

I understand that the expression i = v[i++] causes undefined behavior pre-C++17, since we are not sure which action takes place first: the result of i++ being put back to i, or the value of v[i] being assigned to i. But things seem to have…
megamonium
  • 463
  • 3
  • 7
1
vote
2 answers

Understanding Post increment concept in C

Consider code below: #include int main() { int x=0,y=5; printf("x=%d,x_1=%d,sum=%d",x++,x,y+x); return 0; } My assumption on this code was that, x would be printed as 0 and later on postincrement x_1 would be 1 and y+x be…
Gayathri
  • 1,776
  • 5
  • 23
  • 50
1
vote
1 answer

Does using an object in conditional operator twice yield UB?

As I've learned that the conditional operator (ternary operator "?") guarantees the order of the evaluation of its operands I want to know whether assigning to a variable the return of ? where this variable is used in one of its two exprs. be a UB…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
1
vote
3 answers

Is each conversion associated with an input/out format specifier a Sequence Point in C?

Wikipedia entry on Sequence Points in C has this Sequence point: After each conversion associated with an input/output format specifier. For example, in the expression printf("foo %n %d", &a, 42), there is a sequence point after the %n is…
1
vote
1 answer

Function parameters: intedeterminately sequenced or unsequenced?

On cppreference I see the following text: In a function call, value computations and side effects of the initialization of every parameter are indeterminately sequenced with respect to value computations and side effects of any other…
1
vote
3 answers

Undefined behavior in c/c++: i++ + ++i vs ++i + i++

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…
Yves
  • 11,597
  • 17
  • 83
  • 180
1
vote
3 answers

Is this program having any sequence point issues?

#include int main() { int i=7,j; j=(i++,++i,j*i); return 0; } j=(i++,++i,j*i);Is this well defined ? Let me clear my doubt.
Jagan
  • 4,649
  • 19
  • 60
  • 70
1
vote
3 answers

Confused about spline interpolation in 3D space

I have to confess that I am really confused about the available code and algorithms for 3D spline interpolation. For my application I need a path: given some point, defined in 3D space, I need to interpolate them using a spline function (cubic,…
Dave
  • 349
  • 4
  • 22
0
votes
3 answers

Associativity and Sequence Points in C

Since the associativity of '?' is from right to left,any 2 consecutive '?' operators must be treated as such,Right? Now, int x=-1; int y=x?x++?x:-1:1; I expect this to be executed as: int y = x ? (x++?x:-1) : 1; Now since its being executed from…
nikel
  • 3,402
  • 11
  • 45
  • 71
0
votes
0 answers

Why does pre-increment not work for an overflow check, but post-increment does?

I attempted to write a function that increments an unsigned integer and checks if the result overflowed, by comparing the old value of the integer with the value after incrementing: unsigned detect_overflow(unsigned x) { if (x > ++x) { …