C++ Standard Library C++ STL Library

C++ <cstdlib> - abort() Function



The C <cstdlib> abort() function is used to abort the current process, producing an abnormal program termination.

The function raises the SIGABRT signal (as if raise(SIGABRT) was called). This, if uncaught, causes the program to terminate returning a platform-dependent unsuccessful termination error code to the host environment.

The program is terminated without destroying any object and without calling any of the functions passed to atexit() or at_quick_exit().

Syntax

void abort();
[[noreturn]] void abort() noexcept;

Parameters

No parameter is required.

Return Value

None.

Example:

The example below shows the usage of <cstdlib> abort() function.

#include <cstdio>
#include <cstdlib>
 
int main (){
  //open the file in read mode
  FILE *pFile = fopen("no_such_file.txt", "r");
  
  //aborts the process if file not found or
  //do not have required permission
  if (pFile == NULL) {
    fputs ("error opening file\n",stderr);
    abort();
  }  

  //reads and prints the whole content of the file
  int c = getc(pFile);
  while (c != EOF) {
    putchar(c);
    c = getc(pFile);
  }
  
  //close the file
  fclose(pFile);

  return 0;
}

The output of the above code will be:

error opening file
timeout: the monitored command dumped core
Aborted

❮ C++ <cstdlib> Library