I wanted to prove that there is nothing known as (A protected memory address) and the whole story is just about the compiler, or the OS, or whatever application the hosted app is running on just make some checking on read and write requests the hosted app send to its superior process and this superior app or whatever you call it decide if this child process has the right to read or write to this specific memory location but this c++ code doesn't work in this essence so why :
#include <iostream>
int main()
{
const int x = 10;
std::cout << &x << std::endl; // So i can view address of x
std::cout << "x Before is equal "<< x <<std::endl;
int y ;
std:: cin >> std::hex >>y;
int *pinter = (int*)y ;
*pinter = 20;
std::cout << "x After is equal "<< x <<std::endl;
}
This code is supposed to get around the concept of c++ compiler setting x variable type to const int so that neither pointer to the variable (unlike in C in which pointers to constants can change the value of constant ) nor references to the variable can change the variable so this code is supposed to get address of variable x (of course after it's printed out) and then a pointer do the rest of the work , so what i messed here , cause it seams like this memory location is hardware protected( I know it's not but I am very confused)
This code is supposed to get around the concept of c++ compiler setting x variable type to const int so that [...]
You can break the rules of the language, but then your code is not valid C++. You shall not modify something that is qualified as const. If you do you have undefined behavior. As compilers are made to compile valid C++, they are not mandated to do anything meaningful to non-valid code and the result can be anything or nothing.
As already said in a comment:
const has nothing to do with hardware or memory. It is an agreement between you and your compiler and you broke that agreement. As a reward your compiler will do anything to your code, but not necessarily what you expect.
You tried to trick the compiler by making the modification at runtime such that at compile time the compiler cannot know that you will modify a const. However, you did declare x as const so the compiler will assume that its value will not change. If you still modify the value of x anything can happen.
PS: Rather frequently people come up with hacks to "proove" that private isn't really private, cosnt isn't really const and similar. The thing is: Those are facilities that are supposed to help you to make less mistakes and write cleaner code. If you try hard to cirumvent those facilites you will manage to do so (sometimes even without invoking UB). However, this "prooves" nothing except the possiblity to shoot yourself in the foot. C++ is not Java, it does not hold your hand and tries to prevent you from every possible mistake that you can make. In that regard C++ is closer to Pythons "we are all constenting adults here".
const is provided to: 1. Annotate that the value is not supposed to be changed 2. Allow the compiler to optimize the code better because of the fact that it’s not supposed to be changed.
You didn’t have to jump through all of these hoops, you could have just used ‘const_cast’ to change the const’ness.
The compiler “helps” you to enforce the const’ness but as you shown, there are many ways to bypass it. If you will change a const value will yield UB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With