尝试释放指针时出现段错误

时间:2018-09-19 22:11:50

标签: segmentation-fault free

我有这样的代码

void find_groupings ()

  int *bandwidths;
  int *execution_time;

  bandwidths = (int *)malloc(sizeof(int)*node_count); // node_count is glbl
  execution_time = (int *)malloc(sizeof(int)*node_count);
  //other mallocs, other code etc

  while (condition) {
    // lot of code
    find_bandwidths(bandwidths);
    find_execution_time(execution_time);
    //lot of code
  }

  free(bandwidths);
  free(execution_time);

}

在“ free(execution_time);”行的代码段错误

Thread 1 "vx_tutorial_exe" received signal SIGSEGV, Segmentation fault.
0xf7dd0cd9 in _int_free (av=0xf7f15780 <main_arena>, p=<optimized out>, have_lock=0) at malloc.c:4005
4005    malloc.c: No such file or directory.

我可以保证在find_execution_time()内部“ execution_time”不会超出范围 我相信我会释放我在代码中所做的每一个malloc

还发现在使用gdb在free()崩溃之前,execution_time的指针值是相同的

尝试了valgrind,但由于程序出现段故障,这没有帮助

这里可能是什么问题?

1 个答案:

答案 0 :(得分:0)

整个问题是,在malloc中,我用错误的类型强制转换了

对于我已将其分配为的缓冲区

buffers  = (int *)malloc(sizeof(int)*node_count);

应该是

buffers  = (buffer *)malloc(sizeof(buffer)*node_count);

缓冲区是我代码中的一种结构类型。

太奇怪了,它崩溃了,无法根据错误消息找到它。

谢谢杰里米!和pm100

相关问题