----- Original Message ----- 
From: "deepa laxmi" 
To: 
Sent: Thursday, April 08, 2004 12:09 PM
Subject: [UTTARA] set jmp and long jmp?


> hi,
>     pl can anybody tell me where setjmp and  longjmp can been used and why?
> 
> Deepa.R     
>

    The  header defines the macro setjmp, and declares a function
longjmp() and a type, jmp_buf.  Following are the signatures:

        int setjmp(jmp_buf env);

    The setjmp macro saves its calling environment in its jmp_buf argument for
later use by the longjmp function.  The return value is zero if it is a direct
invocation, and non-zero if the return is from a call to longjmp().

        void longjmp(jmp_buf env, int val);

    The longjmp() restores the environment saved by the recent call to setjmp().
The parameter VAL is returned by setjmp().

    Following program illustrates a simple usage of setjmp() and longjmp().

#include 
#include 
#include 
#include 
#include 

jmp_buf env;

void
fpe ( int sig )
{
    longjmp ( env, sig );
}

int
main ( void )
{
    int val = 5;
    signal ( SIGFPE, fpe );
    printf ( "Before: %d\n", val );
    
    switch ( setjmp ( env ) )
    {
        case 0: puts ( "Return from setjmp" );
                val = val / 0; /* SIGFPE is generated here */
                break;

        case SIGFPE: puts ( "Return from longjmp: FPE detected" );
                 /* 
                  * Here we can unroll all operation that
                  * caused floating point exception
                  */
                 printf ( "After: %d\n", val );
                 break;
    }
    return EXIT_SUCCESS;
}

    There are few points which can be noted:

    *   longjmp() should not be called from an exit handler, i.e., function
        registered by atexit() function.

    *   In an C99 implementation, the longjmp() call that returns back to 
        point of setjmp() invocation might cause memory associated with
        variable lenght array to loose.

    Source: geocities.com/vijoeyz/faq/c

               ( geocities.com/vijoeyz/faq)                   ( geocities.com/vijoeyz)