FluffyBeing
FluffyBeing

Reputation: 468

In C, how can I print out what is stored at a specific memory address?

Is it possible to print out what is stored at a specific memory address? For example I want to know what is stored at the address 0x7FFFFF0. How would I do this? I do not know what is stored at the address before hand ie. it could be an int or char or a null terminator.

Upvotes: 0

Views: 3017

Answers (1)

Carl Norum
Carl Norum

Reputation: 225192

Depending on your environment, you may be able to simply declare a pointer and dereference it:

volatile unsigned int *p = (volatile unsigned int *)0x7FFFFF0;
printf("%u\n", *p);

This operation requires your program to have permission to access that memory, of course. Your mileage may vary on different operating systems and environments.

You definitely won't be able to extract any type information at runtime without doing some more work to figure out what that memory represents, semantically speaking, and then extracting the bytes you care about in that context.

Upvotes: 2

Related Questions