aCuria
aCuria

Reputation: 7175

Where does abort() and terminate() "live"?

Regarding the terminate handler,

As i understand it, when something bad happens in code, for example when we dont catch an exception,

terminate() is called, which in turn calls abort()

set_terminate(my_function) allows us to get terminate() to call a user specified function my_terminate.

my question is: where do these functions "live" they don't seem to be a part of the language, but work as if they are present in every single cpp file, without having to include any header file.

Upvotes: 5

Views: 3229

Answers (3)

Romain Hippeau
Romain Hippeau

Reputation: 24365

set_terminate - terminate handler function

Sets f as the terminate handler function.

A terminate handler function is a function automatically called when the exception handling process has to be abandoned for some reason. This happens when a handler cannot be found for a thrown exception, or for some other exceptional circumstance that makes impossible to continue the handling process.

The terminate handler by default calls cstdlib's abort function

Upvotes: 2

anon
anon

Reputation:

I don't see why you think there is no need to include a header:

int main() {
    abort();
}

gives the following error for me:

error: 'abort' was not declared in this scope

Neither C nor C++ have any "special" functions - if you want to use a function, you must declare it somehow. These two live in the C++ Standard Library, and are declared in cstdlib and exception. Of course, these headers themselves may be #included by other headers, thus making the functions available, but this is not specified by the standard.

Upvotes: 3

If there are default handler functions for terminate and abort that you did not install yourself, they'd have to be in the runtime library provided by your compiler.

Normally, every program is linked against the runtime library (e.g. glibc under Linux). Among other reasons, this is because the runtime library contains "hidden" code for essential things, e.g. code that calls your main function at startup.

Upvotes: 5

Related Questions