How do I read a value from a given memory address (e.g. 0xfed8213) using the C programming language?
Each process has its own virtual address space, so, address 0x12345678 in one program will not be the same as address 0x12345678 in another.
You can access what is at the address simply by doing:
#include <stdio.h>
int main(int argc, char *argv[]){
char *ptr = (char *)0x12345678; //the addr you wish to access the contents of
printf("%c\n", *ptr); //this will give you the first byte, you can add any more bytes you need to the ptr itself, like so: *(ptr + nbyte).
return 0;
}
You'll likely get a segmentation fault though, unless you really know what you're doing. My guess is that you think this is the way to solve some other problem, whereas this isn't the actual solution. This does answer your question in the OP, though.
Here is Another Virtual Memory learning resource.
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