• Advertisement
Sign in to follow this  

Stopping execution in C++

This topic is 4432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement