C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const usage in c++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
chenwei225@gmail.com
Guest





PostPosted: Fri Oct 28, 2005 9:37 am    Post subject: const usage in c++ Reply with quote



I wrote such a piece of code:

const int i = 100;
int *p = (int *)&i;
*p = 200;

After the last line code was executed, variable i is still 100. Why?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
chris jefferson
Guest





PostPosted: Fri Oct 28, 2005 10:59 am    Post subject: Re: const usage in c++ Reply with quote



[email]chenwei225 (AT) gmail (DOT) com[/email] wrote:
Quote:
I wrote such a piece of code:

const int i = 100;
int *p = (int *)&i;
*p = 200;

After the last line code was executed, variable i is still 100. Why?


Short version: Because you are trying to fool your compiler and you
invoke undefined behaviour, so any output is valid.

Longer version: The optimiser knows that if you write "const int i =
100;", there is no way the value of i can ever change. Therefore
wherever you write i, the optimiser can replace it with 100 at compile time.

This is one of the problem with C-style casts, they are a blunt hammer,
and frequently it's easy to write things without realising that they are
invalid. If you'd used a static_cast (the general C++ replacement), the
code wouldn't compile. It would if you used a const_cast (which is
designed just to remove const), but before you used it, I would hope /
assume you'd read the documentation about it, which says you mustn't do
this kind of thing.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Yuri Khan
Guest





PostPosted: Fri Oct 28, 2005 10:59 am    Post subject: Re: const usage in c++ Reply with quote



[email]chenwei225 (AT) gmail (DOT) com[/email] wrote:
Quote:
I wrote such a piece of code:

const int i = 100;
int *p = (int *)&i;
*p = 200;

After the last line code was executed, variable i is still 100. Why?

Modifying a true constant is undefined behavior. The compiler is free
to do anything in this case. It can substitute the value 100 for all
uses of i; it can actually modify the value of i; it can generate code
that will cause access violation (aka segmentation fault); it can even
change the value of 100 for the rest of the program.

Just don't do that.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob
Guest





PostPosted: Fri Oct 28, 2005 3:56 pm    Post subject: Re: const usage in c++ Reply with quote

chris jefferson wrote:

Quote:

This is one of the problem with C-style casts, they are a blunt
hammer, and frequently it's easy to write things without realising
that they are invalid. If you'd used a static_cast (the general C++
replacement), the code wouldn't compile. It would if you used a
const_cast (which is designed just to remove const), but before you
used it, I would hope / assume you'd read the documentation about it,
which says you mustn't do this kind of thing.



Using const_case instead of a basic C-style cast doesn't actually
change anything in this case: the result is still undefined behaviour

While I agree that C-style casts are a blunt hammer, I wouldn't
characterise C++-style casts much differently. The only difference is
that C++ provides a set of blunt hammers, while C only provides one.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Sat Oct 29, 2005 1:08 pm    Post subject: Re: const usage in c++ Reply with quote

Quote:
chris jefferson wrote:
This is one of the problem with C-style casts, they are a blunt
hammer, and frequently it's easy to write things without realising
that they are invalid. If you'd used a static_cast (the general C++
replacement), the code wouldn't compile. It would if you used a
const_cast (which is designed just to remove const), but before you
used it, I would hope / assume you'd read the documentation about it,
which says you mustn't do this kind of thing.

Rob wrote:
Quote:
Using const_case instead of a basic C-style cast doesn't actually
change anything in this case: the result is still undefined behaviour

Read chris jefferson's words again. They imply that const_cast wouldn't
get compile errors -- that doesn't mean that it isn't UB, and in fact
the phrase "the documentation...says you mustn't do this kind of thing"
kind of spells out the fact that it IS still a problem.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
syntetic@mail.ru
Guest





PostPosted: Sat Oct 29, 2005 1:10 pm    Post subject: Re: const usage in c++ Reply with quote

Quote:
const int i = 100;
int *p = (int *)&i;
*p = 200;

Modifying a true constant is undefined behavior.

Hello, All.

Just one more question, what about volatile keyword?
Can I do something like:

volatile const int i = 100;
int * = (int*)&i;
*p = 200;
std::cout << i;

I try this example under VC7.0 and receive the same result.
Is this undefined behavior or compler bug?

With respect,
Alexander Kozlov


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bo Persson
Guest





PostPosted: Sat Oct 29, 2005 8:16 pm    Post subject: Re: const usage in c++ Reply with quote


<syntetic (AT) mail (DOT) ru> skrev i meddelandet
news:1130506861.099976.141820 (AT) g47g2000cwa (DOT) googlegroups.com...
Quote:
const int i = 100;
int *p = (int *)&i;
*p = 200;

Modifying a true constant is undefined behavior.

Hello, All.

Just one more question, what about volatile keyword?
Can I do something like:

volatile const int i = 100;
int * = (int*)&i;
*p = 200;
std::cout << i;

I try this example under VC7.0 and receive the same result.
Is this undefined behavior or compler bug?

No, it's equally undefined.

Using volatile const means that the program cannot change the value,
but someone else might. That someone could be a hardware device, which
might be readable but not writeable.

Whenever you use a cast, you tell the compiler "Trust me, I know what
I am doing!". If you don't really know, it is almost always undefined
behaviour.

There is a contact between the programmer and the compiler, saying
that if you feed it valid program code, it has to compile it properly.
Whenever you invoke undefined behaviour, you have broken the contract,
and the compiler is free to do whatever it feels like. Anything can
happen.


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.