私のコードは次のとおりです。
#include <stdio.h>
int main()
{
int x = 10, y = 0;
x = x++;
printf("x: %d\n", x);
y = x++;
printf("y: %d\n", y);
}
ポストインクリメントの性質を考えると、次の出力が期待されます。
x: 10
y: 10
私の推論は、5行x
目で、増分が行われた後に初期値に割り当てる必要があるということです。
代わりに、しかし、私はこれを取得します:
x: 11
y: 11
アセンブリを掘り下げると、これは私には意図的な選択のように見えます。
LCFI2:
movl $10, -4(%rbp) // this is x
movl $0, -8(%rbp) // this is y
incl -4(%rbp) // x is simply incremented
movl -4(%rbp), %esi
leaq LC0(%rip), %rdi
movl $0, %eax
call _printf
movl -4(%rbp), %eax // now x is saved in a register,
movl %eax, -8(%rbp) // copied to y,
incl -4(%rbp) // and finally incremented
movl -8(%rbp), %esi
leaq LC1(%rip), %rdi
movl $0, %eax
call _printf
何が起きてる?GCCは私を自分から救おうとしていますか?私は便利な言語リファレンスを持っていませんが、これは意図されたセマンティクスを壊すと思いました。