setjump, longjump学习
為什么80%的碼農都做不了架構師?>>> ??
Error handling
Suppose there is an error deep down in a function nested in many other functions and error handling makes sense only in the top level function.
It would be very tedious and awkward if all the functions in between had to return normally and evaluate return values or a global error variable to determine that further processing doesn't make sense or even would be bad.
That's a situation where setjmp/longjmp makes sense. Those situations are similar to situation where exception in other langages (C++, Java) make sense.
Coroutines
Besides error handling, I can think also of another situation where you need setjmp/longjmp in C:
It is the case when you need to implement?coroutines.
Here is a little demo example. I hope it satisfies the request from Sivaprasad Palas for some example code and answers the question of TheBlastOne how setjmp/longjmp supports the implementation of corroutines (as much as I see it doesn't base on any non-standard or new behaviour).
EDIT:
It could be that it actually?is?undefined behaviour to do a?longjmp?down?the callstack (see comment of MikeMB; though I have not yet had opportunity to verify that).
?
轉載于:https://my.oschina.net/mskk/blog/742968
總結
以上是生活随笔為你收集整理的setjump, longjump学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可转债第一天涨停限制
- 下一篇: 扩展名为com的文件是什么