我正在学习C并且正在使用指针,我编写了一个函数来调用calloc()
,并在该函数之外使用参数。它编译得很好,我打开了我能找到的每个GCC警告选项,除了未使用的argc
和argv
之外什么都没有:
#include <assert.h>
#include <stdlib.h>
typedef struct {
int foo;
} thing_t;
static void init_thing( thing_t **t );
int main( int argc, char **argv ) {
thing_t *thing = NULL;
init_thing( &thing );
assert( thing );
return 0;
}
static void init_thing( thing_t **t ) {
*t = calloc( 1, sizeof( thing_t ) );
assert( t );
}
编译后,它似乎正常运行几次,但最终会在启动时挂起:
C:\work>a.exe
C:\work>a.exe
C:\work>a.exe
(hangs)
我试图找出使用gdb的错误:
(gdb) run
Starting program: C:\work/a.exe
[New Thread 17644.0x308c]
[New Thread 17644.0x1084]
[Inferior 1 (process 17644) exited normally]
(gdb) run
Starting program: C:\work/a.exe
[New Thread 17276.0x4250]
[New Thread 17276.0x30c]
[Inferior 1 (process 17276) exited normally]
(gdb) run
Starting program: C:\work/a.exe
[New Thread 8752.0x4c54]
[New Thread 8752.0x62c]
[Inferior 1 (process 8752) exited normally]
(gdb) run
Starting program: C:\work/a.exe
[New Thread 20456.0x4114]
[New Thread 20456.0x2ee8]
(hangs)
在这里按CTRL + C无休止地打印:
[New Thread 20456.0x2b10]
[New Thread 20456.0x176c]
[New Thread 20456.0x51f8]
[New Thread 20456.0x12a0]
[New Thread 20456.0x2cf8]
[New Thread 20456.0x76c]
...
我尝试在main()
和init_thing()
设置断点,但在执行此操作时无法重现挂起。我想我正在与*
,**
,&
进行混合,但我不确定如何找出问题所在。
使用GCC 4.8.1。