Skip to main content

C++ of the Day #4 - Changing const object

Question

오늘은 별로 흥미진진한 내용은 아니네요. 역시 뉴스그룹에서 가져왔습니다. ((comp.lang.c++.moderated:prog output?))
#include "iostream.h"

void main(void)
{
  const int i = 10;
  int *p;
  p = const_cast(&i);
  *p = 111;
  cout << i << "  " << *p;
}
/*
output:
10  111 
*/
질문은...
  1. 왜 i 와 *p의 출력이 다르게 나오는가?


Answer

뉴스그룹의 친절한 답변자들 모두 답에 앞서 코드에 있는 문제들을 지적하네요.
  1. 표준 헤더를 쓸 생각이었다면 iostream.h가 아니라 iostream을 써야 할것이다.
  2. 또한 표준 헤더라면 <>괄호를 사용하여야 한다.
  3. main의 리턴 타입은 int이다.
모두 적용하면 다음과 같이 되겠군요.
#include 

int main()
{
  const int i = 10;
  int *p;
  p = const_cast(&i);
  *p = 111;
  std::cout << i << "  " << *p;
}
진짜 중요한 문제의 답은 간단합니다.
const 객체를 수정하는 코드의 결과는 정의되어 있지 않으므로(undefined behavior) 컴파일러는 위 프로그램에서 어떤 값이라도 출력할 수 있습니다.
너무 간단하죠? 참고로 undefined behavior에 대해 얼마전 DDJ에 올라온 Pete Becker의 Living By the Rules에서 인용해봅니다.
Violations of semantic rules that are not diagnosable semantic rules result in undefined behavior; that is, the C++ Standard doesn't impose any requirement on what a compiler does when faced with such code. When referring to this, please don't use the abominable wording "This program invokes undefined behavior." The correct phrasing is "The behavior of this program is undefined." And please keep in mind that undefined behavior means only that the C++ Standard doesn't say what the code in question does. It does not mean that compilers are obliged to do nasty things like set fire to your hard drive. Often, the best way to write code that takes maximal advantage of the hardware it will run on is to use code constructs whose behavior is undefined, but well understood. For example, if you really need speed, instead of testing whether an integer value is greater than or equal to zero and less than some upper limit, you can convert it to an unsigned integer type with the same number of bits and test whether the result is greater than the upper limit. On most architectures, converting the value doesn't change any bits, so it doesn't require any code; negative values are simply treated as large unsigned values, which will always exceed the limit. The behavior of that code is formally undefined, but it works. Except when it doesn't. Test for it.
마지막 부분에 있는 undefined behavior의 사용예를 보니 간단하면서도 아이디어가 좋네요. 실제로는 가독성이라던가 유지 보수성을 감안했을땐 사용하지 않는게 좋겠죠? :-)

Comments

Popular posts from this blog

CodeHighlighter plugin test page.

This post is for testing CodeHighlighter plugin which uses GeSHi as a fontifier engine. ((Those code blocks are acquired from Google Code Search .)) ((For more supported languages, go CodeHighlighter plugin or GeSHi homepage.)) C++ (<pre lang="cpp" lineno="1">) class nsScannerBufferList { public: /** * Buffer objects are directly followed by a data segment. The start * of the data segment is determined by increment the |this| pointer * by 1 unit. */ class Buffer : public PRCList { public: Buffer() { ++index_; } PHP (<pre lang="php" lineno="4">) for ($i = 0; $i $value = ord( $utf8_string[ $i ] ); if ( $value < 128 ) { // ASCII $unicode .= chr($value); } else { if ( count( $values ) == 0 ) { $num_octets = ( $value } $values[] = $value; Lisp (<pre lang="lisp">) ;;; Assignment (define-caller-pattern setq ((:star var fo...

Textiler plugin test page

This post is for testing Textiler plugin . This plugin uses Textile engine (version 2.0.0). The sample text is come from Textile test page. (Note that the result will be vary according to your CSS options.) Supported wiki syntax Rendering result h2{color:green}. This is a title h3. This is a subhead p{color:red}. This is some text of dubious character. Isn't the use of "quotes" just lazy writing -- and theft of 'intellectual property' besides? I think the time has come to see a block quote. bq[fr]. This is a block quote. I'll admit it's not the most exciting block quote ever devised. Simple list: #{color:blue} one # two # three Multi-level list: # one ## aye ## bee ## see # two ## x ## y # three Mixed list: * Point one * Point two ## Step 1 ## Step 2 ## Step 3 * Point three ** Sub point 1 ** Sub point 2 Well, that went well. How about we insert an <a href="/" title="watch out">old-fashioned hypertext link</a>? Will the quo...

BookCover plugin test page

This post is for testing BookCover plugin . (( The previous test page has some comments from users.)) The 'Books Reading' section in the sidebar is also using this plugin. [ bookcover:1932394613] [bookcover:1932394613] [ bookcover:1590598385(Smart and Gets Things Done)] [bookcover:1590598385(Smart and Gets Things Done)] [ bookcover:1591841380(Wikinomics : How Mass Collaboration Changes Everything)] [bookcover:1591841380(Wikinomics : How Mass Collaboration Changes Everything)] [ bookcover:8995856408] [bookcover:8995856408] [ bookcover:8831793721] [bookcover:8831793721] [ bookcover:9788831793728] [bookcover:9788831793728]