当我使用它时
valgrind --leak-check=yes ./Main
我有大约 185236 个错误。它说:
xx bytes in x blocks are possibly lost in loss record xxxx of xxxx
这是我的代码:
Node InsertString(Head head, Node tree, char* data) {
if (tree == NULL) {
tree = malloc(sizeof (struct TreeNode)); //Error
if (tree == NULL) {
printf("Out of Space!\n");
} else {
tree->theData = malloc(sizeof (char) * strlen(data));//Error
strcpy(tree->theData, data);
}
} else {
if (strcmp(data, tree->theData) < 0) {
tree->Left = InsertString(head, tree->Left, data); //Error
} else {
if (strcmp(data, tree->theData) > 0) {
tree->Right = InsertString(head, tree->Right, data);//Error
}
}
}
}
return tree;
}
谢谢!
最佳答案
你有没有调用过free
来释放你用malloc
分配的内存?
如果不是,好吧,那么你正在泄漏所有的内存。
关于c - 为什么我的程序有内存泄漏检查错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4543774/