setjmp/longjmp query.

S

sh.vipin

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.
 
E

Eric Sosman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top