GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Apr-2007, 09:04
meili100 meili100 is offline
Junior Member
 
Join Date: Feb 2007
Posts: 37
meili100 has a little shameless behaviour in the past
Question

An interesting question about CONST


CPP / C++ / C Code:
const int i=125;
int *j = const_cast<int *> (&i);
(*j) = 256;
printf("%d %d\n", i, *j);

Why the result is "125 256" ?
Last edited by LuciWiz : 06-Apr-2007 at 10:43. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 06-Apr-2007, 09:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: An interesting question about CONST


Quote:
Originally Posted by meili100
Why the result is "125 256" ?

Note to moderator: I posted a response to this on the C board (before I saw it here), I have subsequently pasted the response here and deleted the substance of the discussion from the C board.

Note to Poster: Why would you post the same question to the C board and the C++ board? That's not likely to be more productive. I understand why a Poster might ask the question on two (or more) different forum sites, but not to two different sections of the same site.
Quote:
Originally Posted by meili100
const int i=125;
int *j = const_cast<int *> (&i);
(*j) = 256;
printf("%d %d\n", i, *j);

Why the result is "125 256" ?

My guess would be that the compiler "hard-coded" the value of i into the code. That is: since i is declared to be constant, its value is known at compile time, so the print statement would actually pass "125" to the printf function, and not the current value of the contents of that memory location.

By using the const_cast<>, you made the compiler accept what it would not normally allow. C++ won't let you convert a non-void pointer to a non-void pointer of any type without some kind of cast, and this is converting a const pointer to int to a non-const pointer to int.

But even though you think you fooled it, it still won't generate code that allows you to assign a new value to a declared constant. The value of i will always be 125 wherever it is in scope in this program.

Since j was declared as pointer to int (non-constant), the code allows the contents of the location pointed to by j to be changed.

Note that this is undefined behavior, and the results are not always guaranteed to be what you observe.

From the C++ standard:

"Creating a new object at the storage location that a const object with static or automatic storage duration occupies ... results in undefined behavior."

Whenever I see any kind of cast in a program, I wonder about two things:

1. Is it is really necessary? Sometimes people use casts explicitly in places where they aren't needed, just because they don't understand (much) about data types and language-defined conversions. I look very carefully at their data types and conversions between them.


2. Why is it used? In cases like this, someone is trying to do something that is "illegal". If the only way that the programmer can get the program to compile is by using a cast, is it really trying to do something that is questionable? Or: Maybe they haven't included some header file that would have defined a prototype function. Using a cast might eliminate the pesky warning message (or error message), but this will not necessarily guarantee proper linkage to the function.


There are certainly places in programs where casts are required to get proper behavior as well as to get the blooming compiler to accept the code. This ain't one of them (since the attempted behavior is, in fact, undefined).

The language lets you do lots of things that result in undefined behavior. That doesn't mean that the language should be changed; it means that you really have to know what you are doing and you can't depend on the language to defend itself against user stupidity (or maliciousness).

On the other hand, it makes a reasonably provocative homework problem, right? I agree with your title: It is an interesting question.


Regards,

Dave
Last edited by davekw7x : 06-Apr-2007 at 10:56.
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Morse Code Program Using Switch Statement glacier CPP / C++ Forum 10 24-Sep-2006 22:31
Handling keyup events. harroldm FLTK Forum 3 24-Aug-2006 08:48
need help with a console menu system BullBuchanan CPP / C++ Forum 6 20-Aug-2006 14:46
help me to do this program!!! mrdell_06 CPP / C++ Forum 3 21-Feb-2006 21:12
Error - unresolved external symbol _WinMain ap6118 CPP / C++ Forum 6 23-Mar-2005 22:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.