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 に答える