以下C代码中存在内存泄漏
对于下面的代码示例,存在内存泄漏。在以下情况下,我们如何防止内存泄漏:以下C代码中存在内存泄漏,c,memory-management,memory-leaks,valgrind,C,Memory Management,Memory Leaks,Valgrind,对于下面的代码示例,存在内存泄漏。在以下情况下,我们如何防止内存泄漏: 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct sample_help { 5 int *value; 6 void **pointers; 7 }*samp
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct sample_help {
5 int *value;
6 void **pointers;
7 }*sample,sample_node;
8
9
10 sample main(){
11 sample ABC=NULL;
12 sample XYZ=NULL;
13 sample kanchi = NULL;
14
15 ABC = malloc(sizeof(sample_node));
16 XYZ = malloc(sizeof(sample_node));
17 ABC->pointers = malloc(5*sizeof(void *));
18 XYZ->pointers = malloc(5*sizeof(void *));
19 ABC->value = malloc(5*sizeof(int));
20 XYZ->value = malloc(5*sizeof(int));
21
22 ABC->value[0] = 10;
23 ABC->value[1] = 20;
24
25 XYZ->pointers[0] = ABC;
26 kanchi = XYZ->pointers[0];
27
28 printf("::::%d\n",XYZ->pointers[0]);
29 printf("kanchi1:::::%d\n",kanchi->value[0]);
30
31
32 return XYZ;
33 }
34
在不再需要时释放已用内存:
free(ABC->value);
free(XYZ->value);
free(ABC->pointers);
free(XYZ->pointers);
free(ABC);
free(XYZ);
顺便说一句:当这是整个计划时,其实并不重要。由于操作系统在进程结束时回收所有未释放的内存,因此不需要释放在程序终止前一直在使用的内存。但是,这是一种很好的做法。当不再需要时,只需释放已使用的内存即可:
free(ABC->value);
free(XYZ->value);
free(ABC->pointers);
free(XYZ->pointers);
free(ABC);
free(XYZ);
顺便说一句:当这是整个计划时,其实并不重要。由于操作系统在进程结束时回收所有未释放的内存,因此不需要释放在程序终止前一直在使用的内存。但是,这是一个很好的实践。现在已经在注释中阅读了您的更新,分配和返回内存的模块(不称为main)就可以了 无论哪个模块使用从模块返回的值,都需要在使用后释放数据。因此,如果您将模块实现为
sample mymodule(void)
{
sample foo = malloc(10);
/* set up contents of foo as required */
return foo;
}
然后mymodule的调用者将如下所示:
int main (int argc, char *argv[])
{
sample bar = mymodule();
/* use contents of bar as required */
free(bar);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct sample_help {
int *value;
void **pointers;
} *sample, sample_node;
sample foo(void)
{
sample ABC=NULL;
sample XYZ=NULL;
sample kanchi = NULL;
ABC = malloc(sizeof(sample_node));
XYZ = malloc(sizeof(sample_node));
ABC->pointers = malloc(5*sizeof(void *));
XYZ->pointers = malloc(5*sizeof(void *));
ABC->value = malloc(5*sizeof(int));
XYZ->value = malloc(5*sizeof(int));
ABC->value[0] = 10;
ABC->value[1] = 20;
XYZ->pointers[0] = ABC;
kanchi = XYZ->pointers[0];
printf("::::%d\n",XYZ->pointers[0]);
printf("kanchi1:::::%d\n",kanchi->value[0]);
return XYZ;
}
int main(void)
{
// call your function
sample xyz = foo();
// ... do something with the data structure xyz ...
// free memory allocated by your function
free(xyz->pointers[0]->value); // free ABC->value
free(xyz->pointers[0]->pointers); // free ABC->pointers
free(xyz->pointers[0]); // free ABC
free(xyz->value); // free XYZ->value
free(xyz->pointers); // free XYZ->pointers
free(xyz); // free XYZ
return 0;
}
现在在注释中读取更新后,分配并返回内存的模块(不称为main)就可以了 无论哪个模块使用从模块返回的值,都需要在使用后释放数据。因此,如果您将模块实现为
sample mymodule(void)
{
sample foo = malloc(10);
/* set up contents of foo as required */
return foo;
}
然后mymodule的调用者将如下所示:
int main (int argc, char *argv[])
{
sample bar = mymodule();
/* use contents of bar as required */
free(bar);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct sample_help {
int *value;
void **pointers;
} *sample, sample_node;
sample foo(void)
{
sample ABC=NULL;
sample XYZ=NULL;
sample kanchi = NULL;
ABC = malloc(sizeof(sample_node));
XYZ = malloc(sizeof(sample_node));
ABC->pointers = malloc(5*sizeof(void *));
XYZ->pointers = malloc(5*sizeof(void *));
ABC->value = malloc(5*sizeof(int));
XYZ->value = malloc(5*sizeof(int));
ABC->value[0] = 10;
ABC->value[1] = 20;
XYZ->pointers[0] = ABC;
kanchi = XYZ->pointers[0];
printf("::::%d\n",XYZ->pointers[0]);
printf("kanchi1:::::%d\n",kanchi->value[0]);
return XYZ;
}
int main(void)
{
// call your function
sample xyz = foo();
// ... do something with the data structure xyz ...
// free memory allocated by your function
free(xyz->pointers[0]->value); // free ABC->value
free(xyz->pointers[0]->pointers); // free ABC->pointers
free(xyz->pointers[0]); // free ABC
free(xyz->value); // free XYZ->value
free(xyz->pointers); // free XYZ->pointers
free(xyz); // free XYZ
return 0;
}
您的代码应该更像这样:
int main (int argc, char *argv[])
{
sample bar = mymodule();
/* use contents of bar as required */
free(bar);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct sample_help {
int *value;
void **pointers;
} *sample, sample_node;
sample foo(void)
{
sample ABC=NULL;
sample XYZ=NULL;
sample kanchi = NULL;
ABC = malloc(sizeof(sample_node));
XYZ = malloc(sizeof(sample_node));
ABC->pointers = malloc(5*sizeof(void *));
XYZ->pointers = malloc(5*sizeof(void *));
ABC->value = malloc(5*sizeof(int));
XYZ->value = malloc(5*sizeof(int));
ABC->value[0] = 10;
ABC->value[1] = 20;
XYZ->pointers[0] = ABC;
kanchi = XYZ->pointers[0];
printf("::::%d\n",XYZ->pointers[0]);
printf("kanchi1:::::%d\n",kanchi->value[0]);
return XYZ;
}
int main(void)
{
// call your function
sample xyz = foo();
// ... do something with the data structure xyz ...
// free memory allocated by your function
free(xyz->pointers[0]->value); // free ABC->value
free(xyz->pointers[0]->pointers); // free ABC->pointers
free(xyz->pointers[0]); // free ABC
free(xyz->value); // free XYZ->value
free(xyz->pointers); // free XYZ->pointers
free(xyz); // free XYZ
return 0;
}
请注意,完成后,我们将数据结构从main中释放出来。您的代码应该更像这样:
int main (int argc, char *argv[])
{
sample bar = mymodule();
/* use contents of bar as required */
free(bar);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct sample_help {
int *value;
void **pointers;
} *sample, sample_node;
sample foo(void)
{
sample ABC=NULL;
sample XYZ=NULL;
sample kanchi = NULL;
ABC = malloc(sizeof(sample_node));
XYZ = malloc(sizeof(sample_node));
ABC->pointers = malloc(5*sizeof(void *));
XYZ->pointers = malloc(5*sizeof(void *));
ABC->value = malloc(5*sizeof(int));
XYZ->value = malloc(5*sizeof(int));
ABC->value[0] = 10;
ABC->value[1] = 20;
XYZ->pointers[0] = ABC;
kanchi = XYZ->pointers[0];
printf("::::%d\n",XYZ->pointers[0]);
printf("kanchi1:::::%d\n",kanchi->value[0]);
return XYZ;
}
int main(void)
{
// call your function
sample xyz = foo();
// ... do something with the data structure xyz ...
// free memory allocated by your function
free(xyz->pointers[0]->value); // free ABC->value
free(xyz->pointers[0]->pointers); // free ABC->pointers
free(xyz->pointers[0]); // free ABC
free(xyz->value); // free XYZ->value
free(xyz->pointers); // free XYZ->pointers
free(xyz); // free XYZ
return 0;
}
请注意,完成后,我们将从main中释放数据结构。@thetna您确定这合法吗?@thetna。。。你的逻辑真的被破坏了。@thetna为了澄清破坏的逻辑,它就像说0的平方是0,1的平方是1,所以,任何数的平方就是它本身。@thetna:好的-在这种情况下,你应该实现这是一个正确的函数,如果你想测试它,然后从main调用它。@thetna:请参阅下面的答案,了解如何在不滥用main的情况下正确地实现它,以及如何在完成后释放调用者中的内存。@thetna你确定这是合法的吗?@thetna。。。你的逻辑真的被破坏了。@thetna为了澄清破坏的逻辑,它就像说0的平方是0,1的平方是1,所以,任何数的平方都是它本身。@thetna:好的-在这种情况下,您应该实现这是一个正确的函数,如果您想测试它,然后从main调用它。@thetna:请参阅下面的答案,了解如何在不滥用main的情况下正确实现它,以及如何在完成后释放调用者中的内存。使用示例main,我们知道没有OS:PIf i释放XYZ然后如何返回结构?我想返回XYZ,并在XYZ指针中指定ABC。让我们做一个类比,在根节点中添加一个节点。在这种情况下,main的调用者需要在使用这些值后释放内存。以这种方式使用main是非常奇怪的,你能描述更多关于你正在做的事情的更广泛的背景吗?你永远不知道什么时候别人甚至会在6个月后,在不检查代码的情况下,拿走你的代码。例如,重命名这个主函数,以便它可以在更深的程序中使用。因为这个潜力虽小,但我不认为适当的内存管理仅仅是一个好的实践,我认为它是一个坚实的要求。返回指向它的指针,然后接收它的函数负责稍后释放它。使用示例main,我们知道没有操作系统:PIf i free XYZ,那么如何返回结构?我想返回XYZ,并在XYZ指针中指定ABC。让我们进行一个类比,根节点中添加了一个节点。在这种情况下,main的调用者需要在使用这些值后释放内存。以这种方式使用main是非常奇怪的,你能描述更多关于你正在做的事情的更广泛的背景吗?你永远不知道什么时候别人甚至会在6个月后,在不检查代码的情况下,拿走你的代码。例如,重命名这个主函数,以便它可以在更深的程序中使用。因为这个潜力即使很小,我也不认为适当的内存管理仅仅是一个好的实践,我认为它是一个坚实的要求。不要免费XYZ,返回一个指针,然后接收它的函数负责以后释放它。