views:

158

answers:

2

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 deference any point in memory could be dangerous so maybe that's why this isn't working.

Thank you!

+5  A: 

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
Carl Norum
I've read a bit about OS development so I believe I kind of understand what you're talking about.Is this virtual memory located in RAM - just mapped to a different addressing scheme or something else?Thanks again.EDIT: Answered my question before I even asked it!
Tyler
Virtual memory just means that as far as you're concerned when writing a program, you have a nice contiguous memory space. The operating system handles all the potential memory fragmentation for you.
Carl Norum
@CarlThanks. I gathered as much. I was more interested in *how* it is implemented at the OS level. http://en.wikipedia.org/wiki/Virtual_memory#Page_tables seems to be how it's done.Thanks
Tyler
A: 

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.

Matthew Flaschen