取消引用 C 中的任意内存位置
我正在尝试调试我编写的程序。根据调试器,特定的 void *
保存值 0x804b008。我希望能够取消引用该值(将其转换为 int *
并获取其值)。
我收到此代码的分段错误。 (带有 void *
的程序仍在后台运行,顺便说一句 - 它处于“暂停”状态)
#include <stdio.h>
int main() {
int* pVal = (int *)0x804b008;
printf("%d", *pVal);
}
我可以明白为什么能够遵循内存中的任何点可能是危险的,所以也许这就是为什么这不是的原因在职的。
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的程序(在调试器中运行)和这个程序不会在同一虚拟内存空间中运行;访问该指针(即使它是有效的)不会为您提供任何信息。
机器上运行的每个程序都有自己的逻辑地址空间。您的操作系统、编程语言运行时和其他因素可能会影响您看到的用作任何给定程序指针的实际文字值。但是一个程序肯定无法查看另一个程序的内存空间,当然软件调试器除外,软件调试器会使用一些特殊的技巧来支持这种行为。
无论如何,你的调试器应该让你在程序暂停时看到你想要的任何内存 - 假设你有一个有效的地址。在 gdb 中
x/x 0x804b008
会给你你想看到的东西。有关详细信息:
gdb
文档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:
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.