Questions tagged [setjmp]

Anything related to the setjmp() and longjmp() routines in the C standard library (provided by the setjmp.h header file), which provide control flow that modify the usual call and return sequence of a subroutine.

The setjmp and longjmp pair of functions allow for the creation of non-local jumping in C programs—it is essentially a non-local goto that can jump across functions. This is a feature which is basically equivalent to exceptions in later languages, and has actually been used to write exception-like systems for C.

It works by loading the environmental state (jmp_buf) saved by an earlier setjmp over the current state. In the process, it also returns a value at the site of the original setjmp call. In practice, the environmental state is tied in with the stack, and this can cause some trouble.

For example, if the frame in which setjmp returns, then jumping to that particular frame invokes undefined behavior because that frame no longer exists. Another consequence of longjmp is that no stack unwinding occurs, so open files will not be closed and heap variables will not be freed.

In most C standard libraries, it can be found under setjmp.h. It has the call signature:

void longjmp(jmp_buf env, int val);

If you do not understand any of the above, then please do not use longjmp! It is very much a tool that can bite you if you are inexperienced. This is especially true for C++, which has exceptions which are harder to abuse than longjmp is.

160 questions
110
votes
8 answers

Practical usage of setjmp and longjmp in C

Can anyone explain me where exactly setjmp() and longjmp() functions can be used practically in embedded programming? I know that these are for error handling. But I'd like to know some use cases.
Pala
  • 1,661
  • 3
  • 12
  • 17
43
votes
2 answers

C++: Safe to use longjmp and setjmp?

Is it safe to use longjmp and setjmp in C++ on linux/gcc with regards to the following? Exception handling (I'm not implementing exception handling using longjmp/setjmp. I want to know what side effects longjmp/setjmp will have on standard…
jameszhao00
  • 6,903
  • 14
  • 54
  • 109
24
votes
1 answer

OCaml internals: Exceptions

I'm curious to know how exceptions are dealt with in OCaml runtime to make them so lightweight. Do they use setjmp/longjmp or do they return a special value in each function, and propagate it? It seems to me that longjmp would put a little strain on…
Waneck
  • 2,340
  • 15
  • 30
22
votes
3 answers

Why volatile works for setjmp/longjmp

After invoking longjmp(), non-volatile-qualified local objects should not be accessed if their values could have changed since the invocation of setjmp(). Their value in this case is considered indeterminate, and accessing them is undefined…
MetallicPriest
  • 25,675
  • 38
  • 166
  • 299
21
votes
5 answers

Multitasking using setjmp, longjmp

is there a way to implement multitasking using setjmp and longjmp functions
GingerJack
  • 2,698
  • 1
  • 15
  • 17
20
votes
6 answers

What are some "good" ways to use longjmp/setjmp for C error handling?

I have to use C for one project and I am thinking of using longjmp/setjmp for error handling as I think it will be much easier to handle error in one central place than return codes. I would appreciate if there are some leads on how to do this. I am…
dubnde
  • 4,171
  • 8
  • 40
  • 63
20
votes
2 answers

How to generate an R warning safely in Rcpp

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("...")…
gagolews
  • 12,140
  • 2
  • 43
  • 71
19
votes
5 answers

Longjmp out of signal handler?

From the question: Is it good programming practice to use setjmp and longjmp in C? Two of the comments left said: "You can't throw an exception in a signal handler, but you can do a longjmp safely -- as long as you know what you are doing. –…
John Humphreys - w00te
  • 32,140
  • 30
  • 125
  • 230
18
votes
1 answer

The Cost of C++ Exceptions and setjmp/longjmp

I wrote a test to measure the cost of C++ exceptions with threads. #include #include #include #include static const int N = 100000; static void doSomething(int& n) { --n; throw 1; } static void…
xiver77
  • 6,073
  • 4
  • 23
  • 58
17
votes
2 answers

What sense do these clobbered variable warnings make?

I have a function like this: #include jmp_buf buf; void func2(int g); extern int some_global; void func(int x) { if (setjmp(buf)) return; if (some_global) x += 5; func2(x); } GCC (gcc (Debian 4.4.5-8) 4.4.5)…
Dietrich Epp
  • 182,361
  • 34
  • 307
  • 387
14
votes
2 answers

C/C++ implementations where longjmp unwinds?

Are there major C/C++ implementations where the longjmp function "unwinds", i.e. where it interacts with destructors for automatic-storage objects, __attribute__((__cleanup__(...))), POSIX threads cancellation handlers, etc. rather than just…
R.. GitHub STOP HELPING ICE
  • 195,354
  • 31
  • 331
  • 669
13
votes
2 answers

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

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…
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175
13
votes
2 answers

What does each entry in the Jmp_buf structure hold?

I am running Ubuntu 9.10 (Karmic Koala), and I took a look at the jmp_buf structure which is simply an array of 12 ints. When I use setjmp, and pass in a jmp_buf structure—4 out of 12 entries are saved off. These 4 entries are the stack pointer,…
Setzer
  • 660
  • 4
  • 9
  • 17
12
votes
7 answers

How to (computed) goto and longjmp in C++?

I don't usually code C++, but a strange comp sci friend of mine got sick of looking at my wonderful FORTRAN programs and challenged me to rewrite one of them in C++, since he likes my C++ codes better. (We're betting money here.) Exact terms being…
Code Maker
  • 145
  • 1
  • 6
12
votes
2 answers

Detect recursion robustly even in the presence of non-local jumps

I have a particular function (a signal handler) for which I'd like to detect recursion, i.e. to figure out if the function has directly or indirectly called itself. The tricky bit is that the function calls some code not under its control at one…
nneonneo
  • 154,210
  • 32
  • 267
  • 343
1
2 3
10 11