I would like to understand how C ++ handles constants of basic types.
What will happen if, with the help of the tambourine dances and pointers, the value in the memory cell is changed, where, in theory, a constant should be contained?
There is the following code:
#include <iostream> using namespace std; int main(int argc, char** argv) { const int number = 45; const int * constPoint = &number; cout << "constPoint " << constPoint << endl; int * point = (int *) constPoint; cout << " point " << point << endl; * point = 54; cout << "&number " << &number << "; number " << number << endl; cout << " point " << point << "; *point " << *point << endl; } The conclusion I get is the following:
constPoint 0x7fff1b73a6ec point 0x7fff1b73a6ec &number 0x7fff1b73a6ec; number 45 point 0x7fff1b73a6ec; *point 54 Thus, the value of the constant has not changed, although the number contained in the cell at the address of this constant is different.
How to explain this behavior?
I'm not going to use such code in practice, I just want to understand where the constants are stored.