1

In the following code

#include <string.h> 
#include <iostream> 
using namespace std; 

int main() 
{ 
    int *a;
    const int b=5;
    a=(int *)&b;
    *a=6;
    cout<<*a<<b<<endl;
    cout<<a<<" "<<&b;

    return 0; 
} 

address of b is typecasted and stored into a.So to my knowledge *a must be pointing to same location as b and should give same value as b.Getting an output of a and &b shows that both of them points to the same memory location. But the a* and b gives different values as outputs.If i have not typecasted the pointer then i would have got same value for *a and b.Can someone help my find whats wrong here?.Thanks in Advance

65
0x7fff2e7969fc 0x7fff2e7969fc
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

2

You are trying to write on a variable (b) which was declared const. This is undefined behavior in C++. Undefined behavior means, that the program may do anything at this point, everything is valid, including printing completely wrong numbers or even crashing.

You first promised that b is constant and they you broke this promise.

The compiler gave you a hint that what you do is not allowed, by forcing you to insert a cast. Without the (int*) cast your code will not compile, because it is not const correct. Casting around const is often a hint that something is not correct. There are legitimate uses for casting around const, but not in situations like your code where the underlying variable is really const.

The cases where you are allowed to cast around const all involve a non-const variable, and having a const pointer to it, and then casting the const pointer to a non-const one, for example since you still maintain logical constness, e.g. you maintain an important invariant. But the underlying storage is always non-const in these scenarios.

This is most likely what happens in your scenario:

The compiler remembers that you want to give the value 5 the name b. It also realizes that you are taking a pointer to b, so in addition it reserves space for b on the stack. It will most likely not even bother to put the 5 onto the stack until you try to read it, but you never do. Instead you overwrite the location for b on the stack with 6. So the print statements first print a 6 (for the location of b on the stack) and then a 5 (the compiler simply inserts a 5 everywhere where you use b).

Johannes Overmann
  • 4,914
  • 22
  • 38
  • [Your answer to my unrelated question](https://stackoverflow.com/a/54845646/1275653) remains the sole answer. I would like to leave my question open for now, *accepting* no answer, but do not wish to deny you reputation points. Instead, I have reviewed some of your other answers to find two good ones I can upvote. This is one. [Here](https://stackoverflow.com/a/54728765/1275653) is the other. Both are good answers, both deservedly +1. So, no *acceptance,* but you and I should be even on the points. – thb Feb 26 '19 at 16:39
  • @thb: That is not necessary, but this is very kind of you! Thanks! :-) – Johannes Overmann Feb 26 '19 at 18:37