setjmp / longjmp

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 9 04:18:40 PDT 2015


On Tue, 09 Jun 2015 11:57:03 +0100, Stewart Gordon wrote:

> On 27/04/2015 10:41, ketmar wrote:
> <snip>
>> i believe this has something to do with exception frames. but it needs
>> further investigation.
> 
> What is an exception frame, exactly?

to correctly do unwinding and other interesting things exception handler 
should do, compiler must establish special hidden structures in the form 
of single-linked list, so runtime can traverse it backwards if it needs 
to unwind the stack (i.e. calling destructors for structs, for example).

> Moreover, are these frames applicable even in sections of code where no
> throwing or catching of exceptions takes place?

exception can be thrown by function that is called from your code. 
compiler needs to setup structures to correctly "unwind" the stack in 
this case. if nothing will throw, that setup cost as almost zero and can 
be ignored. but playing games with stack can lead to corruption of such 
structures (runtime doesn't know that stack was changed, and it can't 
update it's internal data structures accordingly). this may be harmless, 
or may lead to crash, or may lead to memory corruption without immediate 
crashing, or... effects are unpredictable.

tldr; don't use setjmp/longjmp in the language with exceptions, unless 
you can describe it's runtime internals and exception handling down to 
assembler code when you're awaken at night completely drunk. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150609/185e34ed/attachment.sig>


More information about the Digitalmars-d-learn mailing list