Question about const types

J

jois.de.vivre

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?
 
V

Victor Bazarov

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
 
J

jois.de.vivre

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!
 
P

Pete Becker

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.
 
G

Giulio Guarnone

Victor Bazarov ha scritto:
After this point, the program has undefined behaviour.



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
 
V

Victor Bazarov

Giulio said:
[..] 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top