473,386 Members | 1,827 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,386 software developers and data experts.

Surprising Problem

Hello Friends
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).

const int k=9;

int *p=(int *)&k;

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k

cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.

cout<<"values \n"<<*p<<k; //But still the values are differentl.

what exaclty is happening here...??
Thanks

Jul 25 '06 #1
7 1408
co******@gmail.com wrote:
Hello Friends
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).

const int k=9;

int *p=(int *)&k;

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k

cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.

cout<<"values \n"<<*p<<k; //But still the values are differentl.

what exaclty is happening here...??
Thanks
You can't change the value of constants, though you can take it's
address... and attempt to modify it via the pointer. The compiler does
const folding to reduce the value of expressions involving constants at
compile time, that's why you are seeing this behavior.

-
SJ

Jul 25 '06 #2
* co******@gmail.com:
>
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).

const int k=9;

int *p=(int *)&k;

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k

cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.

cout<<"values \n"<<*p<<k; //But still the values are differentl.

what exaclty is happening here...??
Undefined Behavior (UB), because: modifying an originally const variable.

Or, practically speaking, that you have told the compiler that it can
rely on k being constant, so that it can use the original value directly
wherever it's needed. When you request the address of k you get the
address of a location containing that value. But that doesn't mean the
compiler needs to (generate code to) retrive the value from that
location where the value is used; you've explicitly told the compiler
that it doesn't need to, that the value is constant, unchanging.

However, formally none of that reasoning holds, because you have UB.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '06 #3
co******@gmail.com wrote:
const int k=9;
^^^^^
int *p=(int *)&k;
*p=99; //here *p has updated the value of k
no, it has not. You previously declared that k won't change, marking it
const. What you actually do in above line is invoking undefined
behaviour - you misled the compiler, thus compiler is free to do
anything.
B.

Jul 25 '06 #4

co******@gmail.com wrote:
Hello Friends
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).

const int k=9;

int *p=(int *)&k;

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k

cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.

cout<<"values \n"<<*p<<k; //But still the values are differentl.

what exaclty is happening here...??
Thanks
like above, you want to change the constant via a pointer which pointe
to it,I think it will work .
the k 's address are the same as p while the values are not, I think
the compiler maybe optimize the constants, like puting them in the
register. so try to add a votatile keywords in front of the definition
of k:
volatile const int k = 9; ///looks very funny
the result may be what you want ( I only test it in VC8.0 ) :-)

Jul 25 '06 #5
co******@gmail.com wrote:
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).

const int k=9;

int *p=(int *)&k;

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k
'k' is a constant object. Its value cannot be changed. An attempt to cast away
constness of 'k' and "change" its value (which is what you do above) will only
lead to undefined behavior. That's what happens in your case.

--
Best regards,
Andrey Tarasevich
Jul 25 '06 #6
co******@gmail.com posted:
const int k=9;

int *p=(int *)&k;

Equivalent to:

int *p = const_cast<int*>(&k);

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k

7.1.5.1/4:

Any attempt to modify a const object during its lifetime results in
undefined behavior.

cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.

cout<<"values \n"<<*p<<k; //But still the values are differentl.

what exaclty is happening here...??

The beauty of undefined behaviour.

--

Frederick Gotham
Jul 25 '06 #7
co******@gmail.com wrote:
Hello Friends
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).

const int k=9;

int *p=(int *)&k;

cout<<"Addr of k : "<<&k<<" "<<p;

*p=99; //here *p has updated the value of k

cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.

cout<<"values \n"<<*p<<k; //But still the values are differentl.

what exaclty is happening here...??
Thanks
Why do you call this "surprising problem"? You have broken the
restrictions on well-defined programs by telling the compiler a variable
is const and then changing it. Now it doesn't do what you expect it to.
Why do you expect any particular answer? The program has undefined
behaviour. That's that. Write programs that obey the standard and _then_
you have cause to query happenings outside what the standard predicts.
But here, even if anyone explains some mechanism that cause this result,
and even if you find some other kludge that makes it give the output you
want, what on earth makes you think that it will still give the output
you want if compiled by another compiler, or even by the same compiler
after a version upgrade? I mean, it's not rocket physics; you can guess
that this is undefined even without looking at the standard: you are
telling porkies to the compiler.

--
Ron House ho***@usq.edu.au
http://www.sci.usq.edu.au/staff/house
Jul 27 '06 #8

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

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
12
by: Jack | last post by:
Imagine if someone asked which of the follwing two methods of encoding an integer into a byte array was quicker. The purpose is in preparation for sending across a socket as part of a 'header'....
2
by: pavan | last post by:
See the following code : #include <iostream> using namespace std; float f1 = 1.0e+6; // This function just returns f1. float value1()
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
4
by: David Abrahams | last post by:
I'm seeing highly surprising (and different!) behaviors of PyImport_ImportModule on Linux and Windows when used in a program with python embedding. On Linux, when attempting to import a module...
1
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Linda: Thank you so much for explaining why my code was really working as it was supposed to; I understand now why the sequence of statements I originally used was flawed. And thanks also for...
10
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.