Difference between std::quick_exit and std::abort in C++
In this tutorial, you will learn the difference between std::quick_exit and std::abort in C++. Before understanding the difference between std::quick_exit() and std::abort() in C++, first lets understands both individually.
std::quick_exit()
The quick_exit() function causes a program to terminate normally without actually entirely cleansing its resources.
Syntax :
void quick_exit(int exit_code) no except;
The quick_exit() function exits the program with cleanup on the authority of at_quick_exit().
The function std::quick_exit() terminates the application by first calls all functions that are registered at_quick_exit() then it terminates the application.
Above all, functions registered to at_quick_exit() function called in their backward order of registrations.
Syntax for at_quick_exit() :
int at_quick_exit( void (*func)(void) );
Hence, at_quick_exit() declares the func which pointer to a function and it is called on a quick program termination. The at_quick_exit() function returns 0(zero) if registration succeeds otherwise non-zero.
//use of at_quick_exit() and quick_exit() #include <cstdlib> void atexitf() { cout << "Inside quick exit function."; } int main() { at_quick_exit(atexitf); cout << " Inside the main function."; quick_exit(0); cout << "End of the main function"; return 0; }
Above program demonstrated functionality of both exit functions. Where at_quick_exit() function calls atexitf(), thereby prints “Inside main function”.
Output :
Inside quick exit function. Inside the main function.
Then the next statement i.e., “Inside the main function” prints on the console. And hence the program terminates.
std::abort()
The std::abort() function basically aborts the program. By aborting a program it might force to strike on some systems.
Syntax :
void abort();
The abort function terminates the program instantly. Above all, it has no parameters, no return type, and no exceptions.
// use of abort() #include <cstdlib> int main () { FILE *filepointer; filepointer = fopen("somefile.txt", "r"); if (filepointer== NULL) { fputs("Error in opening the file \n", stderr); abort(); } fclose(filepointer); return 0; }
The above program works as follows if the file somefile.txt is not able to open in reading mode. Then the file pointer will contain a NULL value. And thereby displays the error messages. And then it calls the abort() function which in turn terminates the program.
Conclusion: Difference between std::quick_exit and std::abort
Here we will understand the difference between std::quick_exit() and std::abort() in C++ as well as its resemblances.
Resemblances: Both functions used for termination of the program still unique. Both functions defined in the cstdlib header. They have no parameters, exceptions and no return value.
Differences: The std::quick_exit() terminates all function registered with at_quick_exit(). The std::abort() function terminates the program in which the functions is either registered to at_quick_exit() or not. The std::abort() function terminates without actually executing the remaining program and not removing the remaining input/output.
Recommended blogs:
Leave a Reply