Up Right Ausnahmenbehandlung

How g++ handles exceptions

von Walt Karas in Codeprojekt.com

As I understand it, GNU's C++ compiler (g++) uses a different implementation of exceptions. GNU's implementation avoids any added execution time in a function that has no catch or throw in it. It works more or less like the following. Suppose the function foo() calls the function bar(), and the function bar() then throws an exception. The stack frame (activation record) for the call of bar() contains a return address in (the object code of) foo(), which indicates where in foo() the call to bar() was made. The compiler generates a table in the object code of the form:

(return address, pointer to cleanup function)
This table contains the return address after each call (to a function that can throw) at a point in a function where objects with non-trivial destructors are active. The stack unwind code would look up the return address in bar()'s stack frame in this table, and get the pointer to the cleanup function. (Presumably, the table is sorted by return address, so a fast binary search can be done.) The unwind code gets the base pointer to foo()'s stack frame (by following the linked list of base pointers). The unwind code then calls the cleanup function, passing it foo()'s base pointer as a parameter. The cleanup function calls the destructors of the objects in foo()'s stack frame that would be active at the given point in foo(). The cleanup function can locate the objects to destruct, because they are at fixed offsets from the base pointer. This process is repeated until the function that catches the exception is reached.

Therefore, when using execptions in g++, you don't have to worry about slowing your application down. You only have to worry about the added size to the executable due to the return address table.

Bemerkung des Autors: IA-64 architecture supports exception handling through exception handling tables (and not frame based).


Informatik- und Netzwerkverein Ravensburg e.V Rudolf Weber