You're running into two issues: 1. Undefined behavior -- you are attempting to modify the value of an object more than once between sequence points, and the Standard imposes no requirement on how to handle that behavior. Basically, any expression of the forms: i = i++; j = k++ * k++; foo(i,i++,--i); invoke undefined behavior. Read up on the semantics of the "++" and "--" operators in an *authoritative* reference (the Standard would obviously be one, but also K&R2 or H&S5); they don't work the way most people think they should. 2. Order of evaluation -- AFAIK, there's no requirement that expressions in a function parameter list be evaluated in any particular order. At first blush, it *looks* like the arguments were evaluated from right to left (if x = 1, then x++ evaluates to 1, with the side effect that x == 2, and ++x evaluates to 3, with the side effect that x == 3), but given that you've invoked undefined behavior the actual reason may be something else entirely. [링크 : http://bytes.com/topic/c/answers/222558-operation-x-may-undefined] |
위의 소스를 gcc -Wall 옵션을 주고 컴파일 할 경우에
$ gcc -Wall cc_test.c cc_test.c: In function ‘main’: cc_test.c:6: warning: operation on ‘x’ may be undefined cc_test.c:6: warning: operation on ‘x’ may be undefined |
분명 x는 변수인데, 그에 대한 operation이 정의되지 않았다는게 무슨 말인지 모르겠지만,
아무튼 실행을 하면
$ ./a.out 3 3 1 |
아무튼, calling convention과도 연관이 있어 보이는데,
c언어 특성상 right-left 로 push 하므로(가장 위에는 왼쪽 값이 오도록)
가장 먼저들어가는, 오른쪽 x++ 은 1이 들어가고
1을 더해준다음, 다음 명령어를 수행하면서(++x) 한번에 2가 증가하게 되고
그럼으로 인해 x, ++x 순으로 3이 들어가는게 아닐까 생각된다.
아니면 말구?
[링크 : http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html]
winXP SP3 32bit / VC++ 6 에서 해본 결과
2 1 1 |
linux / gcc 결과
3 3 1 |
objdump를 소스와 함께 디스어셈블 하기 위해서는
gcc -g 옵션으로 디버깅 정보를 주어야 한다.
[링크 : http://wikidocs.net/mybook/1473]
솔찍히 어셈을 몰라서 모르겠다 ㅋㅋ
결론 : 한줄에 ++나 -- 연산자를 중복으로 사용하지 말자.
'Programming > C Win32 MFC' 카테고리의 다른 글
수직탭이 모야? (what is the Vertical tab?) (4) | 2010.07.19 |
---|---|
CD-ROM 삽입시 메시지 - win32 api cdrom insert message (2) | 2010.07.04 |
cdecl / stdcall / fastcall (0) | 2010.01.06 |
C++ 클래스 공용 변수 초기화 (3) | 2009.11.25 |
static 변수의 범위(scope of static variable) (0) | 2009.10.29 |