繁体   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