sequence-points

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 respect to sequence points? It is really difficult to find out these issues when the comp...

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 singleton { private: singleton(); // private constructor so users must call instance() ...

Can assignment be done before constructor is called?

A comment to http://stackoverflow.com/questions/945232/whats-wrong-with-this-fix-for-double-checked-locking says: The issue is that the variable may be assigned before the constructor is run (or completes), not before the object is allocated. Let us consider code: A *a; void Test() { a = new A; } To allow for more for...

Is this undefined?

Well, I'm not really in serious need of this answer, I am just inquisitive. Expressions like *ptr++ = a are perfectly valid since we are operating on two objects ptr and *ptr but if i write *ptr++ = *ptr + a is it still valid ? For example consider the following snippet: int main(void){ int a[] = {5,7,8,9,2}; int* p =a; *p+...

C++0x without sequence point?

What does it mean by Sequence Point is deprecated from C++0x, Wikipedia says it is deprecated from C++0x. Does that mean undefined behaviors due to sequence point have no effect? ...

Variable assignment in 1st condition and using same variable in 2nd condition Well defined?

Is this well defined? Streamreader ^reader = gcnew Streamreader("test.txt"); String ^line; while ((line = reader->ReadLine()) != nullptr && line != "") { //do stuff } I believe that I read somewhere that it is not guaranteed that the assignment is executed before the 2nd conditional. It may be that I'm wrong or that this just ap...

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 point in between and hence the behavior is well defined. Someone please clarify. P.S: ...

Is this code undefined behavior?

Hi, 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 some (complicated) condition. And they may be pointing to same object like in above ca...

Is this program having any sequence point issues ?

#include<stdio.h> 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. ...

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 sequence point so I am not very sure about the behavior of the expression z = x && y && ...

Suggestions for concise index handling in circular buffer

I've implemented a circular buffer, and I would like a concise means of updating the buffer pointer while properly handling the wrap-around. Assuming an array of size 10, my first response was something like: size_t ptr = 0; // do some work... p = ++p % 10; Static analysis, as well as gcc -Wall -Wextra, rightly slapped my wrist for...

In C99, is f()+g() undefined or merely unspecified?

I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines t...

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 = x++, j = x++; as a normal statement would be undefined behavior is the comma operator, ...