开发者

In C, is it necessary to free a pointer at exit? [duplicate]

This question already has answers here: Closed 12 years ago. 开发者_Python百科

Possible Duplicate:

When you exit a C application, is the malloc-ed memory automatically freed?

In C, is it necessary to free a pointer at exit? When the program exists, does it free memory from pointers still pointing to an allocated block?

Is it dependent on the OS?


From what I know, for the most part, the OS will free any process memory when the process terminates, at least under most common user OSes (Windows, Linux, etc). The OS will also perform cleanup if the process crashes or such.

HOWEVER, relying on the OS to perform cleanup is not proper coding procedure and you can't always guarantee it'll do what you want. You should always perform your own garbage collection if you want it done right and at the right time (I've had programs crash during exit because the system cleaned up memory in an odd order and created some invalid pointers, that it then tried to free).

Process memory cleanup may only apply to memory allocated by your original process or thread. If you spawn new processes, these could keep executing. If you use an already-running service and call some method that allocates memory then gives you control, that may not get cleanup up.

Some video drivers won't free VRAM immediately and on some older cards, running a process that leaked VRAM repeatedly would eventually crash your system.

You should always free any memory you allocate, especially if your process may restart or keep executing.


The OS will free anything allocated by your program when the program exits. However, it is good practice to always free what you've allocated.


It's not strictly necessary, in fact it can sometimes be convenient to "leak" these pointers to avoid worrying about ordering of destructors.

No modern OS is going to leak this memory, all the memory used by the process will be reclaimed.


This is OS-dependent, but really any modern operating system should take back all of your resources when you terminate.

That said, it's really a good idea to free any memory you acquire if you can -- and in all but few cases you can. Also, remember we're talking about memory here, not resources in general. There are other things you may wish to do in a proper cleanup; things the OS can't guess and can't do for you. For example, disconnect from some external service, or delete some temporary file.


It depends on the OS. Although in most cases, forgetting to free the pointer won't do too much harm, doing so is not a good idea either. This can lead into memory leak. If happening long enough, you can experience a strange shortage of memory that you can't access, causing other programs to not having enough memory to operate.

If you're talking about explicitly freeing a dynamically allocated array that a pointer is pointing to, in short, yes, you do have to go through that process. When the program exits, if your program did not free that memory block, the pointer is still pointing to the allocated block, but no longer allows you to have access to it since the program is out of scope upon exit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜