2012-04-25 2 views
3

그래프를 관리하는 간단한 함수를 작성하고 있습니다. 내 프로그램을 실행하면sYSMALLOc : 어설 션이 실패했습니다 - 어떻게 해결할 수 있습니까?

다음과 같은 상황이 발생 오류 :

malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) 
&((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) 
&& old_size == 0) || ((unsigned long) (old_size) >= (unsigned long) 
((((__builtin_offsetof (struct malloc_chunk, fd_nextsize)) 
+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) 
&& ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. 

내가 Valgrind의를 실행하고 저에게이 오류가 표시됩니다 : 이것은 내 구조체

typedef struct GRAPH { 
    int id; 
    int destination; 
    int cost; 
    struct GRAPH *next; 
    struct GRAPH *prev; 
} GRAPH; 

==5903== Memcheck, a memory error detector 
==5903== Invalid write of size 4 
==5903== at 0x8048866: creategraph 
==5903== by 0x8048718: readFile 
==5903== by 0x80486BF: main 
==5903== Address 0x41c3204 is 0 bytes after a block of size 4 alloc'd 
==5903== at 0x4027ABA: malloc (vg_replace_malloc.c:263) 
==5903== by 0x8048850: createGraph 
==5903== by 0x8048718: readFile 
==5903== by 0x80486BF: main 

입니다 내 함수는 readFile입니다.

void readFile() { 
    FILE *f = NULL; 
    char c; 
    f = fopen("g.txt", "r"); 
    if (f == NULL) { 
    puts("Error"); 
    } 
    int line = 0, column = 0; 
    g = createGraph(16); 
    while (!feof(f)) { 
    c = fgetc(f); 
    if (c == '\n') { 
     line++; 
    } else if (c == '1') { 
     createEdge(line, column, 1, g); 
     column++; 
    } 
    } 
    fclose(f); 
} 

이 제 기능 createGraph

graph **creatgraph(int tV) { 
    int i; 
    graph **v; 
    v = (graph**) malloc(sizeof (graph*)); 
    if (v == NULL) { 
     puts("Error"); 
     exit(EXIT_FAILURE); 
    } 
    for (i = 0; i < tV; i++) { 
     v[i] = NULL; 
    } 
    return v; 
} 

인이 내 기능 createVertex

graph *createVertex() { 
    graph *newVertex = NULL; 
    newVertex = (graph*) malloc(sizeof (graph)); 
    if (newVertex == NULL) { 
     puts("Error"); 
     exit(EXIT_FAILURE); 
    } 
    newVertex->id = 0; 
    newVertex->destination = 0; 
    newVertex->cost = 1; 
    newVertex->next = NULL; 
    novoVertice->prev = NULL; 
    return (newVertex); 
} 

void createEdge(int vStart, int vFinal, int id, graph** g) { 
    graph *newVertex = createVertex(); 
    newVertex->destination = vFinal; 
    newVertex->id = id; 
    g[vFinal] = insertLast(g[vStart], newVertex); 
} 

는 당신의 도움을 주셔서 감사합니다 내 기능 createEdge입니다.

+0

오늘도 비슷한 문제가있었습니다. http://stackoverflow.com/questions/10309113/assertion-in-malloc-c2453 –

답변

5

가 바로 여기에있는 메모리 손상 버그 :

v = (graph**) malloc(sizeof (graph*)); 
    ... 
    for (i = 0; i < tV; i++) { 
     v[i] = NULL; 
    } 

당신은 단지 하나의 graph* 포인터에 대한 스토리지를 할당하고는, 아직이 tV 같은 포인터에 대한 충분한 것처럼 할당 된 블록을 처리합니다.

v = (graph**) malloc(tV * sizeof (graph*)); 
+0

문제가 해결되었습니다. 정말 고마워요. –

1

당신은 하나 개의 요소 포인터 배열을 할당했다 : 해결하기 위해

는 해당 malloc() 전화를 변경합니다. 시도 :

graph **creatgraph(int tV) { 
    int i; 
    graph **v; 
    v = malloc(tV * sizeof *v); 
    if (v == NULL) { 
     puts("Error"); 
     exit(EXIT_FAILURE); 
    } 
    for (i = 0; i < tV; i++) { 
     v[i] = NULL; 
    } 
    return v; 
} 
+0

문제가 해결되었습니다. 대단히 감사합니다. –

1

이 오류는 사용자가 어딘가에서 메모리가 손상되었다는 의미입니다. Valgrind로 코드를 실행하여 프로그램이 실패한 곳을 찾으십시오.

관련 문제