Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using the setjmp and longjmp

Having such a simple C code

#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);
    //...
}

I’m getting:

abortframe!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My question is WHY does it prints the abortframe! if we pass 0 (NOT true) and hence the condition if (setjmp (abortframe)){...} should NOT be meet so no abortframe! string printed?

>Solution :

Read the friendly manual (C17 7.13.2.1):

The longjmp function cannot cause the setjmp macro to return the value 0; if val is 0, the setjmp macro returns the value 1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading