0

I'm reading through Programming Principles and Practice 2ed. and in the Hello World program, they use a line, keep_window_open().

Before finding this book, I learned the same Hello World program, but instead using system("PAUSE").

My questions are as follows:

  1. Are keep_window_open() and system("PAUSE") effectively the same thing, or are they different?
  2. If they are basically the same in function, then is there a reason to use one over the other? If so, when and why?
  3. If they are different, then is keep_window_open() deprecated in favor of system("PAUSE"), or vice versa? (Simpler terms: which would be preferred for regular use?)
  4. If keep_window_open() is still in use, is it a part of the std namespace, to where it would need to be written as std::keep_window_open() in order to be used without using namespace std;?
NJJ_002
  • 53
  • 7
  • 2
    See [this answer](https://stackoverflow.com/a/31910145/65863) for how `keep_window_open()` is implemented. `system("PAUSE")` and `keep_window_open()` are equivalent, but they accomplish the same thing (wait for user input) in different ways. – Remy Lebeau Jan 20 '21 at 19:28
  • @RemyLebeau Thank you; while probably helpful for something in the future, I'm not seeing how this answers either of the posed questions. Do you have a different way of explaining what you meant by your referral to that answer? – NJJ_002 Jan 20 '21 at 19:32
  • 1
    `keep_window_open()` is not a part of the standard C++. It's something Stroustrup came up with for his book, and you're only able to use it because you include `std_lib_facilities.h` which is distributed with the book. – HolyBlackCat Jan 20 '21 at 19:32
  • 2
    @RemyLebeau No, they are not equivalent. Both approaches aim to achieve the same thing, but `system("PAUSE")` is platform-dependent, `keep_window_open()` is not (and uses only standard-dependend means). – Jodocus Jan 20 '21 at 19:32
  • @RemyLebeau So that is to say that `system("PAUSE")` would work (or not work) differently on a Mac or Linux system, then, right? Meaning that `keep_window_open()` is system-agnostic? – NJJ_002 Jan 20 '21 at 19:35
  • @HolyBlackCat How would I figure out whether or not I have `std_lib_facilities.h`? Is it something that I could search for in File Explorer? If for some reason I don't have it, is there a way around using that header file? – NJJ_002 Jan 20 '21 at 19:36
  • 3
    FWIW, if all your trying to do is stop the command line window from closing immediately when the program is done, you can place a breakpoint on the last line of code in main to have it stop there. That way you don't need to edit the code to remove the pause once your done. – NathanOliver Jan 20 '21 at 19:37
  • 1
    Some IDEs will also run the program in a console that doesn't close when the program is done. Newer versions of Visual Studio do that now, too. – chris Jan 20 '21 at 19:38
  • @NathanOliver This is probably something that I'll need to focus on at some point in the future and that might not be the most helpful thing now, but I do appreciate your response. I just want to clarify: when you say "breakpoint", is that basically just saying `break;`? And would you call `break;` before or after the `return` statement? – NJJ_002 Jan 20 '21 at 19:39
  • 1
    @NicholasJarecki As a general rule, `std::system` has a lot of problems. Specifically, `system("pause");` is not portable, it is a security vulnerability and it is unnecessarily heavy for blocking execution. It starts a new application which waits for user input, and waits for that application to close. As for `keep_window_open()` there is no such standard function. It must have been defined previously by your book. You should go look at how it is implemented. – François Andrieux Jan 20 '21 at 19:42
  • 3
    @NicholasJarecki No, a breakpoint is a IDE/Debugger tool that let you mark a line of code as a point for it to stop executing once it reaches it. It's used **a lot** for debugging to get the code to stop at certain points so you can see what the value of things are in the code. – NathanOliver Jan 20 '21 at 19:42
  • @FrançoisAndrieux Could you please provide a more in-depth explanation for why `system("PAUSE")` causes all of those problematic things? – NJJ_002 Jan 20 '21 at 19:47
  • An IDE that closes the console window as soon as the program exits is broken. The proper course of action is to use a non-broken IDE. AFAICT modern mainstream IDEs are generally non-broken, and this wart is not needed with them. – n. m. could be an AI Jan 20 '21 at 19:49
  • 1
    @NicholasJarecki See [Why should the system() function be avoided in C and C++?](https://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and-c). As for the security vulnerability, anyone can put a binary with the name you are invoking somewhere it will be found before the one you intended to call to hijack your process and execute with your credentials. – François Andrieux Jan 20 '21 at 19:50

1 Answers1

4

Are keep_window_open() and system("PAUSE") effectively the same thing, or are they different?

They have an equivalent use-case (block the calling process until user input is entered), but they approach it in different ways.

system("PAUSE") spawns a separate process to run the OS's command-line processor and have it execute a PAUSE command, which outputs a message to the console and then blocks the calling process until user input is entered.

keep_window_open() does not use a separate process, it simply uses std::cout and std::cin to display a console message and then wait for user input, all within in the calling process (this answer shows the actual implementation).

If they are basically the same in function, then is there a reason to use one over the other? If so, when and why?

system("PAUSE") is platform-dependent as it relies on an OS processor being present, and that processor implementing a PAUSE command.

keep_window_open() uses standard C++ features.

If they are different, then is keep_window_open() deprecated in favor of system("PAUSE"), or vice versa?

IMHO, you should use keep_window_open(), since it relies on standard behavior independent of the platform your code is run on. But there is not really anything wrong with using system("PAUSE") instead, if your code runs on a platform that supports it.

If keep_window_open() is still in use, is it a part of the std namespace

keep_window_open() is not part of C++, it is just a plain user-defined function introduced in the book. So no, it is not part of the std namespace.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770