Stopping execution in C++

Started by
3 comments, last by Zahlman 18 years, 3 months ago
How do i stop/shut down the program in C++? I thought there was a way...i can't recall.I tried using system("PAUSE"), but that didn't work. -thanks
Advertisement
The usual way to shut down the program is to return from main. ;-)
You can also call exit, but if I recall correctly, it's not guaranteed to call destructors and probably isn't a good idea.
exit() will call destructors of global objects, but not necessarily local objects currently on the stack. It's a perfectly fine way to exit a process.

abort() won't call any destructors, but also will likely generate a crash dump or user message of some sort.

The easiest way is to wrap a big try/catch around your main, and throw something like a quit_exception from within your code; have it caught in the main function and return your status code to the OS.
enum Bool { True, False, FileNotFound };
There was a discussion on this a while ago...

In Win32 the entry code looks something like this:

int ret = main(argc, argv); //same for WinMain(...);exit(ret);


So there is nothing to worry about except calling destructors.
Quote:Original post by Beer Hunter
The usual way to shut down the program is to return from main. ;-)


QFE. Why do you think this won't work in your situation?

This topic is closed to new replies.

Advertisement