Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 436471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:28:51+00:00 2026-05-12T20:28:51+00:00

I have a few questions relating to setjmp/longjmp usage – What is the use

  • 0

I have a few questions relating to setjmp/longjmp usage –

  1. What is the use of setjmp(jmp___buf stackVariables) returning 0. It is a default, which we cannot influence.

  2. Is the only significance of setjmp(stackVariables) is to push the stack in stackVariables. And basically 0 tells us if the stack was pushed on stack_variables successfully.

  3. Their is one occasion when the value is non-zero (any non-zero) when you return from a longjmp. What is returning from a lomgjmp, when do you return from longjmp, when your exception is handled. This is setup is really confusing.

  4. Can some please relate it to try/throw and catch. And would be really great, if some good examples of setjmp/longjmp could be provided.

  5. Is longJmp like throw, and it is called just after the place where exception can be raised.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:28:52+00:00Added an answer on May 12, 2026 at 8:28 pm

    The C99 spec gives:

    If the return is from a direct invocation, the setjmp macro returns the value zero. If the
    return is from a call to the longjmp function, the setjmp macro returns a nonzero
    value.

    So the answer to 1 is that a zero indicates you have called setjmp the first time, and non-zero indicates it is returning from a longjmp.

    1. It pushes the current program state. After a longjmp, the state is restored, control returns to the point it was called, and the return value is non-zero.

    2. There are no exceptions in C. It’s sort-of similar to fork returning different values depending whether you’re in the original process, or a second process which has inherited the environment, if you’re familiar with that.

    3. try/catch in C++ will call destructors on all automatic objects between the throw and the catch. setjmp/longjmp will not call destructors, as they don’t exist in C. So you are on your own as far as calling free on anything you’ve malloced in the mean time.

    With that proviso, this:

    #include <stdio.h>
    #include <setjmp.h>
    #include <string.h>
    #include <stdlib.h>
    
    void foo ( char** data ) ;
    void handle ( char* data ) ;
    jmp_buf env;
    
    int main ()
    {
        char* data = 0;
    
        int res = setjmp ( env ); 
        // stored for demo purposes. 
        // in portable code do not store 
        // the result, but test it directly.
    
        printf ( "setjmp returned %d\n", res );
    
        if ( res == 0 )
            foo ( &data );
        else
            handle ( data );
    
        return 0;
    }
    
    
    void foo ( char** data )
    {
        *data = malloc ( 32 );
    
        printf ( "in foo\n" );
    
        strcpy ( *data, "Hello World" );
    
        printf ( "data = %s\n", *data );
    
        longjmp ( env, 42 );
    }
    
    void handle ( char* data )
    {
        printf ( "in handler\n" );
    
        if ( data ) {
            free ( data );
            printf ( "data freed\n" );
        }
    }
    

    is roughly equivalent to

    #include <iostream>
    
    void foo ( ) ;
    void handle ( ) ;
    
    int main ()
    {
        try {
            foo ();
        } catch (int x) {
            std::cout << "caught " << x << "\n";
            handle ();
        }
    
        return 0;
    }
    
    void foo ( )
    {
        printf ( "in foo\n" );
    
        std::string data = "Hello World";
    
        std::cout << "data = " << data << "\n";
    
        throw 42;
    }
    
    void handle ( )
    {
        std::cout << "in handler\n";
    }
    

    In the C case, you have to do explicit memory management (though normally you’d free it in the function which malloc’d it before calling longjmp as it makes life simpler)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions relating to a Transaction object that I'm creating. Transaction
I have a few questions about PHP memory usage. I'm going to run some
I have a few questions about security using PHP, I dont want to use
I have few questions about the memory usage in ssis package. If i am
I have few questions about ssis transction isolation levels. consider a scenario:I have an
I have few questions about using lock to protect my shared data structure. I
I have few questions, I want to show the alert window on my Update
I have few basic questions on session management in GWTP. Now, we create a
I have few basic questions on session management in GWTP. Client : GWTP, Server
I have a few questions that will help me understand things better if answered:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.