가능한 중복 :
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)후 증가 연산자 : 예기치 않은 동작이
내 코드는 다음과 같다 :
#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가 나 자신을 저축하려고합니까? 나는 편리한 언어 참조를 가지고 있지 않다. 그러나 이것은 의도 된 의미를 깨뜨릴 것이라고 생각했을 것이다.
가증로 기록 될 것입니다! –
http://en.wikipedia.org/wiki/Sequence_point – Ken
옛날, 특정 언어의'a = 1 + a'는 모든 새로운 버그가 닥칠 광산이었습니다. –