Yes, I just used an int that I made so that I would be able to demonstrate reading memory that we already know the contents of, to show it's working.
You could put any value you like in p, like this.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include<iostream>
int main()
{
int* p =(int*)0x403010;
std::cout << p << " " << std::endl;
std::cout << *p;
return 0;
}
| |
This causes a segfault, because with this line
std::cout << *p;
I'm asking for the contents of memory location 0x403010, which is forbidden to me in this case. That value has no special meaning, so I'm not surprised it's not my memory to look at.
What you
should not be doing is picking memory locations at random. If you want to know the memory location of a variable, you can fetch it with the
&
operator, as in my earlier code. You will not often have any business wanting to know the value of the contents of an address in memory if you haven't actually created something in that memory-space. The only exception I can think of at the moment is in embedded hardware with real-world physical devices mapped to set numerical memory addresses (for example, an LED on a hardware board will often have its state mapped to a set numerical value in memory that you read in the board's dev manual and then use for the purpose of setting that LED state).
That template you have above is a long-winded WinAPI version of the same thing; demanding to know the value of the object at memory location 0x403010