Thread: setjmp/longjmp not works

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    2

    Question setjmp/longjmp not works

    Look at the following code:

    Code:
    #include<stdio.h>
    #include<stdint.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<setjmp.h>
    #include<string.h>
    
    typedef struct {
      jmp_buf jump_buffer;
      uint32_t error_code;
      char *error_desc;
    } exception_t;
    
    void exception_throw(exception_t * e, uint32_t error_code, char *error_desc)
    {
      e->error_code = error_code;
      e->error_desc = error_desc;
      longjmp(e->jump_buffer, 1);
    }
    
    bool exception_catch(exception_t * e)
    {
      return setjmp(e->jump_buffer);
    }
    
    int main()
    {
      exception_t ex;
      memset(&ex, 0, sizeof(exception_t));
    
      if (exception_catch(&ex)) {
        printf("exception thrown: %s", ex.error_desc);
        return ex.error_code;
      } else {
        exception_throw(&ex, 1337, "fun");
      }
    
      return 0;
    }
    Why?
    Last edited by Salem; 01-07-2019 at 04:44 AM. Reason: Removed crayola eyesore

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by cppreference.com
    If the function that called setjmp has exited, the behavior is undefined (in other words, only long jumps up the call stack are allowed)
    exception_catch() exits in order to jump to the else clause.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    2
    Quote Originally Posted by GReaper View Post
    exception_catch() exits in order to jump to the else clause.
    Thanks for the response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. understanding setJmp
    By Darkinyuasha1 in forum C Programming
    Replies: 1
    Last Post: 02-23-2015, 04:30 PM
  2. Replies: 8
    Last Post: 04-18-2012, 10:16 PM
  3. Question about setjmp /longjmp
    By hussein in forum C Programming
    Replies: 2
    Last Post: 08-16-2009, 03:47 AM
  4. Usage of setjmp(buf) and longjmp?
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-07-2009, 07:41 AM
  5. it never works...
    By Ryce in forum Game Programming
    Replies: 5
    Last Post: 08-30-2001, 07:31 PM

Tags for this Thread