[질문] UNIX에서 a = a++ ; 의 문제점?

bizzare의 이미지

아주 이상한 현상을 발견해서 이렇게 질문을 올립니다.

#include <stdio.h>

int main()
{
	int a = 0;
	a = a++;

	printf("a = %d\n", a);
	
	return 0;
}

위와 같은 코드를

MSVC6.0에서 돌려봤습니다.
그 결과는 쉽게 예상할 수 있듯이

Quote:
a = 1

이 나왔습니다.

그런데, SUN에서 CC를 이용하여 빌드했더니 결과가

Quote:
a = 0

이 나오더군요.

이건 컴파일러 버그가 아닐까요?
아니면 위의 코드는 OS또는 Compiler에 따라 변할 수 있는 코드일까요?

의견을 말씀해주세요.

다음은 각 시스템에서 테스트한 결과입니다.

Quote:
int a = 0;
a = a++;

결과

SUN (CC) : 0
HP (aCC): 0
IBM (xlC): 0
SUSE(g++): 1
Win32(VC): 1

doldori의 이미지

코드가 잘못된 것입니다. 인접한 두 sequence point 사이에서 동일한 개체를
두 번 이상 변경한 결과는 정의되지 않습니다.

bizzare의 이미지

doldori

Quote:
인접한 두 sequence point 사이에서 동일한 개체를
두 번 이상 변경한 결과는 정의되지 않습니다.

위 문장을 잘 이해하지 못하겠네요. 대충 감은 오지만요.
일단 sequence point 라는 용어를 잘 모르겠습니다. ^^

좀 더 쉽게 설명해주시면 감사하겠습니다.

정말 깨끗하고 멋진 프로그램을 짜고 싶습니다.

bizzare의 이미지

이미 제가 올린 질문에 대한 주제는 많이 논의된 주제였더군요.

혹시라도 제 질문을 보시고 저와 동일한 의문을 가지셨다면
다음 링크를 참고해주세요.

1) http://online-judge.uva.es/board/viewtopic.php?t=5268
2) http://c-faq.com/expr/evalorder2.html

정말 깨끗하고 멋진 프로그램을 짜고 싶습니다.

jachin의 이미지

a=a++; 에서

a++; 과 a=a+1; 은 동치입니다.

따라서 a=a=a+1; 이란 셈이죠.

이렇게도 안될터이니 한 번 해보세용.

unipro의 이미지

#include <stdio.h>

int main()
{
	int a = 0;
	a = a++;

	printf("a = %d\n", a);
	
	return 0;
}

b = a++는 b = a; a = a + 1;와 같은 의미입니다.

컴파일러에 따라서 아래와 같이 다르게 처리하지 않았나 추측해봅니다.
a = a++;
1)

a = a;
a = a + 1;

2)

tmp = a;
a = a + 1;
a = tmp;

내 블로그: http://unipro.tistory.com

체스맨의 이미지

unipro wrote:

2)
tmp = a;
a = a + 1;
a = tmp;

이 부분은 a 에 대해 아무짓도 안하는 것과 같기 때문에, 불필요 코드라 판단하여 아예 제거해버렸을 것 같습니다.

Orion Project : http://orionids.org

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.