Does "i=i++" in C++ code produce undefined behavior running under VxWorks?

Using VxWorks OS and C++ programming language

In Windows OS ' index = index++;' in the below code snippet would produce undefined behavior and would not necessarily 'increment' the index.

Does anyone know if this undefined behavior holds true for the same C++ code running under the VxWorks OS?

int index = 32;
   for(I = 0; I <= 15; I++)
     ... some code here ...
      index = index++;
Who is Participating?
sjklein42Connect With a Mentor Commented:
Thank you.  Yes, I can see how it can be construed as undefined:  Because the autoincrement of index could occur in another 'thread', asynchronously executed in parallel with the assignment of the original value of index to itself, it is undefined as to which will occur last.  Thus index might be unchanged, or might be incremented.

I cannot speak specifically to the VxWorks implementation but suggest it is unwise to depend on it acting one way or another, as its behavior may change in future releases if they change the optimizer.

Are you sure it is undefined?

The way I read it is that the right side (index++) gets executed first, but since the ++ is postfix, the value of (index++) is the unincremented value of index.  This unmodified value of index is then assigned to itself (index), which means that the value of index, after the statement has been completed, is unchanged.

Compare to

index = ++index;

which will increment index and use the new value to assign to itself.  This is, of course, the same as


which is also the same as


Both those standalone statements will also increment index.
SLClearyAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.