ในส่วนนี้เราจะดูความแตกต่างระหว่าง exit() และ _Exit() ใน C และ C++ ใน C exit() ยุติ calling process โดยไม่ execute โค้ดที่เหลือหลัง exit()
ใน C++11 มีฟังก์ชันใหม่ _Exit() ฟีเจอร์คืออะไร? exit() ทำ cleaning ก่อน terminate program เช่น connection termination, buffer flushes ฯลฯ _Exit() ไม่ทำ cleaning อะไรเลย ถ้าทดสอบด้วย atexit() จะไม่ทำงาน
ดูตัวอย่างสองกรณี ใช้ exit() ก่อน แล้วตามด้วย _Exit()
ตัวอย่าง (exit)
#include<bits/stdc++.h> using namespace std; void my_function(void) { cout << "Exiting from program"; } int main() { atexit(my_function); exit(10); }ผลลัพธ์
Exiting from program
ตัวอย่าง (_Exit)
#include<bits/stdc++.h> using namespace std; void my_function(void) { cout << "Exiting from program"; } int main() { atexit(my_function); _Exit(10); }ผลลัพธ์
In this case the output is blank. Nothing has come.