Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the hidden storage?

Tags:

c++

I am little confused over the following situation

const char c = 'C';
char * p = const_cast<char *>(&c);

*p = 'K';
cout << " c = " << c << endl;
cout << " *p = " << *p << endl;

printf("c's address      : %u\n", &c);
printf("P is pointing to : %u\n", p);

Which outputs as below on execution

c = C
*p = K
c's address      : 3221180271
P is pointing to : 3221180271

Here I can observe that both '&c' and 'p' having the same address in the memory.

Then what is the hidden mechanism by which 'p' is capable of storing a different value than 'c' is storing whereas both share the same address space in the memory?

like image 500
paper.plane Avatar asked Dec 02 '22 01:12

paper.plane


1 Answers

There is no "hidden storage". These lines

const char c = 'C'; 
char * p = const_cast<char *>(&c); // NO!

seriously violate const-correctness. You're creating a non-const pointer to something that is originally const. Do not do that ever. While the cast itself is fine, if you attempt to dereference p it'll invoke undefined behavior, meaning anything can happen, including the behavior you've just described.

That being said, what's going on is that the compiler is folding the constant c so that the first cout statement prints out C. So the compiler probably turned the cout statements into these:

cout << " c = " << 'C' << endl; // Note literal 'C' instead of variable c
cout << " *p = " << *p << endl;

So while the second cout statement reflects the new value of c by dereferencing p, the first cout statement isn't affected.

The first cout wasn't affected because the compiler assumed that the value of c will never change (it is const after all). With that assumption the compiler replaced the variable access with the constant value itself.

You've violated the compiler's assumption when you did this:

*p = 'K'; // NO!

since p points to the constant c and you've just changed it to K, giving you the behavior you're seeing.

like image 83
In silico Avatar answered Dec 31 '22 07:12

In silico