[英]Still reachable blocks - valgrind
Hi I have problem with freeing array in my code. 嗨,我在释放代码中的数组时遇到问题。
int main(){
int **pole = mallocator(); ...
In main I call function which allocates memory and looks this: 在主要我调用分配内存并看起来这样的函数:
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;
}
And in the end i free it with this function: 最后,我使用以下功能将其释放:
void freedom(int **pole){
for(int i=0; i < RADEK; i++){
printf("%d\n", i);
free(pole[i]);
}
free(pole);
}
RADEK is constant with value 25. I thought it worked, but valgrind says I have 27 allocs and 26 frees and says that one block is still reachable. RADEK的值恒定为25。我认为它起作用,但是valgrind说我有27个分配和26个释放,并且说仍然可以到达一个块。 Any ideas on how to achieve no leaks?
关于如何实现无泄漏的任何想法? Thanks
谢谢
EDIT:The return line shouldn't have been in for cycle that was badly copied, thanks for noticing. 编辑:返回行不应该已经被严重复制的循环中,感谢您的注意。 Also it probably was a bug of compiler.
也可能是编译器的错误。 If I use gcc instead of g++ it says there are no leaks, but when I compile with g++ it says static still reachable: 72,704 even when I don't use malloc in the code.
如果我使用gcc而不是g ++,则表示没有泄漏,但是当我使用g ++进行编译时,它说仍然可以实现静态:72,704,即使我在代码中未使用malloc。
after correcting the mallocator()
function, as specified in the comments. 如注释中所指定,在更正了
mallocator()
函数之后。
Then the main()
function can free the allocated memory via: 然后
main()
函数可以通过以下方式释放分配的内存:
for( int i=0; i<RADEK; i++ )
{
free( pole[i]);
}
free( pole );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.