我在我的c程序中使用了popen函数来执行shell命令并返回结果。它的工作效率达到了90%,但遗憾的是,由于某种原因,有时我会出现分段错误。
以下是代码位置和gdb stacktrace:
FILE *fp = popen("ip route list | grep default | awk '{print $5}' | tr -d '\n'", "r");
堆栈跟踪:
Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=0x7ffff795d760 <main_arena>, bytes=256) at malloc.c:3491
3491 malloc.c: No such file or directory.
(gdb) bt
#0 _int_malloc (av=0x7ffff795d760 <main_arena>, bytes=256) at malloc.c:3491
#1 0x00007ffff761dae0 in __GI___libc_malloc (bytes=256) at malloc.c:2893
#2 0x00007ffff760ac48 in _IO_new_popen (command=0x415128 "ip route list | grep default | awk '{print $5}' | tr -d '\n'", mode=0x414fde "r") at iopopen.c:288