> Why this not working?
Because you can't just invent addresses and go looking, at least not on machines running protected operating systems like Windows, Linux, Unix and Unix clones.
The OS doesn't instantly give you the entire 4GB address space (32-bit OS) to do with what you want.
Nor is your program loaded into the same place each time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int main () {
int var;
std::cout << "Main is at "
<< reinterpret_cast<void*>(main)
<< std::endl;
std::cout << "Stack is at "
<< reinterpret_cast<void*>(&var)
<< std::endl;
}
$ g++ foo.cpp
$ ./a.out
Main is at 0x5609383b41e9
Stack is at 0x7fffadda9d14
$ ./a.out
Main is at 0x55e0c91391e9
Stack is at 0x7ffc5d8fa1c4
there are systems that not only allow this (which is a huge problem for security and stability both) like old DOS, but some embedded systems expect you do to this (but their libraries hide the addresses with names, etc). I had a programmable multi-switch where you set the voltage on the pins via an address this way, a long time ago, but it wasn't even a computer, it was just a device, and a very minimal device at that. It was basically setting memory locations in EEPROM type memory to tell it what to do when it was plugged in.
SO you may have to do it some day, but its not 'normal' coding and highly specific to the task at hand.
You can also look at memory this way, if you can get the OS to grant permission to you; virus scanners and system tools can read memory anywhere looking for problems. I am not sure how you do that in modern windows, but there is a way. You can force it, and assign a pointer a value, but it will likely crash your program even if you just try to read (ever see "access violation at address 0xblahblah" messages?)
I had a programmable multi-switch where you set the voltage on the pins via an address this way, a long time ago, but it wasn't even a computer, it was just a device, and a very minimal device at that. It was basically setting memory locations in EEPROM type memory to tell it what to do when it was plugged in.
some embedded systems expect you do to this (but their libraries hide the addresses with names, etc). I had a programmable multi-switch where you set the voltage on the pins via an address this way, a long time ago, but it wasn't even a computer, it was just a device, and a very minimal device at that. It was basically setting memory locations in EEPROM type memory to tell it what to do when it was plugged in.
I belive this is the sort of situation where you might want to use volatile, otherwise the compiler can optimize away reads and writes with the assumption that they don't have any side-effects.
For example, if the compiler sees something like this:
but if the address is mapped to some memory-mapped hardware device this might not be at all what you want. Each write might be important and the value that you read might not be the same as you wrote earlier so to prevent the compiler from making these assumptions you can mark ptr as volatile.