| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
23 Aug 2005 05:18:34 PM |
| Object: |
Question about const types |
Hi,
I have the following code:
#include <iostream>
using namespace std;
int main()
{
const int x = 1;
int *y = const_cast<int*>(&x);
*y = 2;
cout << &x <<": "<< x << endl;
cout << y <<": "<< *y << endl;
return 0;
}
The above code produces the following output:
0xbffff49c: 1
0xbffff49c: 2
How is it that they can both have the same address but different values?
.
|
|
| User: "Pete Becker" |
|
| Title: Re: Question about const types |
23 Aug 2005 05:27:10 PM |
|
|
wrote:
How is it that they can both have the same address but different values?
You lied to the compiler. Now it's lying to you. Seems fair.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
.
|
|
|
| User: "" |
|
| Title: Re: Question about const types |
23 Aug 2005 05:29:05 PM |
|
|
Pete Becker wrote:
jois.de.vivre@gmail.com wrote:
How is it that they can both have the same address but different values?
You lied to the compiler. Now it's lying to you. Seems fair.
So writing to the read-only address resulted in undefined behavior is
all?
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Question about const types |
23 Aug 2005 05:55:43 PM |
|
|
wrote:
Pete Becker wrote:
wrote:
How is it that they can both have the same address but different values?
You lied to the compiler. Now it's lying to you. Seems fair.
So writing to the read-only address resulted in undefined behavior is
all?
The behavior of a program that attempts to modify a const object during
its lifetime is undefined.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
.
|
|
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Question about const types |
23 Aug 2005 05:34:44 PM |
|
|
wrote:
I have the following code:
#include <iostream>
using namespace std;
int main()
{
const int x = 1;
int *y = const_cast<int*>(&x);
*y = 2;
After this point, the program has undefined behaviour.
cout << &x <<": "<< x << endl;
cout << y <<": "<< *y << endl;
return 0;
}
The above code produces the following output:
0xbffff49c: 1
0xbffff49c: 2
How is it that they can both have the same address but different values?
Often, when you write 'x' in an expression, and your 'x' designates
a compile-time constant, the compiler is free to use the _value_ of
that constant known to the compiler instead of accessing the contents
of the memory occupied by the object.
But of course, since the behaviour of your code is undefined after you
attempted to change the value of a constant object, any output would
be a fair game, or no output at all, or a crash or ...
V
.
|
|
|
| User: "" |
|
| Title: Re: Question about const types |
23 Aug 2005 05:46:26 PM |
|
|
Often, when you write 'x' in an expression, and your 'x' designates
a compile-time constant, the compiler is free to use the _value_ of
that constant known to the compiler instead of accessing the contents
of the memory occupied by the object.
Interesting, that makes sense. Thanks!
.
|
|
|
|
| User: "Giulio Guarnone" |
|
| Title: Re: Question about const types |
24 Aug 2005 07:04:15 AM |
|
|
Victor Bazarov ha scritto:
jois.de.vivre@gmail.com wrote:
I have the following code:
#include <iostream>
using namespace std;
int main()
{
const int x = 1;
int *y = const_cast<int*>(&x);
*y = 2;
After this point, the program has undefined behaviour.
cout << &x <<": "<< x << endl;
cout << y <<": "<< *y << endl;
return 0;
}
The above code produces the following output:
0xbffff49c: 1
0xbffff49c: 2
How is it that they can both have the same address but different values?
Often, when you write 'x' in an expression, and your 'x' designates
a compile-time constant, the compiler is free to use the _value_ of
that constant known to the compiler instead of accessing the contents
of the memory occupied by the object.
But of course, since the behaviour of your code is undefined after you
attempted to change the value of a constant object, any output would
be a fair game, or no output at all, or a crash or ...
V
Interesting, I don't know that !
But what if :
#include <iostream>
using namespace std;
int main()
{
volatile const int x = 1;
volatile int *y = const_cast<int*>(&x);
*y = 2;
cout << &x <<": "<< x << endl;
cout << y <<": "<< *y << endl;
return 0;
}
the keyword volatile should eliminate every compiler optimization, and
force it to put values in variable, or am I wrong ?
Bye,
Giulio
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Question about const types |
24 Aug 2005 08:24:27 AM |
|
|
Giulio Guarnone wrote:
[..] what if :
#include <iostream>
using namespace std;
int main()
{
volatile const int x = 1;
volatile int *y = const_cast<int*>(&x);
*y = 2;
cout << &x <<": "<< x << endl;
cout << y <<": "<< *y << endl;
return 0;
}
the keyword volatile should eliminate every compiler optimization, and
force it to put values in variable, or am I wrong ?
Maybe, if you remove the attempt to modify it and make the program
well-behaved.
V
.
|
|
|
|
|
|

|
Related Articles |
|
|