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

前端 未结 8 1874
误落风尘
误落风尘 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:26

    You certainly don't want to use setjmp in C++, as you say that's what exceptions are for. You don't want to use them in C either because it's exceedingly hard to get right. Try very hard to find other solutions.

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

    setjmp and longjmp are macros used to bypass normal function call and return flow.
    The setjmp saves the calling env to be used by longjmp
    Use of these macros correctly is really hard and you can easily end up with undefined behavior.
    Because of this, it is mandated for example to restrict longjmp to 1 level of signal handler (best actually to not be called at all).
    In critical systems it is required not to be used at all.

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