简体   繁体   English

免费提供一致的动态分配的char指针吗?

[英]Free for consistent dynamically allocated char pointers?

I have a struct containing a bunch of char pointers whose values are used throughout the program's lifetime. 我有一个结构,其中包含一堆char指针,其值在程序的整个生命周期内都使用。 Most are overwritten every iteration. 大多数在每次迭代时都会被覆盖。

Should these values be freed at any point besides program exit? 除了程序退出外,是否还应在任何时候释放这些值? Should the value in the char pointer be freed before overwriting it with strdup("new value") ? 在用strdup("new value")覆盖char指针中的值之前,应该释放它吗?

@some-programmer-dude is right, there will be memory leak without free if your program works like a server. @ some-programmer-dude是正确的,如果您的程序像服务器一样工作,则会出现内存泄漏而没有空闲。

BTW, all memory will be released after the program exited no matter whether you call free() or not. 顺便说一句,无论您是否调用free(),程序退出后都会释放所有内存。

You already got the answer. 您已经知道答案了。 I'm just providing more information and reference to it. 我只是提供更多信息和参考。

Man page of POSIX standard strdup . POSIX标准strdup的手册页。

Here we go, 开始了,

  The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3). 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM