簡體   English   中英

使用 setjmp 和 longjmp

[英]Using the setjmp and longjmp

有這么簡單的C代碼

#include <stdio.h>
#include <setjmp.h>

void Com_Error(int);

jmp_buf abortframe;

int main() {
    
    if (setjmp (abortframe)){
        printf("abortframe!\n");
        return 0;           
    }
    
    Com_Error(0);
    
    printf("main end\n");    
    return 0;
}

void Com_Error(int code) {
    // ...
    longjmp (abortframe, code);
    //...
}

我越來越:

中止幀!

我的問題是為什么它會打印abortframe! 如果我們通過0 (NOT true ) 並且因此條件if (setjmp (abortframe)){...}不應該滿足所以沒有abortframe! 字符串打印?

閱讀友好手冊(C17 7.13.2.1):

longjmp 函數不能導致 setjmp 宏返回值 0; 如果 val 為 0,則 setjmp 宏返回值 1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM