Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Read value from a specific memory address

Tags:

c

How do I read a value from a given memory address (e.g. 0xfed8213) using the C programming language?

like image 515
user3815726 Avatar asked Dec 03 '25 17:12

user3815726


1 Answers

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.

like image 153
Ricky Mutschlechner Avatar answered Dec 06 '25 09:12

Ricky Mutschlechner