Ambiguity when non constant pointer is pointing to constant variable
-
Hi All,
- In the code above, a non constant pointer is pointing to constant variable.
- I was trying to change the variable value using *ptr = 100.
- When I check the value of *ptr and a, it was different. (Line number 20 and 21)
- But both ptr and &a is pointing to same memory location. (Line number 23)
I am not able to understand how, two different values are assigned to same memory address ? does compiler creates temporary address to hold both different values here (100 and 10) ?
Any help is appreciated. Thanks!
-
@Vinoth-Rajendran4
I believe your code does write intoa
via*ptr
. I wonder if the compiler has keptconst int a
in a register, does not expect it to be changed since it'sconst
, and reports the original value forcout << a
?I wonder if you can fool it with
cout << *&a
? -
@Vinoth-Rajendran4 thats not what const canst was defined for, and is undefined behaviour, switch the compiler, optimisation level or to release/debug build, you may get a different outcome.
-
@Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:
for cout << *&a , I am getting value as 100
As @J-Hilk already said, your code is almost unsafe. Depending on C++ compiler type (GCC, MSVC, etc.), code optimization level and build mode (release/debug) you will probably got different results.
Declaring a variable as
const
will allow compiler to do some optimization. Violate thisconst
state will introduce unpredictable behavior.It is not because you can write a code that it implies that this code is valid, for example following code is also valid but will crash:
char * ptr = nullptr; cout << ptr;
-
@Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:
I thought const_cast<> is used to remove or add constant to the expression. If its okay, can you please explain me the actual use of const_cast<> ?
The easiest is to look at C++ documentation (cf. https://en.cppreference.com/w/cpp/language/const_cast):
const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.
-
@Vinoth-Rajendran4 said in Ambiguity when non constant pointer is pointing to constant variable:
@JonB : for
cout << *&a
, I am getting value as 100.Which was my point/guess! At some level
cout << a
is taking advantage of theconst int a = 10;
it saw earlier, it's not really going to fetch the current contents ofa
which you actually altered via*ptr = 100
. Some optimization (on your compiler) decided you would not altera
.My guess was that
*&a
would force it to generate code to actually look in thea
memory address for the value, by-passing the "optimization". And so it has proved to be --- again, on your compiler.As the others have said, this whole thing is compiler-specific/undefined/"very naughty", for precisely the above reasons (e.g. see @KroMignon's quote) and the way you are seeing
a != *&a
. Which is why you shouldn't be doing it! :) -
@Vinoth-Rajendran4 See here: https://en.cppreference.com/w/cpp/language/const_cast
What you are doing here is exactly what is mentioned there: "Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior." -
@KroMignon said in Ambiguity when non constant pointer is pointing to constant variable:
cout << ptr;
This won't crash. It will just print 0. But this: cout<< *ptr; will.
-
This post is deleted!