如果我不在 C 程序中调用 fclose() 会发生什么? [英] What happens if I don't call fclose() in a C program?

查看:28
本文介绍了如果我不在 C 程序中调用 fclose() 会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道用 fopen() 打开文件而不关闭它是非常不负责任的,而且形式很糟糕.这只是纯粹的好奇心,所以请逗我 :)

Firstly, I'm aware that opening a file with fopen() and not closing it is horribly irresponsible, and bad form. This is just sheer curiosity, so please humour me :)

我知道如果一个 C 程序打开一堆文件并且从不关闭它们中的任何一个,最终 fopen() 将开始失败.是否有任何其他副作用可能导致代码本身之外的问题?例如,如果我有一个程序打开一个文件,然后退出而不关闭它,这会不会对运行该程序的人造成问题?这样的程序会泄漏任何东西(内存、文件句柄)吗?一旦程序完成,再次访问该文件是否会出现问题?如果程序连续运行多次会发生什么?

I know that if a C program opens a bunch of files and never closes any of them, eventually fopen() will start failing. Are there any other side effects that could cause problems outside the code itself? For instance, if I have a program that opens one file, and then exits without closing it, could that cause a problem for the person running the program? Would such a program leak anything (memory, file handles)? Could there be problems accessing that file again once the program had finished? What would happen if the program was run many times in succession?

推荐答案

只要你的程序在运行,如果你一直打开文件而不关闭它们,最有可能的结果是你将耗尽可用的文件描述符/句柄对于您的进程,尝试打开更多文件最终将失败.在 Windows 上,这还可以防止其他进程打开或删除您打开的文件,因为默认情况下,文件以独占共享模式打开,以防止其他进程打开它们.

As long as your program is running, if you keep opening files without closing them, the most likely result is that you will run out of file descriptors/handles available for your process, and attempting to open more files will fail eventually. On Windows, this can also prevent other processes from opening or deleting the files you have open, since by default, files are opened in an exclusive sharing mode that prevents other processes from opening them.

一旦您的程序退出,操作系统将在您之后进行清理.当它终止您的进程时,它将关闭您保持打开的所有文件,并执行任何其他必要的清理(例如,如果文件被标记为关闭时删除,那么它将删除该文件;请注意,这类事情是平台- 具体).

Once your program exits, the operating system will clean up after you. It will close any files you left open when it terminates your process, and perform any other cleanup that is necessary (e.g. if a file was marked delete-on-close, it will delete the file then; note that that sort of thing is platform-specific).

然而,另一个需要注意的问题是缓冲数据.大多数文件流在将数据写入磁盘之前在内存中缓冲数据.如果您使用的是 stdio 库中的 FILE* 流,则有两种可能性:

However, another issue to be careful of is buffered data. Most file streams buffer data in memory before writing it out to disk. If you're using FILE* streams from the stdio library, then there are two possibilities:

  1. 您的程序通过调用exit(3) 函数正常退出,或者从 main(隐式调用 exit(3))返回.
  2. 您的程序异常退出;这可以通过调用 abort(3)_Exit(3),死于信号/异常等

如果您的程序正常退出,C 运行时将负责刷新所有打开的缓冲流.因此,如果您将缓冲数据写入未刷新的 FILE*,它将在正常退出时刷新.

If your program exited normally, the C runtime will take care of flushing any buffered streams that were open. So, if you had buffered data written to a FILE* that wasn't flushed, it will be flushed on normal exit.

相反,如果您的程序异常退出,任何缓冲的数据将不会被刷新.当进程终止时,操作系统只是说哦,亲爱的,你打开了一个文件描述符,我最好为你关闭它";它不知道内存中某处有一些随机数据,程序打算写入磁盘但没有.所以要小心.

Conversely, if your program exited abnormally, any buffered data will not be flushed. The OS just says "oh dear me, you left a file descriptor open, I better close that for you" when the process terminates; it has no idea there's some random data lying somewhere in memory that the program intended to write to disk but did not. So be careful about that.

这篇关于如果我不在 C 程序中调用 fclose() 会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆