Forum Discussion
Altera_Forum
Honored Contributor
19 years agoI wrote the following function:
void TestCompiler2(int j)
{
j = j++;
} and upon compiling received this warning message: ../hello_world_small.c: In function `TestCompiler2': ../hello_world_small.c:189: warning: operation on `j' may be undefined I have all warnings enabled, and for my real projects I have -Wall and -Werror on the compiler flags line (with the -Wall checkbox off, as it can override values on the compiler flags line). Here is the produced code, built with the release configuration: 010002b4 <TestCompiler2>:
10002b4: f800283a ret So, to answer your question, you're building the debug version where the compiler is supposed to produce code that does literally what you are telling it to do. What you are telling it to do, is the following: 1. Set j = to j. 2. At some point, either before or after, increment j. The behavior is undefined according to the C standard because j is modified twice in the same expression. The compiler is free to perform the side-effect (incrementing j) at any time, although it must of course assign the previous value to whatever is to the left of the =. But because j is there, the behavior is undefined and the C compiler is allowed to do *anything* at this point, whether it makes sense or not.