JYUN-NENG JI JYUN-NENG JI - 1 year ago 62
C++ Question

The problems of const data in c++

const int *a = new int(100);
int *ptr = (int*) a;
cout << *a << endl;
*ptr = 1000;
cout << *a << endl;

const int b = 100;
int *c = (int*)&b;
*c = 200;
cout << &b << " " << b << endl;
cout << c << " " << *c << endl;


print out


  1. While I use *ptr, I can change the const data *a. Why?

  2. Both the address of b and *c are the same, but the data are different. Why?


Answer Source

While I use *ptr, I can change the const data *a. Why?

Because your program is causing undefined behavior. You lied to the compiler that b is non-const by casting const-ness away from the pointer produced by &b, but writing to that pointer on the *c = 200 line is undefined behavior.

Both the address of b and *c are the same, but the data are different. Why?

It happens for the same reason: the behavior is undefined.

As for the real-life scenario when this would happen, I would speculate that the compiler has optimized out the read of b, because you promised it that b is const. However, the compiler found a clever way of getting back at you by replacing a read of the actual b with the output of a constant that has been assigned to b originally.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download