簡體   English   中英

仍然可以到達的方塊-Valgrind

[英]Still reachable blocks - valgrind

嗨,我在釋放代碼中的數組時遇到問題。

 int main(){
int **pole = mallocator(); ...

在主要我調用分配內存並看起來這樣的函數:

int ** mallocator(){
int **pole = (int **) malloc(sizeof(int*)*RADEK);

for(int i=0; i < RADEK; i++){ 
    pole[i] = (int *) malloc(sizeof(int)*RADEK); 

 }

return pole;
}

最后,我使用以下功能將其釋放:

void freedom(int **pole){

for(int i=0; i < RADEK; i++){ 
    printf("%d\n", i);
    free(pole[i]);
}
free(pole);
}

RADEK的值恆定為25。我認為它起作用,但是valgrind說我有27個分配和26個釋放,並且說仍然可以到達一個塊。 關於如何實現無泄漏的任何想法? 謝謝

編輯:返回行不應該已經被嚴重復制的循環中,感謝您的注意。 也可能是編譯器的錯誤。 如果我使用gcc而不是g ++,則表示沒有泄漏,但是當我使用g ++進行編譯時,它說仍然可以實現靜態:72,704,即使我在代碼中未使用malloc。

如注釋中所指定,在更正了mallocator()函數之后。

然后main()函數可以通過以下方式釋放分配的內存:

for( int i=0; i<RADEK; i++ )
{
    free( pole[i]);
}
free( pole );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM