473,786 Members | 2,366 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 #1
22 6068
Nimmi Srivastav wrote:
Can someone kindly clarify the distinction between long jumps and
gotos?
The goto construct is a good way to make it hard to read a function, whereas
the longjmp function is a good way to make it hard to read an entire
program.
Why is one frowned upon and not the other?
Because you weren't looking whilst I was frowning at the other.
Is there really
any situation where use of longjmp becomes inevitable?
Not on my watch.
A real-life code snippet would surely help.


Help what? I already /have/ a headache.
All right, let's try to answer this intelligently. Firstly, the basic
difference between them is that longjmp lets you hop around between
functions - or rather, setjmp lets you say "HERE!" and longjmp then
teleports you there later on.

In my experience, it is generally possible, and wise, to manage without
either. Both of them *can* be used wisely, I suppose, but I find it
remarkably easy to do without them.

No doubt there will be some people here who feel differently about it.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #2
Nimmi Srivastav wrote:
Can someone kindly clarify the distinction
between long jumps and gotos?
They really are *not* comparable.
Why is one frowned upon and not the other?
the setjmp/longjpm functions are *not* for new or amateur programmers.
Is there really any situation
where use of longjmp becomes inevitable?
The setjmp/longjmp mechanism
is the way that C programmers "throw" exceptions.
Unfortunately, it does *not* clean-up after you.
Memory allocated from free storage after setjmp
will not be free'd.
A real-life code snippet would surely help.


Take a look at the man page:

http://www.rahul.net/cgi-bin/userbin...gjmp&section=3

The following code fragment indicates the flow of control of
the setjmp() and longjmp() combination:

/function declaration/
...
jmp_buf my_environment;
...
if (setjmp(my_envi ronment)) {
/* register variables have unpredictable values */
code after the return from longjmp
...
} else {
/* do not modify register vars in this leg of code */
this is the return from setjmp
...
}

Nov 14 '05 #3
ni************* @yahoo.com (Nimmi Srivastav) wrote:
# 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.

(1) You can't goto from one function to another. You can longjmp from a
function invocation anywhere upward in the protocol stack.

(2) ANS C doesn't have label variables, but jmp_bufs are variables.

In my recursive ascent parser, error recovery abandons the current parse
before recoverying and restarts from the initial state (more or less).
Rather than have initial function in the parser check on return if a
parse error occurs, I just longjmp from the error detection to the error
recovery.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I'm not even supposed to be here today.
Nov 14 '05 #4
In article <8b************ *************@p osting.google.c om>,
ni************* @yahoo.com (Nimmi Srivastav) wrote:
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.


The answer is very simple: If you have to ask what is the difference
between goto and longjmp, then you shouldn't use either of them.
Nov 14 '05 #5
"Christian Bau" <ch***********@ cbau.freeserve. co.uk> wrote in message
news:ch******** *************** **********@slb-newsm1.svr.pol. co.uk...
In article <8b************ *************@p osting.google.c om>,
ni************* @yahoo.com (Nimmi Srivastav) wrote:
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.


The answer is very simple: If you have to ask what is the difference
between goto and longjmp, then you shouldn't use either of them.


He does not necessarily want to *use* them. He wants
to *understand* them. Big difference.

Knowledge is power.
Nov 14 '05 #6
On Fri, 30 Jan 2004 14:38:21 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:

The setjmp/longjmp mechanism
is the way that C programmers "throw" exceptions.
Unfortunatel y, it does *not* clean-up after you.
Memory allocated from free storage after setjmp
will not be free'd.


Just to be clear, even C++ exception handling doesn't automagically
free up memory allocated from the _free store_ (using new/malloc/etc)
unless some type of smart pointer is inolved. Exception handling only
guarantees that destructors are run for automatic objects that need
them.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #7
Richard Heathfield wrote:
Nimmi Srivastav wrote:

In my experience, it is generally possible, and wise, to manage without
either. Both of them *can* be used wisely, I suppose, but I find it
remarkably easy to do without them.
I have used it once. And I didn't know any better way of getting past
this. This was the problem. I had an application running on a VME
system, on which the host needed to communicate to about 8 VME-boards.
The addresses of these boards are set with jumpers, and the hosts has a
configuration file saying which board is at which address.
But when there was a mismatch in the confiuration file and the jumper
settings, the application would get a buserror, which causes it to crash.

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.

But this is indeed for now the only application I used it in.

Just my two cents.

Mark

No doubt there will be some people here who feel differently about it.

--
<<Remove the del for email>>

Nov 14 '05 #8
ni************* @yahoo.com (Nimmi Srivastav) wrote:
Can someone kindly clarify the distinction between long jumps and
gotos? Why is one frowned upon and not the other?
Both are frowned upon, but beginner programmers aren't likely to abuse
long jumps.
Is there really any situation where use of longjmp becomes inevitable?


I've only encountered one such situation: a dinky-toy compiler made a
long jump necessary.

Richard
Nov 14 '05 #9
ni************* @yahoo.com (Nimmi Srivastav) writes:
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.


A C++ to C translator usually implements exception handling using
[sig]longjmp(). You might need longjmp() to test if some hardware
is there or capable of doing certain things. An example where I
have used longjmp() was to test whether I'm running inside of
VMware or not (it's done by using an instruction that would
raise a SIGSEGV on a native x86 but not on VMware, and you need
the longjmp() to recover from the segmentation violation state).

You can usually "hide" a goto by using break or continue (in
switch or for/while) or simply create an new function and use
multiple return statements (I'm using this when there are a
lot of error checks), this is often easier to understand than
having to work with flags (depends on the application)...
Markus
Nov 14 '05 #10

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
2124
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
1413
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
1279
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
9647
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
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
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
9959
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
6744
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
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.