在C,是否需要自由退出的指针? [英] In C, is it necessary to free a pointer at exit?

查看:114
本文介绍了在C,是否需要自由退出的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2213627/when-you-exit-a-c-application-is-the-malloc-ed-memory-automatically-freed\">When您退出一个C程序,是的malloc-ED自动记忆中解脱出来?

在C,是否需要自由退出的指针?
当程序存在,它与指针仍然指向分配块?空闲内存

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?

它是依赖于操作系统?

推荐答案

据我所知,在大多数情况下,操作系统将释放任何进程的内存进程终止时,至少在最常见的用户操作系统(Windows和Linux等)。如果进程崩溃或者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.

某些视频驱动程序不会免费VRAM立即对一些老卡,运行泄露VRAM反复最终会造成系统崩溃的过程。

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.

这篇关于在C,是否需要自由退出的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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