Safe use of `setjmp` and` longjmp`

I know that people always say that do not use longjmp, this is evil, it is dangerous.

But I think this might be useful for exiting deep recursions / nested function calls.

Is one longjmpfaster than many re-checks and returns like if(returnVal != SUCCESS) return returnVal;?

As for security, provided that dynamic memory and other resources are correctly released, the problem should not be the problem, right?

So far, it seems that it is longjmpnot difficult to use , and this even makes my code. I feel like using it a lot.

(IMHO in many cases there is no dynamic memory / resources allocated in deep recursion in the first place. Deep function call seems more common for data analysis / manipulation / verification. Dynamic allocation often occurs at a higher level before calling a function in which displayed setjmp.)

+4
source share
2 answers

setjmpand longjmpcan be regarded as a bad person exception mechanism. BTW, Ocaml exceptions are as fast as they are setjmp, but have clearer semantics.

, longjmp , , , , .

( Linux)

, , :

  • (malloc)
  • fopen -ing FILE*
  • (, )
  • ,
  • , , . X11 (, , GTK), ...
  • ....

, ( , setjmp longjmp), : setjmp longjmp .

, setjmp.

, malloc, ; GC_malloc malloc , free, ; setjmp ( GC_malloc setjmp longjmp).

( , setjmp, . )

RAII ++ 11 ( ). CPS.

setjmp (3), longjmp (3) ( sigsetjmp, siglongjmp setcontext (3)) , setjmp

+4

, setjmp (, setjmp).

, setjmp, , , .

setjmp longjmp , , , longjmp ( ) . , sigsetjmp siglongjmp .

+2

Source: https://habr.com/ru/post/1607236/


All Articles