简体   繁体   中英

Examine C pointers with GDB

I am readying a book currently, and I am learning to examine pointers in C with GDB. 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). However your pointer in this case is not a string, which is why you get that weird "string"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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