简体   繁体   English

使用GDB检查C指针

[英]Examine C pointers with GDB

I am readying a book currently, and I am learning to examine pointers in C with GDB. 我目前正在准备一本书,并且正在学习使用GDB检查C中的指针。 In the book, when you examine the pointer, the output is as follow: 在书中,当您检查指针时,输出如下:

(gdb) x/xw pointer
0xbffff7e0: 0x6c6c6548
(gdb) x/s pointer
0xbffff7e0: "Hello, world!\n"
(gdb)

But when I do it myself I get the following output: 但是,当我自己执行此操作时,将得到以下输出:

(gdb) x/xw pointer
0x7ffff7de59a0 <_dl_fini>:  0xe5894855
(gdb) x/s pointer
0x7ffff7de59a0 <_dl_fini>:  "UH\211\345AWAVAUATSH\203\354(L\213%\250\177!"

My question is: Why do I get such a different output. 我的问题是:为什么我得到这样一个不同的输出。 I know I am doing something wrong, but, what is it? 我知道我做错了什么,但这是什么?

Thanks everyone. 感谢大家。

Source code: 源代码:

#include <stdio.h>
#include <string.h>

int main() {

    char str[20];
    char *pointer;
    char *pointer2;

    strcpy(str, "Hello, world!\n");
    pointer=str;
    printf(pointer);

    pointer2=pointer+2;
    printf(pointer2);
    strcpy(pointer2, "y you guuuys!\n");
    printf(pointer);
}

What I suspect your "error" is, is that "x/s pointer" returns the "string" pointed by your pointer (remember : a string terminates with \\0). 我怀疑您的“错误”是,“ x / s指针”返回了指针所指向的“字符串”(请记住:字符串以\\ 0终止)。 However your pointer in this case is not a string, which is why you get that weird "string" 但是,在这种情况下,您的指针不是字符串,这就是为什么您会得到奇怪的“字符串”的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM