1
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /path/to/drcalc...(no debugging symbols found)...done.
(gdb) r
Starting program: /path/to/cdrcalc 

Program received signal SIGSEGV, Segmentation fault.
0xb7e606b6 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) 

有人可以帮忙吗?我的代码可以在https://github.com/dramforever/drcalc/分支下载readline

4

2 回答 2

2

您应该使用-g标志进行编译,以便 gdb 能够显示更多调试信息。如果你这样做,你会看到程序在函数inp_readline中崩溃,当你尝试使用sh_line时,它​​是 NULL。

原因是您将sh_line 0(将被解释为 NULL 指针)作为其初始值,然后在inp_readline中检查sh_line是否为非 NULL,在这种情况下释放旧字符串并读取新字符串与readline。但如果它是 NULL,它在开始时,什么都不做,所以当你到达strlen(sh_line)时它仍然是 NULL,并且strlen崩溃。

编辑:

在原文中,它说

if (sh_line) free(sh_line);sh_line=0; 
sh_line = readline(sh_Prompt);

但是你添加了一些大括号,所以它改为说

if (sh_line) {free(sh_line);sh_line=0;
sh_line = readline(sh_Prompt);}

这就是为什么readline永远不会被调用的原因。

于 2013-01-29T09:40:43.803 回答
0

(我是提问者!)
我对 readline 一无所知。所以一个好的函数定义会起作用。
我选择了https://github.com/jterrace/craq/blob/master/gmp-4.3.1/demos/calc/calcread.c

于 2013-01-29T11:07:19.083 回答