473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

why different value in const int and pointer to int ?

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?
Nov 16 '07 #1
2 1586
marsarden wrote:
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
Nov 16 '07 #2
marsarden wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
5
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.