0

Please Explain the following code

#include <iostream>

using namespace std;

int main()
{
    const int x = 10;
    int * ptr;
    ptr = (int *)( &x );    //make the pointer to constant int*
    *ptr = 8;               //change the value of the constant using the pointer.
    //here is the real surprising part
    cout<<"x: "<<x<<endl;          //prints 10, means value is not changed
    cout<<"*ptr: "<<*ptr<<endl;    //prints 8, means value is changed
    cout<<"ptr: "<<(int)ptr<<endl; //prints some address lets say 0xfadc02
    cout<<"&x: "<<(int)&x<<endl;   //prints the same address, i.e. 0xfadc02
    //This means that x resides at the same location ptr points to yet 
    //two different values are printed, I cant understand this.

    return 0;
}
Siddiqui
  • 7,662
  • 17
  • 81
  • 129
  • If something is not clear from comments in the code, you should read about "C++ pointers". Just put it in the Gooogle – MajesticRa May 26 '11 at 07:19
  • 2
    `ptr = (int *)( &x );` (followed by `*ptr = 8`) is undefined behavior, asking about the behavior of the program is meaningless. – GManNickG May 26 '11 at 07:19
  • @GMan: It's not that line that has undefined behavior, it's the next line that attempts to write to a const qualified object. See references [here](http://stackoverflow.com/questions/1542200/is-using-const-cast-for-read-only-access-to-a-const-object-allowed) . – CB Bailey May 26 '11 at 07:20
  • What about the code would you like explained? – jalf May 26 '11 at 07:21
  • g++ throws errors, like it should. – Adam May 26 '11 at 07:21
  • Same question as http://stackoverflow.com/questions/5987817/how-can-understand-a-constant-variable-in-memory-c/ – Kristofer May 26 '11 at 07:22
  • @Adam, I have compiled in Visual Studio 2008, its running.. – Siddiqui May 26 '11 at 07:22
  • @Charles: You're right of course, I'm used to questions like these casting and modifying in a single statement. Didn't realize this one was special. ;) – GManNickG May 26 '11 at 07:23
  • @Siddiqui : What does that prove, other than that one valid manifestation of undefined behavior is "everything _appears_ okay..."? – ildjarn May 26 '11 at 07:23
  • Ok I understand now, thanks to every one.. – Siddiqui May 26 '11 at 07:24
  • @GMan: It think that most people would agree that the line you highlight is "wrong", because it allows the next line to cause UB despite looking relatively innocuous. – CB Bailey May 26 '11 at 07:25

2 Answers2

11
*ptr = 8;

This line causes undefined behavior because you are modifying the value of a const qualified object. Once you have undefined behavior anything can happen and it is not possible to reason about the behaviour of the program.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
6

Since x is a const int, the compiler will most likely, in the places where you use x, directly substitute the value that you've initialized with (where possible). So the line in your source code:

cout<<"x: "<<x<<endl;

will be replaced by this at compile time:

cout<<"x: "<<10<<endl;

That's why you still see 10 printed.

But as Charles explained, the behaviour is undefined, so anything could happen with code like this.

Jesper
  • 202,709
  • 46
  • 318
  • 350