c - 为什么 malloc 在这里返回 NULL?

标签 c pointers null malloc

所以我的程序总是返回一个段错误,但我不明白为什么要尝试使用 GDB 进行调试,它向我展示了这个:

(gdb) backtrace
#0  0x001a98ef in _int_malloc (av=0x2d8440, bytes=8) at malloc.c:3835
#1  0x001abedc in __GI___libc_malloc (bytes=8) at malloc.c:2924
#2  0x0804cd6a in init_capsula (item1_=2, item2_=2)
    at src/modulos/modulos_auxiliares/capsula/capsula.c:25
#3  0x0804d366 in total_dados_produto (f=0x8055838, filial=0x0, mes=6, 
    cod=0xbffff23c "AF1184") at src/modulos/faturacao/faturacao.c:208
#4  0x0804b237 in queries (q=3, c1=0x0, c2=0x0, f=0x8055838, v=0x0) at src/interface.c:815
#5  0x0804b6f4 in menu (c1=0x8055008, c2=0x8055420, f=0x8055838, v=0x0) at src/interface.c:976
#6  0x080487ad in main () at src/interface.c:1037

然后我确定问题的根源来自第 2 帧,因此决定检查它并获得以下输出:

(gdb) frame 2
#2  0x0804cd6a in init_capsula (item1_=2, item2_=2)
    at src/modulos/modulos_auxiliares/capsula/capsula.c:25
25          c->item1 = (int*) malloc((sizeof (int))*item1_);

它告诉我 malloc 正在返回 NULL,但是我看不到这一行的问题,一切都已正确初始化,正如我在下一步操作中确认的那样:

(gdb) print ((sizeof (int))*item1_)
$1 = 8

为什么 malloc 不能分配这么小的空间?我在这里忽略了一些非常愚蠢的事情吗???

我会把函数 init_capsula 放在这里(那个 malloc 所在的地方)给你们看:

Capsula init_capsula(int item1_, int item2_){
     Capsula c = (Capsula) malloc (sizeof (struct capsula));

     c->tipo  = -1;

     if (item1_ > 0)
         c->item1 = (int*) malloc((sizeof (int))*item1_); /*Problematic line*/
     else c->item1 = NULL;

     if (item2_ > 0)
         c->item2 = (float*) malloc((sizeof (float))*item2_);
     else c->item2 = NULL;

     c->q1 = 0;
     c->q2 = 0;

     return c;
}

Capsula 是指向如下定义的结构的指针:

struct capsula{
    int tipo;

    int     q1;
    int *item1;

    int       q2;
    float *item2;
 };

编辑:

如果我尝试使用以下命令运行 valgrind:

     valgrind --tool=memcheck --leak-check=full make run

它输出了这个,我觉得没什么用。

    make: *** [run] Segmentation fault (core dumped)
    ==5848== 
    ==5848== HEAP SUMMARY:
    ==5848==     in use at exit: 62,771 bytes in 1,819 blocks
    ==5848==   total heap usage: 6,060 allocs, 4,241 frees, 580,609 bytes allocated
    ==5848== 
    ==5848== LEAK SUMMARY:
    ==5848==    definitely lost: 0 bytes in 0 blocks
    ==5848==    indirectly lost: 0 bytes in 0 blocks
    ==5848==      possibly lost: 0 bytes in 0 blocks
    ==5848==    still reachable: 62,771 bytes in 1,819 blocks
    ==5848==         suppressed: 0 bytes in 0 blocks
    ==5848== Reachable blocks (those to which a pointer was found) are not shown.
    ==5848== To see them, rerun with: --leak-check=full --show-reachable=yes
    ==5848== 
    ==5848== For counts of detected and suppressed errors, rerun with: -v
    ==5848== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

编辑2:

我终于通过正确使用 valgrind 理解了这个问题,因为我在 make 上使用它,而我应该在程序本身上使用它(如评论中所示)。问题出在一个非常不同的地方,在一个我忘记写 malloc 的地方,感谢大家的帮助,现在我终于明白我应该如何使用 valgrind

最佳答案

如果调试器显示您在此行触发了段错误:

c->item1 = (int*) malloc((sizeof (int))*item1_);

这可能意味着两件事:

  • c是一个坏指针,可能是NULL,但是前面的语句c->typo = -1;应该有也失败了。

  • 竞技场可能已损坏,问题出在到达那里之前执行的代码中。

关于c - 为什么 malloc 在这里返回 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36019287/

相关文章:

c - 在 Win32 API 中从命令行解析的字符的未处理异常

c - 检查 c 指针时出现段错误(LCTHW - Ex17,额外学分)

Java:没有空格的空字符

java - doStuff 并返回 boolean 值是不好的做法吗?

c - 如何使用指针更改方法内的值

c - fgets() 在返回 NULL 和返回正确值之间随机变化

c - 读取字符串时如何排除空终止符?

c - 在单个 printf 调用中打印来自 argv 字符串的 ascii 值

c - 在这些声明下,以下表达式是什么类型,它们是否正确?

c - 在 C 中转换指针时出错