找到变量Buf的准确地址
作为参考,我使用以下代码:
#include <stdio.h>
#include <string.h>
int main (void) {
char buf[100]; // ------> How do I find the address in gdb?
printf ("Buffer is at memory location: %08x\n", &buf);
strcpy (buf, "some random text");
printf ("Text is [%s]\n", buf);
return 0;
}
如何让 gdb
向我显示 buf
变量的地址?
As reference, I'm using the following code:
#include <stdio.h>
#include <string.h>
int main (void) {
char buf[100]; // ------> How do I find the address in gdb?
printf ("Buffer is at memory location: %08x\n", &buf);
strcpy (buf, "some random text");
printf ("Text is [%s]\n", buf);
return 0;
}
How can I get gdb
to show me the address of the buf
variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(gdb) p &a
如果您需要变量a
的地址。不过,变量可能会缓存在寄存器中,在这种情况下,GDB 会告诉您为寄存器 $xxx 中的标识符“a”请求的地址
。旁注:不要使用
gets
,请参阅此处。(gdb) p &a
if you need the address of variablea
. A variable might be cached in a register though, in which case GDB would tell youaddress requested for identifier "a" which is in register $xxx
.Sidenote: do not use
gets
, see here.当 gdb 设置为 C 语言模式(和 Objective-C)时,
&
运算符将起作用。在任何语言模式下,您都可以使用
(输出与您的代码不完全对应。)我写这个答案是因为即使是寻找其他语言答案的人(包括我自己)也发现了这个问题。人们也可以随时通过
set language c
切换到C模式,但更改后符号名称可能会有所不同。The
&
operator will work when gdb is set to C language mode (and Objective-C).In any language mode you can use
(The output does not correspond exactly to your code.) I am writing this answer because this question is found even by people looking for the answer for other languages (including myself). One can also always switch to the C mode by
set language c
, but the symbol names may be different after this change.如果您在 gdb 中输入以下内容,您将获得地址:
如以下记录所示:
If you enter the following into gdb, you'll get the address:
as in the following transcript: