Start New Discussion within our Software Development Community

Hi,

I have one address (0xffbff21c) where I have stored 2 int values, depending on the way I read it. Where is the second value stored (at which address)?

My code is

#include <iostream>
using namespace std;

int main()
{
 const int a1 = 40;
  const int* b1 = &a1;
  cout << "a1 is " << (*b1) << endl;
  cout << "b1 is " << b1 << endl;
  int* c1 = const_cast<int*>(b1);
  cout << "b1 is " << b1 << endl;
  cout << "c1 is " << c1 << endl;

int* d1 = static_cast<int*>(static_cast<void*>(c1));
cout << "d1 is " << d1 << endl;
cout<< "*d1 is " << *d1 << endl;
*d1=50;
cout<< "*d1 is " << *d1 << endl;
cout<< "*d1 address is "<< d1 << endl;
cout << "a1 is " << a1 << endl;
cout << "a1 address is" << &a1 << endl;
cout<< "*d1 is " << *d1 << endl;
cout<< "*d1 address is "<< d1 << endl;
}

The output is:
a1 is 40
b1 is 0xffbff21c
b1 is 0xffbff21c
c1 is 0xffbff21c
d1 is 0xffbff21c
*d1 is 40
*d1 is 50
*d1 address is 0xffbff21c
a1 is 40
a1 address is0xffbff21c
*d1 is 50
*d1 address is 0xffbff21c

So it seams like I do change the value at the given address, but the a1 does not change, even if i has that address.

Does it happen you to know where (at which addresses) the actual two different values of (*d1) and a1 are stored?

M

> cout << "a1 is " << a1 << endl;
The compiler (since it knows the value is const) is allowed to generate cout << "a1 is " << 40 << endl; If you smash away the const-correctness of your program, then don't complain when the results bite you.

This article has been dead for over six months. Start a new discussion instead.