我有一个包含一堆 char 指针的结构,它们的值在程序的整个生命周期中都会使用。大多数在每次迭代时都会被覆盖。
除了程序退出之外,是否应该在任何时候释放这些值?char 指针中的值是否应该在用 覆盖之前被释放strdup("new value")
?
@some-programmer-dude 是对的,如果你的程序像服务器一样工作,就会出现内存泄漏。
顺便说一句,无论您是否调用 free(),程序退出后都会释放所有内存。
你已经得到了答案。我只是提供更多信息和参考。
POSIX 标准strdup的手册页。
开始了,
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).