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.

175 questions
0
votes
0 answers

Longjmp arguments give a type error

I am trying to call longjmp. Setjmp works, but longjmp gives me a type error. void thread_start_threading(void) { setjmp(env); dispatch(); current_thread->function(current_thread->arg); } And then later: void thread_yield(void) { …
Brandon
  • 3,573
  • 6
  • 19
  • 21
0
votes
1 answer

How two independent jmp_bufs work?

Hi I want to ask about setjmp/longjmp. I tried to search, but I was unsucessuful... #include #include jmp_buf a, b; void jump() { int aa = setjmp(a); if (aa) { printf("Jump!\n"); } else { …
jirizaj
  • 35
  • 5
0
votes
2 answers

why am i seeing this libzdb configure error?

I am attempting to install libzdb on my macbook however I see the following error message when running the configure: configure:13334: error: setjmp is required the setjmp.h file resides within /usr/include and is specified within my "$PATH" as…
godzilla
  • 3,005
  • 7
  • 44
  • 60
0
votes
1 answer

call to longjmp is causing programme to exit with code 0 on msvc 2010

I'm trying to use setjmp/longjmp for error handling, however, the call to longjmp is causing the programme to exit with code 0 when compiled using MSVC 2010, here is the full message: The program '[5020] test.exe: Native' has exited with code 0…
has981
  • 425
  • 4
  • 18
0
votes
1 answer

longjmp out of vectored exception handler

Certain kinds of events in 64-bit Windows programs e.g. division by zero, null pointer reference, stack overflow if you detect that with a guard page, are normally handled (in a program that finds it necessary to handle them) with structured or…
rwallace
  • 31,405
  • 40
  • 123
  • 242
0
votes
3 answers

failure of setjmp/longjmp

a piece of code here jmp_buf mark; int Sub_Func() { int be_modify, jmpret; be_modify = 0; jmpret = setjmp( mark ); if( jmpret == 0 ) { // sth else here } else { // error handle…
nzomkxia
  • 1,219
  • 4
  • 16
  • 35
0
votes
3 answers

Are there performance problems with non-local jumps?

I am using non-local jumps (setjmp, longjmp). I would like to know if it can be a problem for the performances. Does setjmp save all the stack, or just some pointers ? Thanks.
md5
  • 23,373
  • 3
  • 44
  • 93
-1
votes
1 answer

Creating a loop using setJmp longJmp

I am trying to create an infinite loop using those functions (unsuccessfully). My code : jmp_buf buf1; void foo(){ int z = 4, y = 1; int v = setjmp(buf1); if(v == 0){ printf("A%d", z); longJmp(buf1, 1); } else if(v ==…
Asurii
  • 69
  • 6
-1
votes
1 answer

How to resolve MISRA C:2012 Rule 21.4?

I am using setjmp and longjmp in our code with standard library [#include ]. When I am trying to use it showing following MISRA error: Violation of MISRA C:2012 21.4: The standard header file shall not be used. and Use of…
-2
votes
3 answers

Use jmp_buf in multiple file

For clearly, please view my sample I have two files: main.cpp and myfunction.h This is main.cpp #include #include int main() { if ( ! setjmp(bufJum) ) { printf("1"); func(); } else { printf("2"); …
Phien
  • 148
  • 1
  • 14
1 2 3
11
12