473,799 Members | 3,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

longjmp vs goto

Can someone kindly clarify the distinction between long jumps and
gotos? Why is one frowned upon and not the other? Is there really
any situation where use of longjmp becomes inevitable? A real-life
code snippet would surely help.

Regards,
Nimmi
Nov 14 '05
22 6071
ni************* @yahoo.com (Nimmi Srivastav) wrote in message news:<8b******* *************** ***@posting.goo gle.com>...
Can someone kindly clarify the distinction between long jumps and
gotos? Why is one frowned upon and not the other? Is there really
any situation where use of longjmp becomes inevitable? A real-life
code snippet would surely help.

Regards,
Nimmi


A goto allows you to branch forward or backward within a function.
They are frowned upon because they can destroy the ability to debug
code by inspection. For example, given the code fragment

i = 7;
label: i = i * 4;
printf ("%d\n", i);

there's no guarantee that i will be set to 28 for any given execution
path. The i=7; statement may executed once, or it may never be
executed at all. You have to trace the function line by line and
account for every goto (if any are present) before you can say with
any confidence what value will be written to the screen.

There are cases where they are useful (such as breaking out of deeply
nested loops), but in general they should be avoided in favor of other
control structures (loops, if-else statements, etc.). In 14+ years as
a professional, I've used gotos twice in production code. If you ever
need to use one, use it to branch forward only, and never into a
compound statement (such as a loop or if-else statement).

A longjmp allows you to branch across function boundaries to a
previous calling function. They're useful for aborting out of a
deeply nested function (sort of like throwing an exception in C++).
They don't make as much of a mess wrt debugging as gotos, but they can
make code harder to understand.
Nov 14 '05 #11
In <8b************ *************@p osting.google.c om> ni************* @yahoo.com (Nimmi Srivastav) writes:
Can someone kindly clarify the distinction between long jumps and
gotos?
Try any good book on C.
Why is one frowned upon and not the other?
Neither is frowned upon by the competent programmer, but goto is more
likely to be abused by the incompetent programmer.
Is there really any situation where use of longjmp becomes inevitable?


No, but there are many situations where the use of longjmp greatly
simplifies the code structure. Imagine a program with multiple levels
of menus. Each menu must contain a "return to main menu" option. This
option can be trivially implemented with a longjmp call.

Another popular use is for error handling when the error was detected
deep in the function call chain. Instead of propagating the error back
through all the call chain, up to the function that is supposed to do
something about it, you get there with a single longjmp call.

Try to implement both examples without using longjmp and you'll see that,
although you can live without it, it's a lot more comfortable to live
with it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
[Followups set to comp.lang.c]

Capstar wrote:
This offcourse isn't the end of the world because the system wouldn't
work anyway, but because this system was for a customer it would be nice
to let the application give a proper message saying what happened and
then exit gracefully.

For this purpose I used a setjump right before accessing a board, and a
longjmp in the buserror signal handler. This way the application
wouldn't crash but exit gracefully.


Well, it obviously worked for you, but I think you'll find that calling
longjmp (or, indeed, any library function) in a signal handler invokes
undefined behaviour.

7.1.4 of the C99 Standard says:

4 The functions in the standard library are not guaranteed to
be reentrant and may modify objects with static storage
duration.[158]

Nov 14 '05 #13
Dan Pop wrote:
[...]
Another popular use is for error handling when the error was detected
deep in the function call chain. Instead of propagating the error back
through all the call chain, up to the function that is supposed to do
something about it, you get there with a single longjmp call.

Try to implement both examples without using longjmp and you'll see that,
although you can live without it, it's a lot more comfortable to live
with it.


Idly wondering ...

jmp_buf bailout;
time_t started_at;

int compare(const void *p, const void *q) {
if (difftime(time( NULL), started_at) > 30.0)
longjmp (bailout, 1);
...
}

...
started_at = time(NULL);
if (setjmp(bailout ) == 0)
qsort (array, count, sizeof array[0], compare);
else
fputs ("Sort was too slow: aborted\n", stderr);

Conforming? (I think so.) Leaking? (I wonder ...)

--
Er*********@sun .com
Nov 14 '05 #14
Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:
[...]
Another popular use is for error handling when the error was detected
deep in the function call chain. Instead of propagating the error back
through all the call chain, up to the function that is supposed to do
something about it, you get there with a single longjmp call.

Try to implement both examples without using longjmp and you'll see that,
although you can live without it, it's a lot more comfortable to live
with it.


Idly wondering ...

jmp_buf bailout;
time_t started_at;

int compare(const void *p, const void *q) {
if (difftime(time( NULL), started_at) > 30.0)
longjmp (bailout, 1);
...
}

...
started_at = time(NULL);
if (setjmp(bailout ) == 0)
qsort (array, count, sizeof array[0], compare);
else
fputs ("Sort was too slow: aborted\n", stderr);

Conforming? (I think so.) Leaking? (I wonder ...)


I don't see a problem with it as far as conformance is concerned.

Performance, however, is likely to be a problem. Unless your
compare() function's normal operation takes a very long time, the time
spent in qsort() is likely to be dominated by calls to time() and
difftime(). A sort that takes too long would likely have worked if
you hadn't tried to measure it.

If I were going to do something like this in real life, I'd probably
add a counter variable and do the time check every N comparisons, for
some suitable value of N.

For a toy example like this, of course, leaving out such bells and
whistles is perfectly appropriate.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #15

[comp.sources.d snipped; this seems very C-specific]

Keith Thompson wrote:

Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:
[...]
Another popular use is for error handling when the error was detected
deep in the function call chain. Instead of propagating the error back
through all the call chain, up to the function that is supposed to do
something about it, you get there with a single longjmp call.

Try to implement both examples without using longjmp and you'll see that,
although you can live without it, it's a lot more comfortable to live
with it.


Idly wondering ...

jmp_buf bailout;
time_t started_at;

int compare(const void *p, const void *q) {
if (difftime(time( NULL), started_at) > 30.0)
longjmp (bailout, 1);
...
}

...
started_at = time(NULL);
if (setjmp(bailout ) == 0)
qsort (array, count, sizeof array[0], compare);
else
fputs ("Sort was too slow: aborted\n", stderr);

Conforming? (I think so.) Leaking? (I wonder ...)


I don't see a problem with it as far as conformance is concerned.

Performance, however, is likely to be a problem. Unless your
compare() function's normal operation takes a very long time, the time
spent in qsort() is likely to be dominated by calls to time() and
difftime(). A sort that takes too long would likely have worked if
you hadn't tried to measure it.

If I were going to do something like this in real life, I'd probably
add a counter variable and do the time check every N comparisons, for
some suitable value of N.

For a toy example like this, of course, leaving out such bells and
whistles is perfectly appropriate.


Yes, it's a toy example: I was just trying to concoct some
semi-plausible reason to longjmp() from a callback function to
the original caller, without giving the intermediate function a
chance to clean itself up. For example, a qsort() implementation
might malloc() some memory or acquire other implementation-specific
resources, and if the comparator called longjmp() these would not
be released.

In my opinion, that's the chief drawback of longjmp(): it lets
you "blow past" intermediate levels in the call history without
knowing whether they need to free() memory, fclose() files, or
whatever. Two general approaches seem useful in this regard:

- Wrap up setjmp() and longjmp() with macros and functions
that enforce a more disciplined structure. In particular,
a function that needs to clean things up should have a way
to "intercept" the abnormal unwinding, do its cleaning up,
and then allow the unwind to proceed upwards through other
interceptors (possibly) to the ultimate catcher. You want
not only try/catch, but try/catch/finally.

- Use setjmp() and longjmp() as they are, but only among
functions that are "intimately connected." Perhaps they
should exist in the same source file, or at least share
the same design (c.f. recursive descent parsers). It's
important in this sort of usage to limit the "scope" of
the extraordinary control transfer.

Keep in mind the phenomenon of bit rot, and the problems of
large software edifices. As the code size grows it eventually
reaches the point where nobody, no matter how competent, is able
to understand all the subsystem-local conventions and practices.
Somebody, sometime, *will* change the behavior of some function
and introduce an apparently benign initialize-operate-cleanup
sequence without realizing that somebody else expects to be able
to longjmp() right past the whole shebang:

void do_something(vo id) {
first_thing();
second_thing();
third_thing();
}

in version 1.0 becomes

void do_something(vo id) {
DBHandle *db = connect_to_data base();
start_transacti on(db);
first_thing();
second_thing();
third_thing();
commit_transact ion(db);
close_database_ connection(db);
}

with the addition of the Sooper Dooper Data Snooper in version 3.0,
and if third_thing() decides to call longjmp() ...

It's a tool. It's a tool with no safety catches or blade
guards, because the compiler is usually unable to diagnose only
a very few of its possible misuses. Thus, IMHO, it's a tool to
be used only with great care, and at great need.

--
Er*********@sun .com
Nov 14 '05 #16
Eric Sosman wrote:

[...] because the compiler is usually unable to diagnose only
a very few of its possible misuses.


Um, er, right. I got myself caught between "able to diagnose
a few" and "unable to diagnose most," and landed somewhere in
the distributed middle. Or the distributed muddle. Sorry
about that, and does anyone have a mail editor that can diagnose
English nonsense?

--
Er*********@sun .com
Nov 14 '05 #17
Richard Heathfield wrote:
[Followups set to comp.lang.c]

Capstar wrote:
For this purpose I used a setjump right before accessing a board, and a
longjmp in the buserror signal handler. This way the application
wouldn't crash but exit gracefully.

Well, it obviously worked for you, but I think you'll find that calling
longjmp (or, indeed, any library function) in a signal handler invokes
undefined behaviour.

7.1.4 of the C99 Standard says:

4 The functions in the standard library are not guaranteed to
be reentrant and may modify objects with static storage
duration.[158]

.
.
.

[158] Thus, a signal handler cannot, in general, call standard library
functions.

(But note that the footnote, like all footnotes, is non-normative.)


But doesn't that mean that each function doesn't have to be reentrant?
So what I understand is that if I was busy calling longjump from my
program, and then I would get into my signal handler and call longjump
there again I invoke undefined behaviour. Please correct me if I'm wrong
here. But since the signal handler is the only place where I call
longjump, there is no problem with longjump being reentrant or not.

I do believe there are all other side effects of setjmp/longjmp, like
variables being undefined and possible memory-leaks, but thats why I
exit as soon as possible anyway.

Mark

--
<<Remove the del for email>>

Nov 14 '05 #18
In article <bv**********@n ews.tudelft.nl> ,
Capstar <sp***@eg.homei p.net> wrote:
But doesn't that mean that each function doesn't have to be reentrant?
So what I understand is that if I was busy calling longjump from my
program, and then I would get into my signal handler and call longjump
there again I invoke undefined behaviour. Please correct me if I'm wrong
here. But since the signal handler is the only place where I call
longjump, there is no problem with longjump being reentrant or not.

I do believe there are all other side effects of setjmp/longjmp, like
variables being undefined and possible memory-leaks, but thats why I
exit as soon as possible anyway.


The following sequence of events would be quite likely what is intended
to happen:

1. Something exceptional happens.
2. Operating system detects this and goes into "exception handling"
state.
3. OS asks running program whether it can handle the exception (note:
the running program wouldn't necessarily be written in C).
4. As _your_ program was written in C, some library code says that
handles signals sees that you have installed a signal handler. It calls
your signal handler.
5. Your signal handler executes, returns to the library code that called
it, which returns to the operating system.
6. The operating system goes out of "exception handling" state and
continues execution of your program.

If your signal handler calls longjmp, it will never return to the
library code and the operating system, so the OS will still be in
"exception handling" state. Basically, after the longjmp your signal
handler is still running, which could cause all kinds of problems.
Nov 14 '05 #19
In <40************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:
[...]
Another popular use is for error handling when the error was detected
deep in the function call chain. Instead of propagating the error back
through all the call chain, up to the function that is supposed to do
something about it, you get there with a single longjmp call.

Try to implement both examples without using longjmp and you'll see that,
although you can live without it, it's a lot more comfortable to live
with it.


Idly wondering ...

jmp_buf bailout;
time_t started_at;

int compare(const void *p, const void *q) {
if (difftime(time( NULL), started_at) > 30.0)
longjmp (bailout, 1);
...
}

...
started_at = time(NULL);
if (setjmp(bailout ) == 0)
qsort (array, count, sizeof array[0], compare);
else
fputs ("Sort was too slow: aborted\n", stderr);

Conforming? (I think so.) Leaking? (I wonder ...)


By your logic, malloc is evil, too, because it can cause memory leaks *if
misused*.

As usual, there is no substitute for knowing what you're doing and
longjmp is not the right thing when malloc and/or fopen (may) have been
called (without) the corresponding free/fclose calls between the
invocation of setjmp and the longjmp call.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2296
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
12
3330
by: Michael B Allen | last post by:
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of code? Or is it frowned apon like goto? I have a need but I don't want to use something that is costly, isn't supported consistenly, or something that might pull in exotic text segments, etc. Specifically I have a hairly algorithm loop that uses what is currently a macro V. Here's a snipplet: for (k = d; k >= -d; k -= 2) { if (k == -d || (k != d && V(fwd, m, k - 1)...
2
2126
by: Ravi Uday | last post by:
Hi, Can anyone explain me why do we use setjmp and longjump functions. I read through the manual pages/doc but wasnt able to get a clear picture of the same. Any small example illustrating these would be real helping. Also, what happens when one jumps from one process/task to other, even though it is OT can anybody clear this concept.
4
2127
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C compiler. I understand the general principle behind setjmp/longjmp, but I am somewhat lost in the detail of how to actually implement this in assembler. (My compiler supports in-line assembler which I hope will prove usefull). I will be very gratefull for any help or pointers with this problem. I am...
2
1414
by: Jerald Fijerald | last post by:
Hello. I'm trying to find an elegant way to longjmp to the callee (aka "generators for C") So far I've come to this experimental program (gcc only as it uses __builtin_frame_address, although we can do without it): ====================================================================== /* longjmp to the callee */
8
2424
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
6
3229
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for automatic non volatile variables of the function invoking setjmp, these will be undefined if modified after the setjmp call"
0
1280
by: sh.vipin | last post by:
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...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10247
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.