0

我正在编写一个 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);
}
4

1 回答 1

1

我打赌这个:

int *index_map = (int *)malloc(iso_matrix->n_rw_col);

是指指向iso_matrix->n_rw_col 整数的分配。您忘记了字节计算中整数的大小:

int *index_map = malloc(iso_matrix->n_rw_col * sizeof(*index_map));

可能还有其他问题,但这显然是一个大问题。注意:我还删除了malloc()你不应该在 C 代码中做的演员表。确保stdlib.h包含在#include此源文件顶部的列表中。

于 2013-04-22T00:47:15.637 回答