From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f8a1,e7e13c943c611b62 X-Google-Attributes: gid10f8a1,public X-Google-Thread: 1164f8,be3d6a6131c61fa7 X-Google-Attributes: gid1164f8,public X-Google-Thread: 102b75,be3d6a6131c61fa7 X-Google-Attributes: gid102b75,public From: Andi Kleen Subject: Re: Exception handling Date: 1999/04/25 Message-ID: #1/1 X-Deja-AN: 470628728 Sender: andi@kali References: <36E9F2A0.9A401A09@pacific.net.sg> <924046418.053.46@news.remarQ.com> Content-Type: text/plain; charset=us-ascii Organization: unorganized. Mime-Version: 1.0 User-Agent: Gnus/5.070079 (Pterodactyl Gnus v0.79) Emacs/20.3 Newsgroups: comp.arch,comp.sys.dec,vmsnet.alpha zalman@netcom.com (Zalman Stern) writes: > Daniel P. B. Smith (dpbsmith@world.std.com) wrote: > : I didn't expect it to; I expected the exception to get thrown upward past > : and through the C library layer and get caught by the exception handlers > : in my calling routines above. That is, to do what it would have done if > : the C source code in the library had been compiled as C++ code by the C++ > : compiler. > > Generally, this works except that C does not have destructors so as the > stack is unwound, resources may leak. Also since the C code has no shot at > capturing control and cleaning up, data structures may be left in an > inconsistent state. Well, not quite. There are usually two general approaches at exception handling: - setjmp/longjmp based: No bad interactions with C and most other languages, because longjmps are a traditional part of C (and required by ANSI-C). You can throw an exception across a C function without problems, except that it doesn't know how to clean up itself in this case. Disadvantage: runtime overhead even when the exception is not thrown, because setjmp is often costly. - so-called "zero cost" exception schemes: the compiler generates some description of the stack frames with the destructors and puts it into the executable. The exception runtime function reads that table, unwinds the stack and calls destructors as needed. Advantage: less runtime overhead when no exception is thrown (although not zero cost because exceptions limit the choices of the optimizer e.g. to eliminate dead variables) Disadvantages: Often significant space overhead for the unwind tables. Only works when all functions have a unwind table - because of the space overhead even hybrid C/C++ compilers usually don't generate the unwind table in C mode without special options, because of the executable bloat. This means you have to prepare all code for exceptions, which makes e.g. callbacks through third party libraries often difficult. There are variants of the zero cost scheme (e.g. used by NT structured exception handling AFAIK) that don't keep a table, but diassemble functions beginnings to guess the stack frame size (and use some other approach to handle destructors). This trades some exception time for space. The problem is that these schemes make strong assumptions about how the stack is handled and the frame is set up, which makes them language and even compiler(-version) dependent. Most modern compilers seem to move to a variant of the "zero cost" scheme. Thus it is an unsafe assumption that exceptions can be thrown over old code. -Andi -- This is like TV. I don't like TV.