int* a = new int;
const int y = 7;
a = const_cast<int*>(&y);
*a = 8;
std::cout << *a << std::endl;
std::cout << (int)*(&y) << std::endl;
Output:
8
7
Same output even with a simple cast
a = (int*)(&y);
If it were the RO segment of the memory, how does it even show the modification in debug mode?
EDIT: Since it is closed - I will just add it here
I looked at the assembly output
std::cout << (int)*(&y) << std::endl;
00381CB6 push offset std::endl<char,std::char_traits<char> > (03813C5h)
00381CBB push 7
00381CBD mov ecx,dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (03900ACh)]
00381CC3 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (03900B8h)]
00381CC9 mov ecx,eax
00381CCB call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (03900BCh)]
The compiler will just replace const values at compile time. Nothing to do with RO segment as I wrongfully assumed :)