是否在 Exit() 时调用基本对象析构函数?
Are Basic Object Destructors Called On Exit()?
我知道这个问题已经出现过几次,但我正试图为上述问题找到明确的答案,但我不断遇到相互矛盾的信息。我需要知道的是,当我使用 exit() 时,基本 class 对象是否被破坏。我知道需要删除动态内存,但我的意思更像是:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class employee
{
public:
employee ();
string name;
~employee();
};
employee::employee ()
{
name = "bob";
}
employee::~employee()
{
cout << "Object destroyed" << endl;
}
int main()
{
employee emp1;
exit(1);
cout << "Hello" << endl;
}
现在,如果我从 main 中删除 exit(1),"Object destroyed" 和 "Hello" 将按预期打印。把它留在那儿,但都没有打印出来。 "Hello" 的原因很明显,但我的印象是 emp1 仍会被破坏,但没有显示破坏消息...
我在看 this link,它说静态对象被销毁了。上面的对象不是静态的吗?
如果不是,有没有办法让程序在不破坏内存的情况下终止?我的项目围绕用户输入展开,我试图在用户输入 'exit'.
时提供退出选项
if(input_var == "exit")
{
cout << "You have chosen to exit the program." << endl;
exit(1);
}
是我意图的粗略示例。
您的 emp1
变量分配在堆栈上。 exit
不会破坏基于局部堆栈的变量。
根据this link,它不会清理对象。
注意如果使用非栈内存,会调用析构函数:
static employee emp1;
第二个音符。任何时候你使用 cout
来调试边缘情况、时间关键调试等,你应该在 cout
之后添加一个 cout.flush()
以确保你的输出在继续之前被打印出来。我看到很多人使用 cout
来调试崩溃并且输出永远不会打印,因为程序在 OS 有机会打印输出之前终止。
你可以扔。异常将清理展开的范围。
//...
int main()
{
try{
employee emp1;
throw 1; //fake; throwing an object is more advisable in real situations
cout << "Hello" << endl;
}catch(int){
exit(1); //or better simply `return 1;`
}
}
输出:
Object destroyed
What I need to know is if a basic class object is destructed when I
use exit().
您已经证明事实并非如此。标记为 'exit' 的 os 服务这样做时无需担心代码问题。请注意,exit 在 C++ 之前就已存在。
is there a way to have a program terminate without it screwing with
memory?
您已经证明 exit 至少是一种无需调用 C++ 析构函数即可终止程序的方法。出口是语言 agnositic
a) 这意味着内存不会被析构函数修改。
内存是不是坏了?
b) 退出(和主要 return)处理的一部分是该进程的所有内存资源将由 OS 回收。内存不会 'screwed' with,(没有调用析构函数,没有 'wipe' 或 'erase')。
c) exit 的一部分是关闭ose 该进程打开的任何流。 (文件、设备、终端等)
如果 b) 或 c) 修改内存,您无法分辨,因为修改与 closed 进程没有更多关联。
我知道这个问题已经出现过几次,但我正试图为上述问题找到明确的答案,但我不断遇到相互矛盾的信息。我需要知道的是,当我使用 exit() 时,基本 class 对象是否被破坏。我知道需要删除动态内存,但我的意思更像是:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class employee
{
public:
employee ();
string name;
~employee();
};
employee::employee ()
{
name = "bob";
}
employee::~employee()
{
cout << "Object destroyed" << endl;
}
int main()
{
employee emp1;
exit(1);
cout << "Hello" << endl;
}
现在,如果我从 main 中删除 exit(1),"Object destroyed" 和 "Hello" 将按预期打印。把它留在那儿,但都没有打印出来。 "Hello" 的原因很明显,但我的印象是 emp1 仍会被破坏,但没有显示破坏消息...
我在看 this link,它说静态对象被销毁了。上面的对象不是静态的吗?
如果不是,有没有办法让程序在不破坏内存的情况下终止?我的项目围绕用户输入展开,我试图在用户输入 'exit'.
时提供退出选项if(input_var == "exit")
{
cout << "You have chosen to exit the program." << endl;
exit(1);
}
是我意图的粗略示例。
您的 emp1
变量分配在堆栈上。 exit
不会破坏基于局部堆栈的变量。
根据this link,它不会清理对象。 注意如果使用非栈内存,会调用析构函数:
static employee emp1;
第二个音符。任何时候你使用 cout
来调试边缘情况、时间关键调试等,你应该在 cout
之后添加一个 cout.flush()
以确保你的输出在继续之前被打印出来。我看到很多人使用 cout
来调试崩溃并且输出永远不会打印,因为程序在 OS 有机会打印输出之前终止。
你可以扔。异常将清理展开的范围。
//...
int main()
{
try{
employee emp1;
throw 1; //fake; throwing an object is more advisable in real situations
cout << "Hello" << endl;
}catch(int){
exit(1); //or better simply `return 1;`
}
}
输出:
Object destroyed
What I need to know is if a basic class object is destructed when I use exit().
您已经证明事实并非如此。标记为 'exit' 的 os 服务这样做时无需担心代码问题。请注意,exit 在 C++ 之前就已存在。
is there a way to have a program terminate without it screwing with memory?
您已经证明 exit 至少是一种无需调用 C++ 析构函数即可终止程序的方法。出口是语言 agnositic
a) 这意味着内存不会被析构函数修改。
内存是不是坏了?
b) 退出(和主要 return)处理的一部分是该进程的所有内存资源将由 OS 回收。内存不会 'screwed' with,(没有调用析构函数,没有 'wipe' 或 'erase')。
c) exit 的一部分是关闭ose 该进程打开的任何流。 (文件、设备、终端等)
如果 b) 或 c) 修改内存,您无法分辨,因为修改与 closed 进程没有更多关联。