I'm trying to change value of const variable via its address.
following this code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(void)
{
uint64_t const x = -1;
uint64_t *b = reinterpret_cast<uint64_t*>(0x28ff10);
cout<< x << endl;
cout<< &x << " " << b << " " << *b << endl;
printf("%p\n", &x);
*b = 10;
cout<< &x << " " << x << " " << b << " " << *b << " " << *(reinterpret_cast<uint64_t*>(0x28ff10)) <<endl;
return 0;
}
Compiled with MinGW 4.8.1
:
g++ -g main.cpp && ./a.exe
And this is output:
18446744073709551615
0x28ff10 0x28ff10 18446744073709551615
0028FF10
0x28ff10 18446744073709551615 0x28ff10 10 10
Could anyone explain it ?
EDIT:
Figured out! compile still optimized my variable although I compiled it with -O0
. Looked at ASM generated, I saw that printf and cout put directly the value instead of the variable symbol.
So, to make my code do right behavior, I have to declared it with volatile static