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

Is (a=1)=2 undefined behaviour in C++98?

Similar codes for example (a+=1)%=7;, where a is an int variable. We know that operator += or = is not a sequence point, therefore we have two side-effects between two adjcent sequence points. (we are using cpp98's sequence point rules…
5
votes
2 answers

Does writing the new value form part of the "value computation" of a pre-increment expression, or is it a "side effect"?

Consider the following expression (with declaration for exposition): int n = 42; --n &= 0x01; Does this fall foul of sequencing rules? In my opinion, the pre-increment is needed as part of the "value computation" of the left-hand operand. If this…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
5
votes
4 answers

Operator associativity with 'postfix decrement' and 'logical AND' operators in c

Disclaimer: I don't code like this, I'm just trying to understand how the c language works!!!! The output is 12. This expression (a-- == 10 && a-- == 9) evaluates left-to-right, and a is still 10 at a-- == 10 but a is 9 for a-- == 9. 1) Is there a…
5
votes
1 answer

sequence points in java

Is there a guaranteed sequence of execution of the following java code: int i = getA() + getB(); Is getA() always executed before getB(), as any average person would expect?
milan
  • 2,355
  • 2
  • 23
  • 38
5
votes
2 answers

C - Output explanation of printf("%d %d\n",k=1,k=3);

How to explain the output of the below code: include int main(void) { int k; printf("%d %d\n",k=1,k=3); return 0; } Ideone Link My thinking was that 1 will be assigned to k variable and then 1 would be printed. Similarly 3…
5
votes
3 answers

Why is this Undefined Behavior?

Why does the following given expression invoke undefined behavior? int i = 5; i = (i,i++,i) + 1 My question is influenced by Als' question here
McCoy
  • 59
  • 2
5
votes
2 answers

Is "int i = x++, j = x++;" legal?

Pretty clear in the title, I think. I'm not entirely sure on this, and I can't find a good answer via the Googles (alas, I haven't committed to the fine art of standards-fu), so I ask: int i = x++, j = x++; Is this defined? I am quite sure that i =…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
5
votes
2 answers

c - Why does i = ++i invoke undefined behaviour?

I understand that C uses the notion of sequence points to identify ambiguous computations, and that = operator is not a sequence point. However, I am unable to see any ambiguity in executing the statement i = ++i As per my understanding, this…
5
votes
2 answers

Is something like "for(i=1;i<=10;printf("%d\n";i),i++) valid and UB-free in C?

Are the following two code blocks exactly the same and achieve the same thing?It displays the same thing when I run the program,but I would appreciate some rigorous…
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
5
votes
1 answer

Why are operations on primitive types unsequenced instead of indeterminitely sequenced?

If i is an int, expressions like ++i + ++i are undefined behavior since there are 2 unsequenced modifications of i. However, if i is some int-like class, ++i + ++i instead has indeterminately sequenced modifications, and is therefore defined…
m42a
  • 1,322
  • 2
  • 10
  • 14
5
votes
1 answer

How to get VS or Xcode warning with something like "x = x++"?

In the spirit of undefined behavior associated with sequence points such as “x = ++x” is it really undefined?, how does one get the compiler to complain about such code? Specifically, I am using Visual Studio 2010 and Xcode 4.3.1, the latter for an…
Jim Buck
  • 20,482
  • 11
  • 57
  • 74
5
votes
1 answer

Sequencing among a variadic expansion

For this non-variadic example: int Func1(); double Func2(); void MyFunc( int, double ); int main() { MyFunc( Func1(), Func2() ); //... } it's not specified whether Func1() or Func2() is computed first, just that both must be done…
CTMacUser
  • 1,996
  • 1
  • 16
  • 27
4
votes
6 answers

Which issues have you encountered due to sequence points in C and C++?

Below are two common issues resulting in undefined behavior due to the sequence point rules: a[i] = i++; //has a read and write between sequence points i = i++; //2 writes between sequence points What are other things you have encountered with…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
4
votes
1 answer

Is the output of this C code compiler dependent?

#include int main(void) { int a = 0, b = 1; a = (a = 5) && (b = 0); printf("%d", a); printf("%d", b); } The value of variable a is getting updated twice, which violates the C standards. So I think it will be compiler…
4
votes
3 answers

Will this expression evaluate to true or false (1 or 0) in C?

#include int main() { int a=4; int b=4; int c= a++ < ++b? 1 : 0; printf ("%d",c); } It is known that there is a sequence point at ?, which means that both the prefix and postfix operations have to be completed by that…