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

Where do sequence points come from?

I know that writing something like ++a = a++; Is not only unreadable but also violates the c/c++ sequence points. Where do these limitations come from? How can one see those 'problems' before finding them as bugs?
8
votes
3 answers

Is there a sequence point between structure member initializations?

Is there a sequence point between structure member initialization expressions? For example, is it well defined that the code bellow will always print "a, b"? #include typedef struct { char *bytes; int position; int length; }…
Jon Hess
  • 14,237
  • 1
  • 47
  • 51
8
votes
2 answers

Is this code well defined?

I suspect the following chaining of functions would result in unspecified sequence according to the C++ standards (assume C++0x). Just want a confirmation and if anyone could provide an explanation, I'd appreciate it. #include struct…
Zach Saw
  • 4,308
  • 3
  • 33
  • 49
8
votes
1 answer

Which compilation flags should I use to avoid run time errors

Just learned here that -Wsequence-point comiplation flag will pop a warning when the code can invoke UB. I tried it on a statement like int x = 1; int y = x+ ++x; and it worked very nicely. Until now I have compiled with gcc or g++ only using…
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
8
votes
5 answers

An explanation about Sequence points

Lately, I have seen a lot of questions being asked about output for some crazy yet syntactically allowed code statements like like i = ++i + 1 and i=(i,i++,i)+1;. Frankly realistically speaking hardly anyone writes any such code in actual…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
8
votes
2 answers

Calling function with side effects inside expression

I thought I understand how sequence points work in C++, but this GeeksQuiz question puzzled me: int f(int &x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; } int main() { int p = 5; cout << f(p, p)…
Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
8
votes
1 answer

Undefined behavior, or: Does Swift have sequence points?

In C/C++, the second statement in int i = 0; int j = i++ + i++ + ++i; invokes both unspecified behavior, because the order of evaluation of operands is unspecified, and undefined behavior, because the side effects on the same object i are…
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
8
votes
3 answers

Does the following code invoke undefined behaviour?

I would like to do something like this #include #include struct Foo {}; using FooPtr = std::unique_ptr; FooPtr makeFoo() { return FooPtr(new Foo()); } struct Baz { Baz(FooPtr foo) : Baz(std::move(foo), bar(foo)) {} …
wakjah
  • 4,541
  • 1
  • 18
  • 23
8
votes
5 answers

how to check that behavior is undefined in c?

I know that the following is undefined because I am trying to read and write the value of variable in the same expression, which is int a=5; a=a++; but if it is so then why the following code snippet is not undefined int a=5; a=a+1; as here also…
OldSchool
  • 2,123
  • 4
  • 23
  • 45
7
votes
3 answers

Why does gcc not give a warning at undefined behaviour in code inside?

I just read this SO C++ FAQ about undefined behavior and sequence points and experimented a bit. In the following code gcc-4.5.2 gives me a warning only in the line mentioned in the code comment, although the one line before shows undefined…
halex
  • 16,253
  • 5
  • 58
  • 67
7
votes
1 answer

Is it valid to initiate the size part of a VLA in the same sequencepoint as the VLA is declared?

In this post, the OP contains code where there is a lot wrong with, but 1 line made me especially curios, since I wasn't able to look anything up, disallowing it. This is the specific line: int n = 100000, arr[n]; Is the order of declaration and…
dhein
  • 6,431
  • 4
  • 42
  • 74
7
votes
1 answer

Safe short circuit evaluation in C++11

Pre-C++11 we know that short-circuiting and evaluation order are required for operator && because of: 1.9.18 In the evaluation of the following expressions a && b a || b a ? b : c a , b using the built-in meaning of the operators in these…
7
votes
2 answers

Is one side of an assignment sequenced before the other in c++?

I understand that this is undefined behavior: int i = 0; int a[4]; a[i] = i++; //<--- UB here because the order of evaluation of i for the left hand side and the right hand side are undefined (the ; is the only sequence point). Taking that…
odinthenerd
  • 5,422
  • 1
  • 32
  • 61
7
votes
4 answers

Is the order of this C statement well defined?

The title is a bit vague as I don't really know how to define this question. It has to do with the following code: for (match = root, m_matchBase = match->requestedBase, m_matchLength = match->length; match != NULL; …
Jimmy Lu
  • 4,810
  • 7
  • 25
  • 30
7
votes
2 answers

Sequence point from function call?

This is yet another sequence-point question, but a rather simple one: #include void f(int p, int) { printf("p: %d\n", p); } int g(int* p) { *p = 42; return 0; } int main() { int p = 0; f(p, g(&p)); return 0; } Is this…
lucasmrod
  • 260
  • 2
  • 9