In C programming the <stdlib.h> abort function terminates the program abruptly.The declaration of the function is given below.
Some points to note:
i) On executing the ‘abort’ function,for output stream buffer whether the data are flushed or the temporary files are removed is implementation-defined.The files are also not closed.
ii) The class destructor is not called if ‘abort’ is called untimely.
iii) In executing the ‘abort’ function the function pointed by the ‘atexit’ and ‘at_quick_exit’ parameter is not called.
Link: C atexit stdlib.h
iv) An implementation-defined status to signify an ‘unsuccessful termination’ is passed to the Operating System through ‘raise(SIGABRT)‘ call.
printf(“\nAfter calling abort”);
Output in VS,
Testing ‘abort’ function
A new window pop up with the message
“.. abort has been called”
As you can see the second line “printf(“\nAfter calling abort”);” is not executed because the program has been aborted in calling the ‘abort’ funtion.
A code example utilizing ‘abort’ function in a meaningful way is shown below.
#include <string.h> ///strlen function is include here
int main( )
char *c=”Sad string”;
char *cmem=(char*)malloc( strlen(c)+1 ); //allocated a memory of size same as the size of the ‘c’ string
memcpy( cmem , c , strlen(c)+1 );
//outputting the content of ‘cmem’
for(size_t i=0 ; i< strlen(c) ; i++)
printf(“%c” , *(mem+i) );
abort(); //if allocation fail abort is called
The program is simple,if ‘malloc’ fails to allocate the storage abort() is called else you can carry out whatever instructions you want.In this case a string is copied to the memory and the content is printed out.