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
7
votes
3 answers

Sequence points and side effects in C

In this C-FAQ it is give about sequence point; The Standard states that; Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value…
haccks
  • 104,019
  • 25
  • 176
  • 264
7
votes
6 answers

Sequence Points vs Operator Precedence

Possible Duplicate: Unsequenced value computations (a.k.a sequence points) Undefined Behavior and Sequence Points Operator Precedence vs Order of Evaluation I'm still trying to wrap my head around how the following expression results in undefined…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
7
votes
1 answer

Ternary operator and Sequence Points in C

I've an expression of the form shown below :- while (count) { ... ... index = ((count == 20)? 0 : index++); ... ... } Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part. Is this…
Zshn
  • 121
  • 1
  • 5
6
votes
2 answers

Is (++i)++ undefined behavior?

Is (++i)++ undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me. My gut feeling says this is undefined in C++03…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6
votes
3 answers

Does Java have undefined behavior like C++ does?

Undefined behavior and sequence points The link above is talking about sequence point and side effect in C++. In a word, it means that between two sequence points, if we have more than one side effects, the order of the side effects are…
Yves
  • 11,597
  • 17
  • 83
  • 180
6
votes
1 answer

Does standard C++11 guarantee that temporary object passed to a function will have been destroyed after the end of the function?

As known, that standard C++11 guarantees that temporary object passed to a function will have been created before function call: Does standard C++11 guarantee that temporary object passed to a function will have been created before function…
Alex
  • 12,578
  • 15
  • 99
  • 195
6
votes
3 answers

complicated expression involving logical AND (&&)

void main(void) { int x,y,z; x=y=z=1; z = x && y && ++z;//is this fine? } I have lately started reading about sequence points stuffs but I cannot figure out whether the above sample of code is fine or not. I know the && operator introduces a…
6
votes
1 answer

In a function call, why isn't comma a sequence point?

In the following code int main(){ int a=3; printf("%d %d %d",++a,a,a++); return 0; } As specified, From C99 appendix C:, The following are the sequence points described in 5.1.2.3: The call to a function, after the arguments…
kauray
  • 739
  • 2
  • 12
  • 28
6
votes
5 answers

Why does (*p=*p) & (*q=*q); in C trigger undefined behavior

Why does (*p=*p) & (*q=*q); in C trigger undefined behavior if p and q are equal. int f2(int * p, int * q) { (*p=*p) & (*q=*q); *p = 1; *q = 2; return *p + *q; } Source (Nice article by the way):…
nawfel bgh
  • 1,393
  • 17
  • 27
6
votes
5 answers

Is following statement valid in ANSI C? Is it valid at all?

During my preparation to exam on ANSI C I have encountered the following question - Is following statement valid? If not, please make required changes to make it valid. The original statement is: test(i++,i++); it isn't valid because the…
Anatoly
  • 5,056
  • 9
  • 62
  • 136
6
votes
2 answers

Are there "sequence-point" issues with statements like "int a=4,*ptr=&a;" or "x+=4,y=x*2;"?

My understanding of the whole sequence points thing is basic. All I have is some crude intuitive idea that "once a sequence point is encountered, we can be sure all side effects of previous evaluations are complete". I also read that in statements…
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
6
votes
4 answers

A compiler may not move accesses to volatile variables across sequence points; what does it mean?

Declaring a variable as 'volatile' means to read/write directly from the memory location, not from the register variable. I have a knowledge about 'sequence point'. But i dont understand the statement mentioned in the title. Could someone explain…
Whoami
  • 13,930
  • 19
  • 84
  • 140
6
votes
1 answer

Is the behavior of i = post_increment_i() specified, unspecified, or undefined?

Consider the following C program: int i = 0; int post_increment_i() { return i++; } int main() { i = post_increment_i(); return i; } With respect to the 2011 version of the C standard (known as C11), which of the following alternatives is…
5
votes
3 answers

What's wrong with this fix for double checked locking?

So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this: class…
5
votes
2 answers

Relationship between C11 atomics and sequence points

I basically have the following code snippet: size_t counter = atomic_fetch_sub_explicit(&atomicCounter, 1, memory_order_release); if (counter - 1 == 0 && atomic_load_explicit(&anotherAtomicCounter, 1, memory_order_relaxed) == 0 { //Some…
Skyfarmer
  • 127
  • 6