Thread: modifying const using pointer

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    modifying const using pointer

    I was trying to modify a const int using pointer. To my surprise, I was able to modify it but was surprised to see that the values when printed out from the const int and through pointer shows different values (even though the memory addresses are the same). Does anybody has any explanation for this behavior ? Below I am posting the code as well as output.

    I would greatly appreciate if somebody who is more knowledgeable in C/C++ can help me understand.

    Code:
    Code:
    void modify_const(void) {
    	const int x = 1;
    	const int *y;
    	int *z;
    	y = &x;
    	z = const_cast<int *>(y);
    	cout << "Value in y: " << y << endl;
    	cout << "Value in z: " << z <<endl;
    	cout << "Address of x: " << &x << endl;
    	cout << "I was able to do address manipulation to remove constness" << endl;
    	cout << "Value of z is: " << *z << endl;
    	//cout << "Value of x is incremented to: " << ++x << endl;
    	(*z)++;
    	cout << "Value of z is: " << *z << endl;
    	cout << "Value of x is: " << x << endl;
    	cout << "Value in z: " << z <<endl;
    	cout << "Address of x: " << &x << endl;
    }
    Output:
    Value in y: 0x27ff14
    Value in z: 0x27ff14
    Address of x: 0x27ff14
    I was able to do address manipulation to remove constness
    Value of z is: 1
    Value of z is: 2
    Value of x is: 1
    Value in z: 0x27ff14
    Address of x: 0x27ff14

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Check address of z
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The compiler is free to optimise

    cout << "Value of x is: " << x << endl;

    to

    cout << "Value of x is: " << 1 << endl;

    because you declared it as a const, and the compiler knows what that const value is.

    If you break that trust, then it's your problem to fix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Thanks a lot for the reply Salem. I think what you mentioned explains the issue. I am trying to understand what Dr.Bjarne Stroustrup mentions in his Book TC++PL Sec.10.2.2 by "The protection of private data relies on restriction of the use of class member names. It can therefore be circumvented by address manipulation and explicit type conversion".

    Does anybody has an example where we can access private data members in class. I failed miserably in trying that :-) Again thanks a lot for the help.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think something like this may be meant:

    Code:
    #include <iostream>
    
    class Int
    {
    private:
        int n;
    public:
        Int(int n): n(n) {}
        int get() const { return n; }
    };
    
    struct FakeInt
    {
        int n;
    };
    
    int main()
    {
        Int i(10);
        *reinterpret_cast<int*>(&i) = 40; //because the int in the class and the instance happen to be at the same memory location
        std::cout << i.get() << '\n';
    
    
        reinterpret_cast<FakeInt*>(&i)->n = 100; //because FakeInt happens to have the same memory layout, except n is public there.
        std::cout << i.get() << '\n';
    }
    Again, the point of const and private data is helping the compiler help you get things right, so something like the above is something that you just shouldn't do.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Thank you very much Anon for taking the time to give me an example. I do very much appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  2. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM