exit ()와 abort ()의 차이점은 무엇입니까?
C와 C ++에서 exit()
와 의 차이점은 무엇 abort()
입니까? 오류가 발생하면 프로그램을 종료하려고합니다 (예외 아님).
abort()
atexit()
먼저 사용하여 등록 된 함수를 호출 하지 않고 객체의 소멸자를 먼저 호출 하지 않고 프로그램을 종료합니다 . exit()
프로그램을 종료하기 전에 둘 다 수행합니다. 자동 객체에 대해서는 소멸자를 호출하지 않습니다. 그래서
A a;
void test() {
static A b;
A c;
exit(0);
}
소멸 a
되고 b
적절하지만의 소멸자를 호출하지 않습니다 c
. abort()
객체의 소멸자를 호출하지 않습니다. 불행히도 C ++ 표준은 적절한 종료를 보장하는 대체 메커니즘을 설명합니다.
자동 저장 기간이있는
main()
객체 는 함수 에 자동 객체가없고 호출을 실행 하는 프로그램에서 모두 파괴 됩니다exit()
. 에main()
잡힌 예외를 발생시켜 제어를 그러한에 직접 전송할 수 있습니다main()
.
struct exit_exception {
int c;
exit_exception(int c):c(c) { }
};
int main() {
try {
// put all code in here
} catch(exit_exception& e) {
exit(e.c);
}
}
을 호출하는 대신 exit()
해당 코드 throw exit_exception(exit_code);
를 정렬하십시오 .
중단 SIGABRT 신호를 전송, 출구는 단지 일반 정리 작업을 수행하는 응용 프로그램을 닫습니다.
원하는대로 중단 신호를 처리 할 수 있지만 기본 동작은 오류 코드와 함께 응용 프로그램을 닫는 것입니다.
중단 은 정적 및 전역 구성원의 오브젝트 삭제를 수행하지 않지만 종료 됩니다.
물론 응용 프로그램이 완전히 닫히면 운영 체제는 사용 가능한 메모리와 기타 리소스를 비 웁니다.
모두에서 중지 및 종료 프로그램 종료, 리턴 코드는 응용 프로그램을 시작 부모 프로세스로 돌아갑니다 (기본 동작을 재정의하지 않은 가정).
다음 예를 참조하십시오.
SomeClassType someobject;
void myProgramIsTerminating1(void)
{
cout<<"exit function 1"<<endl;
}
void myProgramIsTerminating2(void)
{
cout<<"exit function 2"<<endl;
}
int main(int argc, char**argv)
{
atexit (myProgramIsTerminating1);
atexit (myProgramIsTerminating2);
//abort();
return 0;
}
코멘트:
중단 이 주석 처리되지 않은 경우 : 아무것도 인쇄되지 않으며 어떤 오브젝트의 소멸자가 호출되지 않습니다.
위와 같이 중단 이 언급 되면 someobject 소멸자가 호출되고 다음과 같은 결과가 나타납니다.
종료 기능 2
종료 기능 1
프로그램이 exit
()를 호출하면 다음과 같은 일이 발생합니다 .
- Functions registered by the
atexit
function are executed - All open streams are flushed and closed, files created by
tmpfile
are removed - The program terminates with the specified exit code to the host
The abort
() function sends the SIGABRT
signal to the current process, if it is not caught the program is terminated with no guarantee that open streams are flushed/closed or that temporary files created via tmpfile
are removed, atexit
registered functions are not called, and a non-zero exit status is returned to the host.
From the exit() manual page:
The exit() function causes normal process termination and the value of status & 0377 is returned to the parent.
From the abort() manual page:
The abort() first unblocks the SIGABRT signal, and then raises that signal for the calling process. This results in the abnormal termination of the process unless the SIGABRT signal is caught and the signal handler does not return.
abort
sends the SIGABRT
signal. abort
does not return to the caller. The default handler for the SIGABRT
signal closes the application. stdio
file streams are flushed, then closed. Destructors for C++ class instances are not, however (not sure on this one -- perhaps results are undefined?).
exit
has its own callbacks, set with atexit
. If callbacks are specified (or only one), they are called in the order reverse of their registration order (like a stack), then the program exits. As with abort
, exit
does not return to the caller. stdio
file streams are flushed, then closed. Also, destructors for C++ class instances are called.
참고URL : https://stackoverflow.com/questions/397075/what-is-the-difference-between-exit-and-abort
'Programming' 카테고리의 다른 글
TDD와 BDD의 주요 차이점은 무엇입니까? (0) | 2020.07.06 |
---|---|
목록 이해와 기능적 기능이 "for loop"보다 빠릅니까? (0) | 2020.07.06 |
GitHub-작성자 별 커밋 목록 (0) | 2020.07.06 |
PHP ORM : 교리와 프로 펠 (0) | 2020.07.06 |
Firebase를 사용하여 이름 속성별로 사용자 확보 (0) | 2020.07.06 |