시스템 ( "일시 정지"); 왜 틀렸어?
내가 이해하지 못하는 질문이 있습니다.
이 명령 system("pause");
은 프로그램을 일시 중지하고 키보드 입력이 계속 될 때까지 기다리는 방법으로 새로운 프로그래머에게 배웁니다. 그러나 그것은 많은 베테랑 프로그래머들에 의해 다양한 각도에서 수행되어서는 안되는 것으로 찌푸린 것처럼 보입니다.
어떤 사람들은 사용하는 것이 좋다고 말합니다. 어떤 사람들은 방에 갇혀 있고 아무도보고 있지 않을 때만 사용해야한다고 말합니다. 어떤 사람들은 개인적으로 집에 와서 집을 쓰면 죽일 것이라고 말합니다.
저는 공식적인 프로그래밍 교육을받지 않은 새로운 프로그래머입니다. 나는 그것을 사용하도록 배웠기 때문에 그것을 사용합니다. 내가 이해하지 못하는 것은 그것이 사용해야 할 것이 아니라면 왜 그것을 사용하도록 가르쳐 졌는가? 아니면 반대로, 결국 그렇게 나쁘지 않습니까?
이 주제에 대해 어떻게 생각하십니까?
실제로 프로그래밍 학습과는 관련이 없지만 IDE / OS의 기능을 둘러싼 플랫폼 별 해킹이기 때문에 눈살을 찌푸 렸습니다 .Visual Studio에서 시작된 콘솔 창은 프로그램 실행이 완료되면 닫힙니다. 새로운 사용자는 그의 새로운 프로그램의 결과를 볼 수 없습니다.
System ( "pause")에서 Bodging은 Windows 명령 줄 "pause"프로그램을 실행하고 프로그램 실행을 계속하기 전에 프로그램이 종료 될 때까지 기다립니다. 콘솔 창은 열려있어 출력을 읽을 수 있습니다.
더 나은 아이디어는 끝에 중단 점을두고 디버그하는 것이지만 다시 문제가 있습니다.
느리다. 플랫폼에 따라 다릅니다. 안전하지 않습니다.
첫째 : 무엇을 하는가. "시스템"을 호출하는 것은 문자 그대로 Windows 명령 프롬프트에 명령을 입력하는 것과 같습니다. 응용 프로그램에서 이러한 호출을 수행하는 데는 많은 설정과 해체가 있으며 오버 헤드는 어리 석습니다.
"일시 정지"라는 프로그램이 사용자의 PATH에 배치되면 어떻게됩니까? system ( "pause")를 호출하면 "pause"라는 프로그램 만 실행됩니다 ( "pause"라는 실행 파일이 없어야합니다!).
_getch를 사용하는 "Pause ()"함수를 작성하십시오. 물론 _getch는 플랫폼에 따라 다릅니다 (참고 : "conio.h"에 정의되어 있음). 그러나 system()
Windows에서 개발할 때보 다 훨씬 좋으며 동일한 효과가 있습니다 (텍스트를 제공하는 것은 귀하의 책임 임) cout 정도).
기본적으로 두 줄의 코드를 추가하고 한 줄에 훨씬 더 유연한 메커니즘을 추가 할 수있을 때 왜 그렇게 많은 잠재적 문제가 발생합니까?
- 느리게 : 간단한 조작을 위해 불필요한 Windows 코드와 별도의 프로그램을 많이 뛰어 넘어야합니다.
- 휴대용이 아님 : 일시 중지 프로그램에 따라 다름
- 좋은 스타일이 아님 : 시스템 호출은 실제로 필요한 경우에만 수행해야 합니다.
- 더 많은 타이핑 : System ( "pause")가 getchar ()보다 깁니다
간단한 getchar ()는 잘 작동합니다.
system("pause");
Ungood Practice ™를 사용하면
완전히 불필요 합니다.
Visual Studio에서 프로그램을 실행할 때 프로그램의 콘솔 창을 계속 열어 두려면 Ctrl+ F5를 사용 하여 디버깅하지 않고 프로그램을 실행하거나의 마지막 오른쪽 괄호}
에 중단 점을 배치하십시오main
. 따라서 Visual Studio에는 아무런 문제가 없습니다. 물론 명령 줄에서 실행할 때 전혀 문제가 없습니다.
커맨드 라인에서 프로그램을 실행할 때 문제가되고 성가시다 . 대화식 실행을 위해서는 목적에 상관없이 마지막에 키를 눌러야합니다. 그리고pause
매우 원치 않는 일부 작업의 자동화에 사용하기 위해 !휴대용이 아닙니다.
유닉스 랜드에는 표준pause
명령 이 없습니다 .
The pause
command is an internal cmd.exe
command and can't be overridden, as is erroneously claimed in at least one other answer. I.e. it's not a security risk, and the claim that AV programs diagnose it as such is as dubious as the claim of overriding the command (after all, a C++ program invoking system
is in position to do itself all that the command interpreter can do, and more). Also, while this way of pausing is extremely inefficient by the usual standards of C++ programming, that doesn't matter at all at the end of a novice's program.
So, the claims in the horde of answers before this are not correct, and the main reason you shouldn't use system("pause")
or any other wait command at the end of your main
, is the first point above: it's completely unnecessary, it serves absolutely no purpose, it's just very silly.
In summary, it has to pause the programs execution and make a system call and allocate unnecessary resources when you could be using something as simple as cin.get(). People use System("PAUSE") because they want the program to wait until they hit enter to they can see their output. If you want a program to wait for input, there are built in functions for that which are also cross platform and less demanding.
Further explanation in this article.
You can use std::cin.get()
from iostream
:
#include <iostream> // std::cout, std::cin
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
Besides, system('pause')
is slow, and includes a file you probably don't need: stdlib.h
. It is platform-dependent, and actually calls up a 'virtual' OS.
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.
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.)
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.
system("pause");
is wrong because it's part of Windows API and so it won't work in other operation systems.
You should try to use just objects from C++ standard library. A better solution will be to write:
cin.get();
return 0;
But it will also cause problems if you have other cin
s in your code. Because after each cin
, you'll tap an Enter
or \n
which is a white space character. cin
ignores this character and leaves it in the buffer zone but cin.get()
, gets this remained character. So the control of the program reaches the line return 0
and the console gets closed before letting you see the results.
To solve this, we write the code as follows:
cin.ignore();
cin.get();
return 0;
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.
the pro's to using system("PAUSE"); while creating the small portions of your program is for debugging it yourself. if you use it to get results of variables before during and after each process you are using to assure that they are working properly.
After testing and moving it into full swing with the rest of the solution you should remove these lines. it is really good when testing an user-defined algorithm and assuring that you are doing things in the proper order for results that you want.
In no means do you want to use this in an application after you have tested it and assured that it is working properly. However it does allow you to keep track of everything that is going on as it happens. Don't use it for End-User apps at all.
It's all a matter of style. It's useful for debugging but otherwise it shouldn't be used in the final version of the program. It really doesn't matter on the memory issue because I'm sure that those guys who invented the system("pause") were anticipating that it'd be used often. In another perspective, computers get throttled on their memory for everything else we use on the computer anyways and it doesn't pose a direct threat like dynamic memory allocation, so I'd recommend it for debugging code, but nothing else.
참고URL : https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong
'Programing' 카테고리의 다른 글
JS를 사용하지 않고 이미지를 드래그하거나 선택하지 못하도록 방지 (0) | 2020.07.15 |
---|---|
Objective-C의 선택기? (0) | 2020.07.14 |
Oracle에서 예약어를 어떻게 피할 수 있습니까? (0) | 2020.07.14 |
새로운 (그리고 비어있는) "루트"브랜치를 만드는 방법? (0) | 2020.07.14 |
Java를 사용하여 Selenium WebDriver에서 마우스 오버 기능을 수행하는 방법은 무엇입니까? (0) | 2020.07.14 |