strcmp in gdb giving odd results

十年热恋 提交于 2020-01-14 19:29:10

问题


In GDB (gnu v 7.1-ubuntu) I am getting really weird results when I try to use strcmp to determine if two strings are equal. p strcmp("hello","hello") is giving me the result -145947168.

Everything I try with strcmp or strncmp is returning -145947168 in gdb. What am I doing wrong?

EDIT (thanks to Carl for the pointer to a related answer in the comments): See the answer to this question: How to evaluate functions in GDB?

Apparently sometimes the compiler optimizes out functions called from external libraries, and defining a function in code that calls the function of the external library you want access to in GDB will make it available.

I added this to my code:

#ifdef DEBUG
int mystrcmp(char *a, char *b){
        return strcmp(a,b);
}
int mystrncmp(char *a, char *b, int n){
        return strncmp(a,b,n);
}
#endif

and then re-made with -DDEBUG -g to enable the compilation of those helper functions for my gdb debugging.

(gdb) p mystrcmp("hello","hello")
$1 = 0
(gdb) p strcmp("hello","hello")
$2 = -145947168

来源:https://stackoverflow.com/questions/9610882/strcmp-in-gdb-giving-odd-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!