system(“pause”); - Why is it wrong?

前端 未结 13 2065
轻奢々
轻奢々 2020-11-21 07:39

Here\'s a question that I don\'t quite understand:

The command, system(\"pause\"); is taught to new programmers as a way to pause a program and wait for

相关标签:
13条回答
  • 2020-11-21 07:51

    Because it is not portable.

    pause
    

    is a windows / dos only program, so this your code won't run on linux. Moreover, system is not generally regarded as a very good way to call another program - it is usually better to use CreateProcess or fork or something similar.

    0 讨论(0)
  • 2020-11-21 07:53

    As listed on the other answers, there are many reasons you can find to avoid this. It all boils down to one reason that makes the rest moot. The System() function is inherently insecure/untrusted, and should not be introduced into a program unless necessary.

    For a student assignment, this condition was never met, and for this reason I would fail an assignment without even running the program if a call to this method was present. (This was made clear from the start.)

    0 讨论(0)
  • 2020-11-21 07:54

    It's slow. It's platform dependent. It's insecure.

    First: What it does. Calling "system" is literally like typing a command into the windows command prompt. There is a ton of setup and teardown for your application to make such a call - and the overhead is simply ridiculous.

    What if a program called "pause" was placed into the user's PATH? Just calling system("pause") only guarantees that a program called "pause" is executed (hope that you don't have your executable named "pause"!)

    Simply write your own "Pause()" function that uses _getch. OK, sure, _getch is platform dependent as well (note: it's defined in "conio.h") - but it's much nicer than system() if you are developing on Windows and it has the same effect (though it is your responsibility to provide the text with cout or so).

    Basically: why introduce so many potential problems when you can simply add two lines of code and one include and get a much more flexible mechanism?

    0 讨论(0)
  • 2020-11-21 07:56
    • slow: it has to jump through lots of unnecessary Windows code and a separate program for a simple operation
    • not portable: dependent on the pause program
    • not good style: making a System call should only be done when really necessary
    • more typing: System("pause") is longer than getchar()

    a simple getchar() should do just fine.

    0 讨论(0)
  • 2020-11-21 07:57

    For me it doesn't make sense in general to wait before exiting without reason. A program that has done its work should just end and hand over its resources back to its creator.

    One also doesn't silently wait in a dark corner after a work day, waiting for someone tipping ones shoulder.

    0 讨论(0)
  • 2020-11-21 08:02

    Here's one reason you shouldn't use it: it's going to piss off most anti-virus programs running on Windows if you're passing the program over to another machine because it's a security threat. Even if your program only consists of a simple cout << "hello world\n"; system("pause"); It's resource heavy and the program gets access to the cmd command, which anti viruses see as a threat.

    0 讨论(0)
提交回复
热议问题