-2

I'm working on a C++ project. I gave the user the option to exit from the program by entering 'q' when asked to enter a command in the console window. I've looked around StackOverflow and came across exit(). Apparently using exit is actually a quite bad way of ending the program. I'm not sure if it's because it abruptly aborts without time for the program to round up things. I'd rather not take the easy way and actually learn something here so which is the best way to exit a program in c++. Here is a snippet of the code.

else if (operation == "q"){

    }

Operation variable is the user's input. Thanks in advance

Tazza
  • 35
  • 1
  • 2
  • 9
  • If it's sufficiently small program, what's wrong, with simply `return`ing from `main`? – Algirdas Preidžius Jul 21 '18 at 15:26
  • 1
    Whoever told you that "using exit is actually a quite bad way of ending the program" was wrong. – Sam Varshavchik Jul 21 '18 at 15:30
  • Just call `exit(0)`, or `std::terminate()`, both are safe ways to exit a program. – MivVG Jul 21 '18 at 15:36
  • 1
    If you're worried about memory leaks, everything owned by the process should be freed up when the program ends. However, keep in mind that destructors aren't called when using `exit()`. – 3Dave Jul 21 '18 at 15:43
  • 1
    Calling `exit()` is certainly an effective way of ending a program. Whether it is a good or bad idea depends on the program. For example, calling `exit()` does NOT call/invoke destructors of objects created with automatic storage duration. If your program relies on those destructors being called, using `exit()` is not a good idea. `exit()` does destroy objects with static storage duration, but that is problematic if the program has multiple threads running. – Peter Jul 21 '18 at 15:44
  • Possible duplicate of [How to end C++ code](https://stackoverflow.com/questions/30250934/how-to-end-c-code) – dandan78 Jul 21 '18 at 16:36
  • It would seem opinion is divided on this. Personally I think it's bad because it won't clean up properly before ending the program. Maybe you can get away with that in some trivial programs but I would never recommend it. – Galik Jul 21 '18 at 17:17
  • No duplication. Just clarification. Read what i wrote – Tazza Jul 22 '18 at 11:07

1 Answers1

-2

Calling exit(0) is definitely fine and a safe way to stop execution of your program. Another call you can use to stop execution is std::terminate(), which is defined in the header <exception and does about the same as exit(0).

MivVG
  • 679
  • 4
  • 16
  • Thanks for that. The reason i was worried was because of this stackoverflow question's answers if you're interested. https://stackoverflow.com/questions/4038302/how-do-i-make-a-c-console-program-exit – Tazza Jul 21 '18 at 15:48
  • 3
    In your case, you should not be worried about the downsides of `exit()`. When making more complex programs, the best way is to throw and exception, catch it in `main()` and return from there. – MivVG Jul 21 '18 at 15:52
  • Ok. I guess it's something i'll have to learn as i become more advanced – Tazza Jul 21 '18 at 15:55