c ++程序崩溃时会自动释放内存吗? [英] Does a c++ program automatically free memory when it crashes?

查看:897
本文介绍了c ++程序崩溃时会自动释放内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了Google的c ++编码标准,Google不使用例外。如果没有使用异常,当程序中出现错误时,如何释放内存?

I read in Google c++ coding standards that Google does not use exception. If exception is not used, how do you free memory when errors occur in your program?

例如,f()调用g(),如果g()中有错误,我应该释放所有在g()中分配的内存,然后调用f()的异常。一旦f()捕获到异常,f()将释放f()中分配的所有内存,并退出程序。

For example, f() calls g(), and if there is error in g(), I should free all memory allocated in g(), and then call an exception to f(). Once f() catches the exception, f() will free all memory allocated in f(), and exits the program.

如果不使用异常,是g()中的一个错误,我可以强制退出 exit(0),并且c ++程序会聪明到释放所有分配的内存吗?我的猜想是,由于c ++维护一个堆栈和堆,一旦程序退出,c ++会自动释放堆栈和堆?

If exception is not used, and if there is an error in g(), can I force exit exit(0), and will the c++ program be smart enough to free all memory that is allocated? My guess is, since c++ maintain a stack and heap, and once the program exits, c++ will automatically free both stack and heap?

推荐答案

p>当进程由于某种原因终止时,操作系统会清除所有使用的内存和文件句柄。

The operating system cleans up all used memory and file handles when a process is terminated for whatever reason.

这篇关于c ++程序崩溃时会自动释放内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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