system("pause") do you hate it?

Cogman

Lifer
Sep 19, 2000
10,284
138
106
So on another board in another time and place I am in an argument with a guy about system("pause"); Essentially his argument goes something like this.

"System pause is expensive and very wasteful, therefore it shouldn't be used"

My argument goes something like this.

The command's entire purpose is to stop the program. Why on earth should a user care if the program stops in 1 cycle or 8 cycles? The point is to stop the program. And ultimately it isn't THAT expensive anyways (100k of memory, *gasp*).

Ok, the biggest con I can see is that it is non-portable. But other then that, it is meant to stop a program, why is it such a bad thing if it takes more time to stop a program? Did I just miss programmers monthly here? Was there some memo that I should have known about the efficiency of your stopping commands? Is it really THAT bad of a habit?
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Originally posted by: tfinch2
It's like opening a can of soup with a jackhammer.

But what does it matter? the goal is to stop the program, so why is it so bad to slow to a stop verses having an abrupt stop? both are stopping at exactly the same point, just one reaches stopped first.

Its not like its adding huge overhead either. a program compiled without anything in it (just a main) using g++ comes to a whopping 3.0kb. one that is compiled with cstdlib and system("pause"), A whole 3.3kb. So the program file size increased by .3 kb, Not such a big deal.

a program constantly looping with a system("PAUSE") would jump from 40kb, to 90kb. Somehow, I think the extra 50kb is available for each time you want to pause the system.

I guess, you could register more enters if you used your own looping method rather then a system("pause"), however, why would you even have a pause if you just wanted someone to hold down the enter key/push it as many times as possible?

Compare that to a program that adds a cin statement, and all the sudden you get a 100kb jump in size for a program just to pause it. If anything cin wastes more storage.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
while I don't use system("pause") myself (cin.get() is easy enough), I don't buy his argument. it's not like you call the thing 100 times a second. there's no point in fussing over optimization of non-critical code. CPU cycles are cheaper than developing time.

 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I would conclude that system("pause") is not, in fact, the devil. However, it is closely related to the devil -- for it is sloth, and as others have pointed out, it is not very portable. Moreover, it makes my developer-sense tingle.

Googling around, I found the following objections to system("pause"); -- I make no claims about author quality here, these are merely hits near the top.

http://www.gidnetwork.com/b-61.html: Too much overhead. I tend to agree with Cogman's point on the overhead issue.

http://cpp.codenewbie.com/arti...ystemPAUSE-Page_1.html: Use of system("pause") relies on capabilities of the shell, the availability of the pause command, and that PATH contains pause. This is a bigger point... but probably still OK if you are in control of the environment in which your code executes.

http://www.daniweb.com/forums/thread21020.html: system("pause") used in a book. And Surprise! It breaks on a newbie's Mac, yielding lots of confusion. Also here again: http://lists.apple.com/archive...2004/Dec/msg00013.html

http://www.unix.com/high-level...tem-pause-problem.html: Here's a little gem. Since system() is fork()ing and exec()ing and such, there is no relationship between the parent process's I/O and the child process's I/O. In order to guarantee, say, that couts are visible to the user before the pause, you must explicitly flush cout. This problem gets worse for more exotic parent/child relationships. Another example here: http://www.techimo.com/forum/w...-working-corectly.html
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Originally posted by: xtknight
Bad practice. Use cin.get() instead that does the same thing.

Why is it bad practice though? in all my programming years the only time I have every used the system function was to call a pause. And the only time I did that was to stop the program at the end of an execution.

Now if you where using system to write your entire program I could see it being a problem, but the fact remains that most people don't use it for anything other then a quick way to pause.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: Cogman
Originally posted by: xtknight
Bad practice. Use cin.get() instead that does the same thing.

Why is it bad practice though? in all my programming years the only time I have every used the system function was to call a pause. And the only time I did that was to stop the program at the end of an execution.

Now if you where using system to write your entire program I could see it being a problem, but the fact remains that most people don't use it for anything other then a quick way to pause.

I think the previous post already explained why. It doesn't port properly and it goes outside of the process itself to execute something. We're not lambasting you saying it's a terrible solution, it's the difference between 1% and 2% but if you can have the better way why not use the better way...

It's like picking up a Coke for $1.99 or for $2.09 next door (10 feet away). Why would you go next door to pick up the more expensive Coke? cin.get() is less to type and it works better.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: xtknight
It's like picking up a Coke for $1.99 or for $2.09 next door (10 feet away). Why would you go next door to pick up the more expensive Coke? cin.get() is less to type and it works better.

Actually to really do it properly, you'll have to print out a message, and do a cin.ignore() after the cin.get() to not mess up the stream buffer for later calls (or a getline(), but that requires a variable). Not much more work, but certainly not less.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Why is it bad practice though?

Using the system() function at all is bad practice for all of the reasons that are listed in degibsons's codenewbie.com's link. If the host doesn't have a working shell it'll fail, if pause isn't in the PATH it'll fail, if pause is superseded by another command, batchfile, etc anything could happen.
 

deveraux

Senior member
Mar 21, 2004
284
0
71
Personally, I think it really depends on the context of its usage. If you are using it while development of some software or as a placeholder or for debugging purposes (I used to just to provide momentary pauses because I did not have access to a proper debugger at that point), then I think it does not really matter.

But in finished/polished code, you should not really require this but the way you describe it, a one-off use at the end of the program, I really do not believe it matters, there isn't any "efficiency" or "optimisation" issues since the program has run its course anyway. Correct me if I am wrong in how you use it.

Others have brought up the issue of portability and I think that the programmer should know better what his/her code will be used for so it is a moot point. If portability is important, ditch it, but not all code is made to be portable nor does it need to be unless specified.

deveraux