2016-09-07 27 views
3

代碼:內存錯誤嘗試空閒指針在結構 - Visual C

#include <stdio.h> 
#include <stdlib.h> 

typedef struct Nodo{ 
    int valor; 
    struct Nodo* hijo1; 
    struct Nodo* hijo2; 
}Nodo; 

typedef Nodo* Arreglo; 

Arreglo INIC_ARR(int longitud); 
void IMP_ARR(Arreglo A,int longitud); 
void INIC_ARBOL(Arreglo A, int longitud); 
void FREE_ARBOL(Arreglo A, int longitud); 


int main(){ 
    Arreglo A; 
    int longitud = 10; 
    A = INIC_ARR(10); 
    INIC_ARBOL(A,longitud); 
    IMP_ARR(A,longitud); 
    FREE_ARBOL(A,longitud); 
    return 0; 
} 

Arreglo INIC_ARR(int longitud){ 
    int i; 
    Arreglo A = (Arreglo)calloc(longitud,sizeof(Nodo)); 
    for(i = 0; i < longitud; i++){ 
     A[i].valor = rand()%10; 
    } 
    return A; 
} 

void IMP_ARR(Arreglo A,int longitud){ 
    int i; 
    for(i = 0;i < longitud; i++){ 
     printf("[%d,",A[i].valor); 
     if(A[i].hijo1 == NULL){ 
      printf("-,"); 
     } 
     else{ 
      printf("%d,",(*(A[i].hijo1)).valor); 
     } 
     if(A[i].hijo2 == NULL){ 
      printf("-]"); 
     } 
     else{ 
      printf("%d]",(*(A[i].hijo2)).valor); 
     } 

    } 
    printf("\n"); 
} 

void INIC_ARBOL(Arreglo A, int longitud){ 
    int i; 
    for(i = 0; i < longitud; i++){ 
     if(2*i + 1 < longitud) 
      A[i].hijo1 = &A[2*i + 1]; 
     if(2*i + 2 < longitud) 
      A[i].hijo2 = &A[2*i + 2]; 
    } 
} 


void FREE_ARBOL(Arreglo A, int longitud){ 
    int i; 
    for(i = 0; i < longitud; i++){ 
     free(A[i].hijo1); 
     free(A[i].hijo2); 
    } 
    free(A); 
} 

我創建結構的arrary(Nodo);每個都有兩個在INIC_ARBOL函數中初始化的指針。我試圖打印,然後使用FREE_ARBOL釋放他們,然後這說明了:

[3,6,7][6,5,3][7,5,6][5,2,9][3,1,-][5,-,-][6,-,-][2,-,-][9,-,-][1,-,-] 
*** Error in `./a.out': free(): invalid pointer: 0x0000000000a31028 *** 
======= Backtrace: ========= 
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f3f8adea725] 
/lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7f3f8adf2f4a] 
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3f8adf6abc] 
./a.out[0x40096b] 
./a.out[0x400696] 
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3f8ad93830] 
./a.out[0x400579] 
======= Memory map: ======== 
00400000-00401000 r-xp 00000000 08:0a 140095        /home/luis/Documentos/Programacion/C/a.out 
00600000-00601000 r--p 00000000 08:0a 140095        /home/luis/Documentos/Programacion/C/a.out 
00601000-00602000 rw-p 00001000 08:0a 140095        /home/luis/Documentos/Programacion/C/a.out 
00a31000-00a52000 rw-p 00000000 00:00 0         [heap] 
7f3f84000000-7f3f84021000 rw-p 00000000 00:00 0 
7f3f84021000-7f3f88000000 ---p 00000000 00:00 0 
7f3f8ab5d000-7f3f8ab73000 r-xp 00000000 08:09 135547      /lib/x86_64-linux-gnu/libgcc_s.so.1 
7f3f8ab73000-7f3f8ad72000 ---p 00016000 08:09 135547      /lib/x86_64-linux-gnu/libgcc_s.so.1 
7f3f8ad72000-7f3f8ad73000 rw-p 00015000 08:09 135547      /lib/x86_64-linux-gnu/libgcc_s.so.1 
7f3f8ad73000-7f3f8af33000 r-xp 00000000 08:09 141748      /lib/x86_64-linux-gnu/libc-2.23.so 
7f3f8af33000-7f3f8b132000 ---p 001c0000 08:09 141748      /lib/x86_64-linux-gnu/libc-2.23.so 
7f3f8b132000-7f3f8b136000 r--p 001bf000 08:09 141748      /lib/x86_64-linux-gnu/libc-2.23.so 
7f3f8b136000-7f3f8b138000 rw-p 001c3000 08:09 141748      /lib/x86_64-linux-gnu/libc-2.23.so 
7f3f8b138000-7f3f8b13c000 rw-p 00000000 00:00 0 
7f3f8b13c000-7f3f8b162000 r-xp 00000000 08:09 141744      /lib/x86_64-linux-gnu/ld-2.23.so 
7f3f8b340000-7f3f8b343000 rw-p 00000000 00:00 0 
7f3f8b35e000-7f3f8b361000 rw-p 00000000 00:00 0 
7f3f8b361000-7f3f8b362000 r--p 00025000 08:09 141744      /lib/x86_64-linux-gnu/ld-2.23.so 
7f3f8b362000-7f3f8b363000 rw-p 00026000 08:09 141744      /lib/x86_64-linux-gnu/ld-2.23.so 
7f3f8b363000-7f3f8b364000 rw-p 00000000 00:00 0 
7fffddc61000-7fffddc82000 rw-p 00000000 00:00 0       [stack] 
7fffddd39000-7fffddd3b000 r--p 00000000 00:00 0       [vvar] 
7fffddd3b000-7fffddd3d000 r-xp 00000000 00:00 0       [vdso] 
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0     [vsyscall] 
Abortado (` 

核心」 generado)

我有像Valgrind的工具,零知識,所以如果你能解釋我慢慢做什麼要解決我會很感激。

+0

你有零知識,好吧,不要羞恥,爲什麼不開始聚集一些?我想是時候了。 –

+0

OT:UPPER中的函數名稱通常用於類函數宏,而不是用於普通函數。 –

回答

1

在此功能

Arreglo INIC_ARR(int longitud){ 
    int i; 
    Arreglo A = (Arreglo)calloc(longitud,sizeof(Nodo)); 
    for(i = 0; i < longitud; i++){ 
     A[i].valor = rand()%10; 
    } 
    return A; 
} 

有被分配使用語句

Arreglo A = (Arreglo)calloc(longitud,sizeof(Nodo)); 

的存儲器中的一個程度,否則也可以在動態分配的數組的不自由元件

void FREE_ARBOL(Arreglo A, int longitud){ 
    int i; 
    for(i = 0; i < longitud; i++){ 
     free(A[i].hijo1); 
     free(A[i].hijo2); 
    } 
    free(A); 
} 

,因爲它們是部分程度,並沒有動態分配自己。

+0

所以爲了「釋放」這兩個指針,將會: A [i] .hijo1 = NULL; A [i] .hijo2 = NULL; 夠了嗎? –

+0

@Luis_V你可以將它們設置爲NULL。還要考慮到,例如,函數INIC_ARBOL僅將該數據字段設置爲少於一半的節點。 –

+0

是的。我只需要初始化一些「hijo」指針。非常感謝您的幫助,善良的用戶C: –

1

不要傳遞既沒有從內存管理函數返回的內存管理函數也沒有空指針,否則將調用未定義的行爲

從功能FREE_ARBOL()卸下有問題的部分

for(i = 0; i < longitud; i++){ 
    free(A[i].hijo1); 
    free(A[i].hijo2); 
} 

1

短篇小說:你只有一個calloc,所以你應該只有一個free

此外,由於您在INIC_ARBOL之內有這些if s(例如if (2*i + 1 < longitud)),您陣列中的半個節點甚至不指向任何東西。我會在代碼的其餘部分完全刪除typedef Nodo *Arreglo,並且只使用Node*,這將使讀取代碼變得更容易。

+0

感謝您的建議 –