setjmp/longjmp query.

Discussion in 'C Programming' started by sh.vipin@gmail.com, Nov 22, 2008.

  1. Guest

    Based on some study about setjmp / longjmp I have developed following
    notions about setjmp / longjmp .
    Would like to get feedback on them ??

    Q1. Is there any point in keeping jmp_buf variable local to a
    function. Because if the variable is local then any other function can
    not return to this state (without using any other global variable)
    because jmp_buf variable is local to caller function. And if doing
    error handling within a function only can be achieved using goto
    itself which will be more efficient. If one has to do the error
    handling across multiple functions it must use global "jmp_buf"
    variable.

    Q2. When doing real error handling in C Programs across different
    function calls, we will always have to keep jmp_buf variable global.
    Becasue when we want to make longjmp call we must know which variable
    to return. Just for above reason.

    Q3. When there is a hierarchy of function calls, you can save the
    state at multiple places in different jmp_buf variables. Now when
    making longjmp call, it must know at which place to return. There
    doesn't seem to be a way of returning by default to place where last
    setjmp call was made (i.e. there is nothing similar to associating
    else with last if ). Though that makes sense as C doesn't support
    full syntax support for error handling like C++(i.e. try catch).
    Although everything seems achievable by some macro writing and
    overriding.
    , Nov 22, 2008
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > Based on some study about setjmp / longjmp I have developed following
    > notions about setjmp / longjmp .
    > Would like to get feedback on them ??
    >
    > Q1. Is there any point in keeping jmp_buf variable local to a
    > function. Because if the variable is local then any other function can
    > not return to this state (without using any other global variable)
    > because jmp_buf variable is local to caller function. And if doing
    > error handling within a function only can be achieved using goto
    > itself which will be more efficient. If one has to do the error
    > handling across multiple functions it must use global "jmp_buf"
    > variable.


    You could pass a local jmp_buf (a pointer, really) to
    a called function, which could then longjmp() to it. Or you
    might have a global pointer to a local jmp_buf, presumably
    with a discipline that pushed and popped pointers as scopes
    entered and exited.

    > Q2. When doing real error handling in C Programs across different
    > function calls, we will always have to keep jmp_buf variable global.
    > Becasue when we want to make longjmp call we must know which variable
    > to return. Just for above reason.


    You have made a statement and labeled it as a question.
    I do not know what you intended to ask, so I cannot answer.

    > Q3. When there is a hierarchy of function calls, you can save the
    > state at multiple places in different jmp_buf variables. Now when
    > making longjmp call, it must know at which place to return. There
    > doesn't seem to be a way of returning by default to place where last
    > setjmp call was made (i.e. there is nothing similar to associating
    > else with last if ). Though that makes sense as C doesn't support
    > full syntax support for error handling like C++(i.e. try catch).
    > Although everything seems achievable by some macro writing and
    > overriding.


    You have made a statement and labeled it as a question.
    I do not know what you intended to ask, so I cannot answer.

    The keyboards common in my part of the world have a key
    that produces a "?" sign: it is usually on the same key that
    produces a "/" when un-shifted and is positioned just to the
    left of the right-hand Shift key. Search your keyboard to
    see whether it has such a key, and consider using it when it
    would help to make your meaning clearer. As things stand, I
    don't know what! you're? asking.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 22, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Baruchel

    How dirty is setjmp+fopen+longjmp ?

    Thomas Baruchel, Oct 2, 2003, in forum: C Programming
    Replies:
    2
    Views:
    456
    Eric Sosman
    Oct 2, 2003
  2. Mantorok Redgormor

    setjmp, longjmp

    Mantorok Redgormor, Nov 12, 2003, in forum: C Programming
    Replies:
    2
    Views:
    427
    Ian Woods
    Nov 12, 2003
  3. someone
    Replies:
    5
    Views:
    3,459
    SM Ryan
    May 1, 2004
  4. Michael B Allen

    Is setjmp/longjmp ok?

    Michael B Allen, May 1, 2004, in forum: C Programming
    Replies:
    11
    Views:
    2,252
    -wombat-
    May 4, 2004
  5. Ravi Uday

    setjmp/longjmp

    Ravi Uday, Aug 3, 2004, in forum: C Programming
    Replies:
    2
    Views:
    544
    Dave Vandervies
    Aug 6, 2004
Loading...

Share This Page