开发者

Dereference arbitrary memory location in C

I'm trying to debug a program I've written. According to the debugger a particular void * holds the value 0x804b008. I'd like to be able to dereference this value (cast it to an int * and get it's value).

I'm getting a Segmentation Error with this code. (The program with the void * is still running in the background btw - it's 'paused')

#include <stdio.h>

int main() {
    int* pVal = (int *)0x804b008;
    printf("%d", *pVal);
}

I can see why being able to de开发者_开发技巧ference any point in memory could be dangerous so maybe that's why this isn't working.

Thank you!


Your program (running in the debugger) and this one won't be running in the same virtual memory space; accessing that pointer (even if it were a valid one) won't give you any information.

Every program running on your machine has its own logical address space. Your operating system, programming language runtime, and other factors can affect the actual literal values you see used as pointers for any given program. But one program definitely can't see into another program's memory space, barring of course software debuggers, which do some special tricks to support this behaviour.

In any case, your debugger should let you see whatever memory you want while your program is paused - assuming you have a valid address. In gdb x/x 0x804b008 would get you what you want to see.

For more information:

  1. Wikipedia article on Virtual Memory.
  2. gdb documentation


It's quite simple. The OS knows that address does not belong to your program, so you can't print it without bypassing memory protection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜