Reputation: 235
I realise this question has come up a few times, but I'm trying to get a definitive answer for the above question, but I keep coming across conflicting info. What I need to know is if a basic class object is destructed when I use exit(). I'm aware of dynamic memory needing to be deleted, but I am meaning something more like:
#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;
}
Now if I remove exit(1) from the main, the "Object destroyed" and "Hello" are printed as expected. Leaving it in there though, neither are printed. The reason is obvious for "Hello", but I was under the impression that emp1 would still be destructed, but the destruct message isn't shown...
I was looking at this link and it says about static objects being destroyed. Is the above object not considered static?
If not, is there a way to have a program terminate without it screwing with memory? My project revolves around user input and I was trying to give the option to exit if the user inputs the word 'exit'.
if(input_var == "exit")
{
cout << "You have chosen to exit the program." << endl;
exit(1);
}
Is a rough example of what my intent was.
Upvotes: 3
Views: 3358
Reputation: 60056
You can throw. An exception will clean up the unwound scope.
//...
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;`
}
}
Outputs:
Object destroyed
Upvotes: 1
Reputation: 5566
What I need to know is if a basic class object is destructed when I use exit().
You've demonstrated that it does not. The os service labeled 'exit' does so without concern about code issues. Note that exit existed before C++.
is there a way to have a program terminate without it screwing with memory?
You have demonstrated that exit is at least one way to terminate the program without calling a C++destructor. exit is language agnositic
a) This means the memory will not be modified by the destructor.
So is the memory screwed with?
b) Part of exit (and main return) processing is that all memory resources of that process will be reclaimed by the OS. The memory will not be 'screwed' with, (no destructor called, no 'wipe' or 'erase').
c) Part of exit is to close any streams open by that process. (Files, devices, terminals, etc.)
If b) or c) modify memory, you can not tell, because the modifications have no more association with the closed process.
Upvotes: 1
Reputation: 2211
According to this link, it does not clean up the object. Note that if you use non-stack memory, it will call the destructor:
static employee emp1;
Second note. Any time you are using cout
for debugging edge cases, timing critical debugging, etc., you should add a cout.flush()
right after the cout
to ensure your output is printed before it moves on. I have seen many people use cout
for debugging crashes and the output never prints because the program terminates before the OS had a chance to print the output.
Upvotes: 2
Reputation: 32732
Your emp1
variable is allocated on the stack. exit
does not destroy local stack based variables.
Upvotes: 1