在_SUCCESS和_FAILURE上退出()释放分配的内存

时间:2021-02-27 21:17:14

This a short snippet of code, with two calls to exit(3) in case of failure. Do these calls deallocate memory allocated by malloc? Google search once says it does, and even more times, it doesn't...

这是一段简短的代码片段,如果失败则会有两次exit(3)调用。这些调用是否释放malloc分配的内存?谷歌搜索曾经说过它,甚至更多次,它没有......

Should I add free()?

我应该添加free()吗?

Also: which is better if (!word)(it would also work for eg. word == 0 which is different from word == NULL, so I guess it is wrong) or if (word == NULL) ?

另外:哪个更好(!word)(它也可以用于例如.word == 0,这与word == NULL不同,所以我猜它是错的)或if(word == NULL)?

char *word = NULL, *temp = NULL;
    word = (char *)malloc(sizeof(char) * size);

    if (!word) {            /* or maybe rather it should be (word == NULL)  */
        perror("malloc fail");
        if (fclose(fp)) {
            perror("fclose fail");
            exit(3);                            /* exit without free ? */
        }
        exit(3);                                /* exit without free ? */
    }

Thanks in advance!

提前致谢!

4 个解决方案

#1


9  

Yes, all memory is returned. BTW, what would you want to do with leftover memory after the exit anyway?
Or are you worrying about a memory leak in exit()? If the memory weren't reclaimed, it would leak a bit more with each exiting process, which is something no credible OS could afford. So, except for a buggy OS, stop worrying about memory and use exit() wherever you need it.

是的,返回所有内存。顺便说一下,退出之后你还想用剩余的内存做什么呢?或者您是否担心exit()中的内存泄漏?如果内存没有被回收,那么每次退出过程都会泄漏更多内容,这是操作系统无法承受的。因此,除了有缺陷的操作系统外,不要再担心内存并在任何需要的地方使用exit()。

To answer the questions in the comments of your code, whether to free, I'd say it's proper software engineering to write a corresponding free with every malloc. If that appears hard, it is pointing to a structural problem in your code. The benefit of freeing all memory before exiting is that you can use powerful tools like valgrind to check for memory leaks in the rest of your code without false positives from the malloc you've shown us.

要回答代码注释中的问题,是否要释放,我会说适当的软件工程是为每个malloc写一个相应的免费软件。如果看起来很难,则表明代码中存在结构性问题。在退出之前释放所有内存的好处是,您可以使用像valgrind这样的强大工具来检查代码中其余代码的内存泄漏,而不会出现您向我们展示的malloc的误报。

Note that after a failed malloc there is no point in attempting to free the result--it's a null pointer anyway.

请注意,在malloc失败后,尝试释放结果没有意义 - 无论如何它都是空指针。

And third, I prefer if (pointer == NULL) over if (!pointer) but this is totally subjective and I can read and understand both :-)

第三,我更喜欢if(pointer == NULL)over if(!pointer)但这完全是主观的,我可以阅读并理解两者:-)

#2


7  

After calling exit you're beyond malloc and friends but the OS reclaims everything. Think of malloc as a convenient intermediary between the OS and your process.

在调用退出后,你超越了malloc和朋友,但操作系统收回了所有内容。将malloc视为操作系统和流程之间的便捷中介。

#3


2  

Just a note that at those 2 calls to exit you have - you have failed to allocate any memory, so freeing that pointer is going to be pretty pointless (and might crash, depending on how old your C runtime system is).

只需要注意,在那两个退出调用的情况下 - 你没有分配任何内存,因此释放该指针将毫无意义(并且可能会崩溃,具体取决于C运行时系统的年龄)。

So, no, you shouldn't free it, because it doesn't exist.

所以,不,你不应该释放它,因为它不存在。

I'd have said in the case of a fatal error like that, you probably don't want to bother freeing up memory.

我已经说过像这样的致命错误,你可能不想打扰释放内存。

However, if your program exits normally, yes, you should try and free up all the memory you have allocated. That can be quite tricky sometimes.

但是,如果您的程序正常退出,是的,您应该尝试释放已分配的所有内存。这有时候非常棘手。

#4


1  

When you exit the program, all allocated memory is reclaimed by the OS (both the stack and the heap). Your program doesn't leave any footprint in the RAM, unless you work outside the program's memory through buffer overflows and such.

退出程序时,操作系统(堆栈和堆)都会回收所有已分配的内存。您的程序不会在RAM中留下任何空白,除非您通过缓冲区溢出等工作在程序的内存之外。

#1


9  

Yes, all memory is returned. BTW, what would you want to do with leftover memory after the exit anyway?
Or are you worrying about a memory leak in exit()? If the memory weren't reclaimed, it would leak a bit more with each exiting process, which is something no credible OS could afford. So, except for a buggy OS, stop worrying about memory and use exit() wherever you need it.

是的,返回所有内存。顺便说一下,退出之后你还想用剩余的内存做什么呢?或者您是否担心exit()中的内存泄漏?如果内存没有被回收,那么每次退出过程都会泄漏更多内容,这是操作系统无法承受的。因此,除了有缺陷的操作系统外,不要再担心内存并在任何需要的地方使用exit()。

To answer the questions in the comments of your code, whether to free, I'd say it's proper software engineering to write a corresponding free with every malloc. If that appears hard, it is pointing to a structural problem in your code. The benefit of freeing all memory before exiting is that you can use powerful tools like valgrind to check for memory leaks in the rest of your code without false positives from the malloc you've shown us.

要回答代码注释中的问题,是否要释放,我会说适当的软件工程是为每个malloc写一个相应的免费软件。如果看起来很难,则表明代码中存在结构性问题。在退出之前释放所有内存的好处是,您可以使用像valgrind这样的强大工具来检查代码中其余代码的内存泄漏,而不会出现您向我们展示的malloc的误报。

Note that after a failed malloc there is no point in attempting to free the result--it's a null pointer anyway.

请注意,在malloc失败后,尝试释放结果没有意义 - 无论如何它都是空指针。

And third, I prefer if (pointer == NULL) over if (!pointer) but this is totally subjective and I can read and understand both :-)

第三,我更喜欢if(pointer == NULL)over if(!pointer)但这完全是主观的,我可以阅读并理解两者:-)

#2


7  

After calling exit you're beyond malloc and friends but the OS reclaims everything. Think of malloc as a convenient intermediary between the OS and your process.

在调用退出后,你超越了malloc和朋友,但操作系统收回了所有内容。将malloc视为操作系统和流程之间的便捷中介。

#3


2  

Just a note that at those 2 calls to exit you have - you have failed to allocate any memory, so freeing that pointer is going to be pretty pointless (and might crash, depending on how old your C runtime system is).

只需要注意,在那两个退出调用的情况下 - 你没有分配任何内存,因此释放该指针将毫无意义(并且可能会崩溃,具体取决于C运行时系统的年龄)。

So, no, you shouldn't free it, because it doesn't exist.

所以,不,你不应该释放它,因为它不存在。

I'd have said in the case of a fatal error like that, you probably don't want to bother freeing up memory.

我已经说过像这样的致命错误,你可能不想打扰释放内存。

However, if your program exits normally, yes, you should try and free up all the memory you have allocated. That can be quite tricky sometimes.

但是,如果您的程序正常退出,是的,您应该尝试释放已分配的所有内存。这有时候非常棘手。

#4


1  

When you exit the program, all allocated memory is reclaimed by the OS (both the stack and the heap). Your program doesn't leave any footprint in the RAM, unless you work outside the program's memory through buffer overflows and such.

退出程序时,操作系统(堆栈和堆)都会回收所有已分配的内存。您的程序不会在RAM中留下任何空白,除非您通过缓冲区溢出等工作在程序的内存之外。