why different value in const int and pointer to int ?

M

marsarden

here is the code:

int main()
{
const int a = 1;
int *p = const_cast<int*>(&a);
*p = 2;

cout << "value a=" << a << endl;
cout << "value *p=" <<*p << endl;
cout << "address a="<<&a << endl;
cout << "address p="<<p << endl;

}

the result like:

value a=1
value *p=2
address a=0012FED4
address p=0012FED4

why the same memory address has different value?
 
K

Kai-Uwe Bux

marsarden said:
here is the code:

int main()
{
const int a = 1;
int *p = const_cast<int*>(&a);
*p = 2;

Modifying an object (here the int-object a) declared as const is undefined
behavior.
cout << "value a=" << a << endl;
cout << "value *p=" <<*p << endl;
cout << "address a="<<&a << endl;
cout << "address p="<<p << endl;

}

the result like:

value a=1
value *p=2
address a=0012FED4
address p=0012FED4

why the same memory address has different value?

You are misinterpreting the results. One could even argue that any
interpretation of the results is a misinterpretation since the program has
UB and could produce any output whatsoever.

However, the output that you see is indicative of a certain compiler
optimization: since a is const, the compiler propagates the value 1 (given
to a at initialization) to the places where a is used. (That the should be
compiler is allowed to do that is one of the reasons why the standard makes
modifying a UB).


Best

Kai-Uwe Bux
 
V

Victor Bazarov

marsarden said:
here is the code:

int main()
{
const int a = 1;
int *p = const_cast<int*>(&a);
*p = 2;

cout << "value a=" << a << endl;
cout << "value *p=" <<*p << endl;
cout << "address a="<<&a << endl;
cout << "address p="<<p << endl;

}

the result like:

value a=1
value *p=2
address a=0012FED4
address p=0012FED4

why the same memory address has different value?

The boilerplate answer is, your program has undefined behaviour
as soon as it attempts to change the value of a constant object;
after that anything can happen.

A more advanced asnwer, involving guesswork, is that the compiler
creates the code that uses the 'a's value (1) defined at the time
of its creation, instead of the current value (which you may have
changed by breaking the rules of the language) when you ask the
value to be output. Hey, says the compiler, the object is const,
it's not going to change, why bother retrieving its value every
time, I will just use the value it was initialised with... And
the code is created to ouptut 1 instead of 'a'. Now, *p cannot
be given the same treatment, since it's *not* const. So, the
compiler generates the code that does in fact retrieve the actual
value (which you managed to change) to be printed.

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top