A dead loop was encountered while porting a Windows program to Linux, and finally a statement similar to this was located for (i = 0; i < 1; i = i++),
Don't ask me who wrote it, why do you write it so ( tear eyes !) ).
According to the common sense of c, i = i++ should be equivalent to i++, it is true on Windows, but Linux is not, this should be caused by compiler differences.
---------------------------------------Split-line no.0------------------------------------------------------------- ----------------------------------
So the question is, why is it so?
Compiled with arm-linux-gcc-s I.C , compile the results and the source code as follows (GCC assembly is not very good, so use the arm version):
First introduce the assembly instructions that appear:
mov R3, #0 (assigning constant value to R3, equivalent to r3=0)
STR R3, [FP, #-8] (store R3 value to memory address [FP, #-8])
LDR R3, [FP, #-8] ( load the value of memory address [FP, #-8] to R3, and Str opposite )
add R2, R3, #1 (r2 = r3 + 1)
Okey, in fact, the original value of I cache to R3, and then add 1 to the value assigned to R2,R2 will update the I value (because i++), finally R3 will also update I value (because i=),
As to why this is the order, please call the great God. Therefore, the value of I will always be 0, causing the for loop at the beginning of the article to die.
---------------------------------------can divide the line------------------------------------------------------------- ----------------------------------
For the sake of understanding, I also compiled the j=i++, as follows:
j = i++ : The original value of I is cached first, then I is assigned an I value of 1, and finally the original value of the cached I is assigned to J (and i = i++ in the same order).
j = ++i : First assigns the I value plus 1 to I, and then the I value is assigned to J.
---------------------------------------Split-Line No.2------------------------------------------------------------- ----------------------------------
In addition, I have always wanted to see how i++ and ++i 's compilations differ, and the results are as follows:
Since I have heard this, the ++i efficiency in the For loop is higher than i++, but it can be seen in a separate statement,
++i and i++ are the same (GCC series compilers), but it is recommended to use the ++i, for portability, cannot trust the compiler.
i=i++ in Linux gcc for loop causes dead loop problems and ++i/i++ compilation analysis