日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

setjump, longjump学习

發布時間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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).

#include <stdio.h> #include <setjmp.h>jmp_buf bufferA, bufferB;void routineB(); // forward declaration void routineA() {int r ;printf("(A1)\n");r = setjmp(bufferA);if (r == 0) routineB();printf("(A2) r=%d\n",r);r = setjmp(bufferA);if (r == 0) longjmp(bufferB, 20001);printf("(A3) r=%d\n",r);r = setjmp(bufferA);if (r == 0) longjmp(bufferB, 20002);printf("(A4) r=%d\n",r); }void routineB() {int r;printf("(B1)\n");r = setjmp(bufferB);if (r == 0) longjmp(bufferA, 10001);printf("(B2) r=%d\n", r);r = setjmp(bufferB);if (r == 0) longjmp(bufferA, 10002);printf("(B3) r=%d\n", r);r = setjmp(bufferB);if (r == 0) longjmp(bufferA, 10003); }int main(int argc, char **argv) {routineA();return 0; }

?

轉載于:https://my.oschina.net/mskk/blog/742968

總結

以上是生活随笔為你收集整理的setjump, longjump学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。