我写它的相关细节低于C程序:为什么会产生这一核心转储
void calculate(struct iso_matrix *iso_matrix) {
struct graphlet *graphlet = init_graphlet(GL_SIZE);
int *index_map = (int *)malloc(iso_matrix->n_rw_col);
//some other stuff. Working fine.
free(index_map); //line 90(for future references)
}
输出我在终点站下车:
*** glibc detected *** ./bin/exec: free(): invalid next size (fast):0x00000000023696f0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x2b3b5fc92b96]
./bin/exec[0x403ff9]
./bin/exec[0x4049eb]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x2b3b5fc3576d]
./bin/exec[0x400889]
======= Memory map: ========
(not shown here)
而且GDB回溯是:
#0 0x00007ffff7a51425 in raise() from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff7a54b8b in abort() from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff7a8f39e in ??() from /lib/x86_64-linux-gnu/libc.so.6
#3 0x00007ffff7a99b96 in ??() from /lib/x86_64-linux-gnu/libc.so.6
#4 0x0000000000403ff9 in calculate (iso_matrix=0x6084a0) at src/graphlet_iso_mat.c:90
#5 0x00000000004049eb in main (argc=3, argv=0x7fffffffdef8) at src/main.c:70
我无法理解为什么会发生这种情况或如何调试。任何帮助赞赏。
[编辑]完整的calculate
功能:
void calculate(struct iso_matrix *iso_matrix)
{
printf("Calculate called\n");
struct graphlet *graphlet = init_graphlet(GL_SIZE);
int *index_map = (int *)malloc(iso_matrix->n_rw_col);
struct graph *graph = init_graph(0, GL_SIZE); /*Small graph so prefered matrix representation.*/
/*Initialize the list_head.*/
if(!iso_matrix->unique)
iso_matrix->unique = init_listhead();
for(int i=0; i<iso_matrix->n_rw_col; ++i)
{
graphlet_to_graph(graphlet, graph);
calc_heuristic(graph, 3);
/*check_unique() compares only between same type of graphs.*/
index_map[i] = check_unique(iso_matrix->unique, graph);
if(index_map[i]==-1)
{
struct graph *cpy=init_graph(0, GL_SIZE);
cpy_graph(graph, cpy);
int *graphlet_no = (int *)malloc(sizeof(int));
*graphlet_no = i;
struct container *container = (struct container *)malloc(sizeof(struct container));
container->data = (void *)cpy;
container->id = (void *)graphlet_no;
struct list_node *list_node = init_listnode((void *)container);
add_to_list(list_node, iso_matrix->unique);
}
else
{
*(*((iso_matrix->iso_mat)+index_map[i])+i) = 1;
}
inc_graphlet(graphlet);
reset_graph(graph);
}
for(int i=0; i<iso_matrix->n_rw_col; ++i)
{
if(index_map[i]==-1) /*If same then continue.*/
continue;
for(int j=0; j<iso_matrix->n_rw_col; ++j)
*(*((iso_matrix->iso_mat)+i)+j) = *(*((iso_matrix->iso_mat)+index_map[i])+j);
}
/*Destroying allocated memory.*/
free(index_map);
}
它告诉你,free()得到了一个指针,看起来不像它来自malloc()。可能有些东西在“//一些其他的东西”部分是用指针或覆盖它不应该覆盖的内存。仅仅因为代码有效,这并不意味着它是正确的。 :-) – 2013-04-22 00:25:00
这可能是一个缓冲区溢出的地方。内存分配和释放通常检测堆结构中的损坏并发出警告和断言。如果可能的话,在'free'之前在调试器中断开并查看'index_map'周围的内存。您可能会覆盖分配给“index_map”的内存之前的空间中动态分配的缓冲区。 – 2013-04-22 00:20:03
我检查了代码,它看起来不像我覆盖的东西。你能指出我可以从哪里读取这种形式的调试是使用GDB完成的。我也是GDB新手。另请参阅编辑的问题。 – 2013-04-22 00:32:52