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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Operating Systems

From novice to tech pro — start learning today.