gdb中的strcmp给出了奇怪的结果

时间:2012-03-07 23:42:25

标签: c gdb strcmp

在GDB(gnu v 7.1-ubuntu)中,当我尝试使用strcmp来确定两个字符串是否相等时,我得到了非常奇怪的结果。 p strcmp("hello","hello")正在向我提供结果-145947168

我尝试使用strcmpstrncmp的所有内容都会在gdb中返回-145947168。我做错了什么?

编辑(感谢Carl在评论中指出相关答案): 请参阅此问题的答案:How to evaluate functions in GDB?

显然,有时编译器会优化从外部库调用的函数,并且在代码中定义一个函数,该函数调用您希望在GDB中访问的外部库的函数,这将使其可用。

我将此添加到我的代码中:

#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

然后用-DDEBUG -g重新编译,以便为我的gdb调试编译那些帮助函数。

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

0 个答案:

没有答案