0
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);

before modifying after modifying

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 :)

DharinS
  • 85
  • 1
  • 5
  • 3
    It's Undefined Behaviour. Anything can happen. – Yksisarvinen Dec 07 '19 at 14:08
  • 1
    https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Dec 07 '19 at 14:12
  • Who told you it's in "the RO segment of the memory"? – Lightness Races in Orbit Dec 07 '19 at 14:17
  • 1
    Declaring `const int y=7;` is basically a promise to the compiler that you will never ever change the value of `y`. If you lie to the compiler, it will kick you in the butt with hard to track bugs. – HAL9000 Dec 07 '19 at 14:37
  • You don't cast away constness in a variable *declared* `const`, suppose `class Foo` has `int a` in it and there is a class function `void Foo::dosomething() const`, none of the variables can be modified in it, but you can use `const_cast` inside `dosomething() const` because `int a` was not *declared* `const`. Got it? – Vinícius Dec 07 '19 at 14:43
  • Also note that you rarely should do it, it's much better to declare `int a` as `mutable int a` if you really need it – Vinícius Dec 07 '19 at 14:46
  • @vini -- `int Foo::a` will implicitly be declared `const` whenever an instance of Foo is declared `const`. So in `void Foo::dosomething() const`, `const_cast<>(this)->i = 123` may only work if `this` points to an object that was originally declared *not* const. At least that is what I understand from c++ – HAL9000 Dec 07 '19 at 14:54
  • Whats the down-vote for? It is a genuine question, now closed – DharinS Dec 07 '19 at 14:57
  • @HAL9000 Yup, if an instance of an object `Foo` is *declared* `const` its data members will be `const`, because the object itself cannot be modified, it even feel silly when I write it down. If then its data members are `const` you shouldn't cast away constness in a variable declared `const`. But note that you've said *implicitly*, in my view (someone correct me if I'm wrong), `a` is *explicitly* declared, because you are *explicitly* declaring a Foo object `const`. – Vinícius Dec 07 '19 at 15:06

1 Answers1

1

The behaviour of modifying a const object is undefined.

how does it even show the modification in debug mode?

Because the behaviour of the program is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326