longjmp

Use of setjmp and longjmp in C when linking to C++ libraries

拥有回忆 提交于 2019-12-23 09:17:47
问题 I would like to use setjmp and longjmp in a C program that links to a library that is implemented in C++ (but has a C API). The C++ code does do dynamic memory allocation and pointers get passed through the API, but as long as the C side of the code manages those (opaque) objects correctly, there shouldn't be any messing up when using longjmp, right? I know it's not safe to use these functions in C++ code, but should it be safe in C code that is linked to C++ code? 回答1: The fact that you call

Implementing setjmp and longjmp in C without built in functions or assembly (getting incorrect return values)

泄露秘密 提交于 2019-12-22 06:58:04
问题 I'm trying to test 2 of my functions that sort of mimic setjmp and longjmp for a homework - which is pretty difficult since we're not allowed to use built in functions or assembly asm() to implement the longjmp and setjmp functions. (Yes, that's really the assignment.) Problem : I keep getting wrong return values. So, in short, when main() calls foo() and foo() calls bar(), and bar() calls longjump(), then bar() should not return to foo() but instead setjmp() should return to main with return

How to generate an R warning safely in Rcpp

守給你的承諾、 提交于 2019-12-20 17:33:13
问题 We know that calling Rf_error() should be avoided in Rcpp as it involves a longjmp over C++ destructors on the stack. This is why we rather throw C++ exceptions in Rcpp code (like throw Rcpp::exception("...") or via the stop("...") function). However, R warnings may also result in a call to Rf_error() (this behavior depends on the warn option). So, a call to Rf_warning() is also risky. Rcpp::sourceCpp(code = ' #include <Rcpp.h> using namespace Rcpp; class Test { public: Test() { Rcout <<

trapping signals in a multithreaded environment

拜拜、爱过 提交于 2019-12-14 00:48:21
问题 I have a large program that needs to be made as resilient as possible, and has a large number of threads. I need to catch all signals SIGBUS SIGSEGV , and re-initialize the problem thread if necessary, or disable the thread to continue with reduced functionality. My first thought is to do a setjump , and then set signal handlers, that can log the problem, and then do a longjump back to a recovery point in the thread. There is the issue that the signal handler would need to determine which

Setjmp/longjmp in Ruby’s Continuation

左心房为你撑大大i 提交于 2019-12-11 07:28:32
问题 I was wondering about this while digging through the code of cont.c in Ruby’s current version. The documentation of setjmp says that calling longjmp on the jmp_buf structure after the caller of setjmp returned is an error. But, it seems that Ruby does this happily and does not crash: https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L522 https://github.com/ruby/ruby/blob/05f087c844f0e1e7cfe323edcf591de64069a289/cont.c#L775 (+ a couple more usages with fibers)

How does Non - local Jumps in C defined in setjmp.h work?

白昼怎懂夜的黑 提交于 2019-12-10 17:57:02
问题 The C Reference Manual , Appendix B describes two functions setjmp and longjmp for something called non-local jumps . Apart from the basic understanding that setjmp saves the state information and longjmp restores the state , I haven't been able to understand the exact flow and use cases for this feature. So, what exactly does this feature accomplish and where is it useful? 回答1: As for the control flow: setjmp returns twice, and longjmp never returns. When you call setjmp for the first time,

setjmp and longjmp - understanding with examples

蓝咒 提交于 2019-12-08 17:41:29
问题 I know the definition of setjmp and longjmp. setjmp stores the environment in stack context and the other one restores. But i think there is somewhere some lack of understanding in my part. Can someone explain me, with the help of good examples as how can i assure, and how it will be saved and how it will be restored? I saw the there are a lot of CPU registers pointed in jmp_buf. But how do i assure that it is restored? Kindly help me to explain with neat examples. I googled and referred to

question with longjmp

a 夏天 提交于 2019-12-08 07:44:33
问题 I want to use longjmp to simulate goto instruction.I have an array DS containing elements of struct types (int , float, bool ,char). I want to jump to the place labled "lablex" where x is DS[TOP].int_val. how can I handle this? sample code : ... jmp_buf *bfj; ... stringstream s;s<<"label"<<DS[TOP].int_val; bfj = (jmp_buf *) s.str(); longjmp(*bfj,1); but as I thought it's having problem what should I do? error: output.cpp: In function ‘int main()’: output.cpp:101: error: invalid cast from type

Objective-C ARC and longjmp

风流意气都作罢 提交于 2019-12-08 03:24:36
问题 What is the best practice for mixing Objective-C ARC with longjmp ? I am using Lua as scripting language, and my platform exports custom library for scripts. Entry points do check arguments with luaL_checkinteger(L, 2) (among others), which, in turn, may call luaL_typerror(L, 2, ...) , that is implemented in Lua with setjmp / longjmp . As far as I know, ARC simply auto-generates retain / release code, but what happens if it longjmp s out of scope? Will this code leak on mistyped arguments?

If I jump out of a catch-block with “goto”, am I guaranteed that the exception-object will be free'ed?

耗尽温柔 提交于 2019-12-04 17:09:36
问题 I have such code as follows try { doSomething(); } catch(InterruptException) { goto rewind_code; } if(0) { rewind_code: longjmp(savepoint, 1); } My question is, is the exception object that is stored by the C++ runtime free'ed when I goto out of the catch block? Or is the runtime allowed to cache it until the surrounding function exists or something like that? I simply want to ensure that if I execute above code multiple times, each time taking the rewind code, I won't leak memory (because