Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const_cast question

Reply
Thread Tools

const_cast question

 
 
Gajanan Bhat
Guest
Posts: n/a
 
      05-04-2004
Hi,

When i run this piece of code.....i get the following result...

const int w = 10;
int * wp = const_cast<int *> (&w);

*wp = 20;

cout << w << endl << &w << endl << wp << endl << *wp << endl;

Output :

10
0012F58C
0012F58C
20

can any1 explain y this happens even though &w and wp
are the same?

Gajanan


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      05-04-2004

"Gajanan Bhat" <(E-Mail Removed)> wrote in message
newsUHlc.5152$(E-Mail Removed) m...
> Hi,
>
> When i run this piece of code.....i get the following result...
>
> const int w = 10;
> int * wp = const_cast<int *> (&w);
>
> *wp = 20;
>
> cout << w << endl << &w << endl << wp << endl << *wp << endl;
>
> Output :
>
> 10
> 0012F58C
> 0012F58C
> 20
>
> can any1 explain y this happens even though &w and wp
> are the same?


Because this is undefined behavior.
You first promise the compiler that w is const and then cast away it's
const-ness. Compiler then gets the freedom to do whatsoever it wants!

-Sharad


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-04-2004
Gajanan Bhat wrote:

> Hi,
>
> When i run this piece of code.....i get the following result...
>
> const int w = 10;
> int * wp = const_cast<int *> (&w);
>
> *wp = 20;
>
> cout << w << endl << &w << endl << wp << endl << *wp << endl;
>
> Output :
>
> 10
> 0012F58C
> 0012F58C
> 20
>
> can any1 explain y this happens even though &w and wp
> are the same?


Yes, you invoked undefined behaviour by creating an object as const and
then casting the constness away. If you define the object as const, the
compiler is allowed to assume that its value will never change.
Questions like yours can be seen very often, and I wonder why so many
people want to define objects as const just to cast the constness away,
and what they think why const is there in the first place.

 
Reply With Quote
 
Bill Seurer
Guest
Posts: n/a
 
      05-04-2004
Rolf Magnus wrote:
> ...I wonder why so many
> people want to define objects as const just to cast the constness away,
> and what they think why const is there in the first place.


People want their code to be as fast as possible so they throw in all
the "tricks" they have learned (or just heard of) to make it faster even
if their code breaks the "contract" that those tricks require. It's
fairly obviously for something like this but a lot trickier other times.
I see this quite frequently with the aliasing rules and optimization
in my compiler work.
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-04-2004
"Gajanan Bhat" <(E-Mail Removed)> wrote in message newsUHlc.5152

> When i run this piece of code.....i get the following result...
>
> const int w = 10;
> int * wp = const_cast<int *> (&w);
>
> *wp = 20;
>
> cout << w << endl << &w << endl << wp << endl << *wp << endl;
>
> Output :
>
> 10
> 0012F58C
> 0012F58C
> 20
>
> can any1 explain y this happens even though &w and wp
> are the same?


Because wp is the address of w, regardless of const modifiers.

BTW, a highly optimizing compiler would probably generate code that would
crash the machine. As w is a const object, it will be placed in read-only
memory as part of the compiled program, and any attempt to modify it will be
a memory access violation.

The compiler may also note that *wp is the same as w and therefore output
the following (note the last line is different):

10
0012F58C
0012F58C
10


 
Reply With Quote
 
George Privalov
Guest
Posts: n/a
 
      05-04-2004
Bill Seurer <(E-Mail Removed)> wrote in message news:<c7801c$1e8q$(E-Mail Removed)>...
> People want their code to be as fast as possible so they throw in all
> the "tricks" they have learned (or just heard of) to make it faster even
> if their code breaks the "contract" that those tricks require. It's
> fairly obviously for something like this but a lot trickier other times.
> I see this quite frequently with the aliasing rules and optimization
> in my compiler work.


I can not come up with one example when casting away constness should
improve the code's performance. Would't it be easier for compiler to
deal with simple and clear intent code rather than think what the
particular writer was assuming about performance of this or that
architecture?

G.
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-04-2004
George Privalov wrote:
> Bill Seurer <(E-Mail Removed)> wrote in message news:<c7801c$1e8q$(E-Mail Removed)>...
>
>>People want their code to be as fast as possible so they throw in all
>>the "tricks" they have learned (or just heard of) to make it faster even
>>if their code breaks the "contract" that those tricks require. It's
>>fairly obviously for something like this but a lot trickier other times.
>> I see this quite frequently with the aliasing rules and optimization
>>in my compiler work.

>
>
> I can not come up with one example when casting away constness should
> improve the code's performance.


The compiler can sometimes assume cached values have not been modified
if the corresponding variables have been declared const. This may
improve performance. Also, const function arg's passed semantically by
reference actually can be copied, so that accessing the arg's value does
not require dereferencing.

> Would't it be easier for compiler to
> deal with simple and clear intent code rather than think what the
> particular writer was assuming about performance of this or that
> architecture?


Usually.
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      05-05-2004
"Jeff Schwab" <(E-Mail Removed)> wrote in message
> George Privalov wrote:


> > I can not come up with one example when casting away constness should
> > improve the code's performance.

>
> The compiler can sometimes assume cached values have not been modified
> if the corresponding variables have been declared const. This may
> improve performance. Also, const function arg's passed semantically by
> reference actually can be copied, so that accessing the arg's value does
> not require dereferencing.


True for adding const, but casting away constness, how can that ever improve
performance?


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-05-2004
Siemel Naran wrote:
> "Jeff Schwab" <(E-Mail Removed)> wrote in message
>
>>George Privalov wrote:

>
>
>>>I can not come up with one example when casting away constness should
>>>improve the code's performance.

>>
>>The compiler can sometimes assume cached values have not been modified
>>if the corresponding variables have been declared const. This may
>>improve performance. Also, const function arg's passed semantically by
>>reference actually can be copied, so that accessing the arg's value does
>>not require dereferencing.

>
>
> True for adding const, but casting away constness, how can that ever improve
> performance?


Never. Sorry, I misread your post.
 
Reply With Quote
 
Bill Seurer
Guest
Posts: n/a
 
      05-05-2004
George Privalov wrote:

> Bill Seurer <(E-Mail Removed)> wrote in message news:<c7801c$1e8q$(E-Mail Removed)>...
>
>>People want their code to be as fast as possible so they throw in all
>>the "tricks" they have learned (or just heard of) to make it faster even
>>if their code breaks the "contract" that those tricks require. It's
>>fairly obviously for something like this but a lot trickier other times.
>> I see this quite frequently with the aliasing rules and optimization
>>in my compiler work.

>
>
> I can not come up with one example when casting away constness should
> improve the code's performance.


No, no. The other way round. They stick in const thinking it makes the
code faster and then break it by casting away const because the stuff
really wasn't const to begin with.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
basic question on const_cast pauldepstein@att.net C++ 4 02-01-2008 03:27 AM
beginner's question on static_cast and const_cast subramanian100in@yahoo.com, India C++ 4 11-14-2007 04:27 PM
const_cast<> R. Anbeeswaran C++ 7 11-14-2003 12:15 PM
const_cast question drowned C++ 3 08-04-2003 08:51 AM
[help] const_cast Kaspar Minosiants C++ 2 07-21-2003 12:43 PM



Advertisments