Is it good programming practice to use setjmp and longjmp in C?

前端 未结 8 1873
误落风尘
误落风尘 2021-02-07 10:28

I\'m a C++ programmer and used to OO languages with good exception handling.

From what I can understand, setjmp and longjmp are essentially a c-style way to propogate ex

相关标签:
8条回答
  • 2021-02-07 11:01

    Apart from the answers posted so far, I would like to add that the setjmp in C allow the implementation of tail recursiveness in an elegant way.

    So yes, not only it's a good practice but this is the only portable elegant way to do some things in C.

    0 讨论(0)
  • 2021-02-07 11:02

    You should have brought up the issue that goto and maybe longjmp are not good before Exception handling (Try+catch+finally+..) became popular (using longjmp). If people can't handle a sparse usage of goto to make things easier then how can they handle all of the permutations of logic bypassed by the longjmp exception handling and then continue on like nothing happened? The real issue is people are looking for rules instead of concepts.

    0 讨论(0)
  • 2021-02-07 11:14

    Essentially, you're right in your assertion that jmp-style propagation is essentially the same thing as goto. Read Dijkstra's (famous and controversial) paper about gotos which (I think) provides sensible reasoning for why gotos should rarely be used. Unless you know exactly why you're doing what you're doing (or you're working in very specific fields -- such as embedded programming), you should not touch either goto or longjmp.

    0 讨论(0)
  • 2021-02-07 11:17

    setjmp/longjmp is a useful way to implement an own exception-handling in pure C. http://sourceware.org/pthreads-win32/announcement.html

    0 讨论(0)
  • 2021-02-07 11:19

    There are some correct uses of setjmp/longjmp. Implementing coroutines with them is virtually impossible, since you have to use (nonportable) tricks (read: inline assembly) to switch stacks.

    One use of setjmp/longjmp is to catch floating point signals, but this messes up the C++ stack unwinding. Correct in C though.

    You can also implement some form of stack unwinding (by maintaining you own cleanup handler stack) and implement true destructors and exceptions in C with them. This is very handy in large projects: the lack of a correct error handling mechanism is the weak point of C. However, it is quite difficult to do it correctly, and you'll have to write a bunch of macros to facilitate the task.

    0 讨论(0)
  • 2021-02-07 11:20

    they are used to implement coroutines. There are a couple of c++ coroutine libraries running around on the net, that in Unix/Linux will use setjmp/longjmp to implement the functionality.

    So, if your goal is to implement a coroutine library, then it is a moot point if its good practice or not, since on those platforms it is the only way to support that functionality.

    if your goal is to use a coroutine library, you should search for some of these instead. There is even a boost vault proposal called boost::context, which is already approved.

    0 讨论(0)
提交回复
热议问题