473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

longjump

Hi ! I just do not understand how longjmp works.. I read this docs :
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

but I just don't see why the variable value, which the address is not
passed in any argument, changed it's value...

I understood the environnement thing, I think that means all the variables
are saved and then restored using this environnement, but I just don't see
where 'value' changed it's value...
I know using jumps are not a good idea, but I want to have a in-depth
knowledge of the C langage, and all it's obscure corners

Thanks !
Nov 13 '05 #1
14 5035
In article <W2********************@wagner.videotron.net>, Eric wrote:
Hi ! I just do not understand how longjmp works.. I read this docs :
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

but I just don't see why the variable value, which the address is not
passed in any argument, changed it's value...
I think you should leave "addresses" out of this. There are no
pointer in this example.
I understood the environnement thing, I think that means all the variables
are saved and then restored using this environnement, but I just don't see
where 'value' changed it's value...


The variable 'value' changed its value on the line
"value=setjmp(environment_buffer);" in main(), where you landed
after the longjmp() in some_function(). It gets the value 5
since that's the return value of setjmp() that you specified in
the longjmp() call.
--
Andreas Kähäri
Nov 13 '05 #2
Eric wrote:
Hi ! I just do not understand how longjmp works.. I read this docs :
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

but I just don't see why the variable value, which the address is not
passed in any argument, changed it's value...

I understood the environnement thing, I think that means all the variables
are saved and then restored using this environnement,
No, it does not mean that at all. Variables are not saved or restored by
setjmp or longjmp.

7.13.2.1 The longjmp function

[#3] All accessible objects have values as of the time
longjmp was called, except that the values of objects of
automatic storage duration that are local to the function
containing the invocation of the corresponding setjmp macro
that do not have volatile-qualified type and have been
changed between the setjmp invocation and longjmp call are
indeterminate.
but I just don't see
where 'value' changed it's value...
I know using jumps are not a good idea, but I want to have a in-depth
knowledge of the C langage, and all it's obscure corners


The example isn't even legal:

7.13.1.1 The setjmp macro

[#4] An invocation of the setjmp macro shall appear only in
one of the following contexts:

-- the entire controlling expression of a selection or
iteration statement;

-- one operand of a relational or equality operator with
the other operand an integer constant expression, with
the resulting expression being the entire controlling
expression of a selection or iteration statement;

-- the operand of a unary ! operator with the resulting
expression being the entire controlling expression of a
selection or iteration statement; or

-- the entire expression of an expression statement
(possibly cast to void).

Note that this does not allow a setjmp invocation to appear as the right
side of an assignment expression.

But aside from that, the point is that setjmp may return more than once.
The initial call returns 0. Later, when you invoke longjmp, the effect
is to cause the program to continue executing from the setjmp call
again, but this time with a different return value. So the return value
from setjmp tells you whether it is returning from a direct invocation
or from a later longjmp invocation.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #3


ok thanks everyone !

-Eric

"Kevin Goodsell" <us*********************@neverbox.com> a écrit dans le
message de news:4T*******************@newsread2.news.pas.eart hlink.net...
Eric wrote:
Hi ! I just do not understand how longjmp works.. I read this docs :
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

but I just don't see why the variable value, which the address is not
passed in any argument, changed it's value...

I understood the environnement thing, I think that means all the variables are saved and then restored using this environnement,


No, it does not mean that at all. Variables are not saved or restored by
setjmp or longjmp.

7.13.2.1 The longjmp function

[#3] All accessible objects have values as of the time
longjmp was called, except that the values of objects of
automatic storage duration that are local to the function
containing the invocation of the corresponding setjmp macro
that do not have volatile-qualified type and have been
changed between the setjmp invocation and longjmp call are
indeterminate.
but I just don't see
where 'value' changed it's value...
I know using jumps are not a good idea, but I want to have a in-depth
knowledge of the C langage, and all it's obscure corners


The example isn't even legal:

7.13.1.1 The setjmp macro

[#4] An invocation of the setjmp macro shall appear only in
one of the following contexts:

-- the entire controlling expression of a selection or
iteration statement;

-- one operand of a relational or equality operator with
the other operand an integer constant expression, with
the resulting expression being the entire controlling
expression of a selection or iteration statement;

-- the operand of a unary ! operator with the resulting
expression being the entire controlling expression of a
selection or iteration statement; or

-- the entire expression of an expression statement
(possibly cast to void).

Note that this does not allow a setjmp invocation to appear as the right
side of an assignment expression.

But aside from that, the point is that setjmp may return more than once.
The initial call returns 0. Later, when you invoke longjmp, the effect
is to cause the program to continue executing from the setjmp call
again, but this time with a different return value. So the return value
from setjmp tells you whether it is returning from a direct invocation
or from a later longjmp invocation.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #4
In article <4T*******************@newsread2.news.pas.earthlin k.net>, Kevin Goodsell wrote:
Eric wrote: [cut]
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

[cut] The example isn't even legal: [cut] Note that this does not allow a setjmp invocation to appear as the right
side of an assignment expression.


So you're not allowed to store the return value of setjmp() in
any way?

--
Andreas Kähäri
Nov 13 '05 #5
Andreas Kahari <ak*******@freeshell.org> wrote:
In article <4T*******************@newsread2.news.pas.earthlin k.net>, Kevin Goodsell wrote:
Note that this does not allow a setjmp invocation to appear as the right
side of an assignment expression.


So you're not allowed to store the return value of setjmp() in
any way?


Alas, no. Might've been useful in some cases, but no.

Then again, anything which makes setjmp()/longjmp() less likely to be
used (except excising them) is probably a Good Thing.

Richard
Nov 13 '05 #6
In article <sl**********************@norge.freeshell.org>,
Andreas Kahari <ak*******@freeshell.org> wrote:
In article <4T*******************@newsread2.news.pas.earthlin k.net>, Kevin
Goodsell wrote:
Eric wrote:

[cut]
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

[cut]
The example isn't even legal:

[cut]
Note that this does not allow a setjmp invocation to appear as the right
side of an assignment expression.


So you're not allowed to store the return value of setjmp() in
any way?

No. It is very limited what you are allowed to do: If x is an integer
constant expression, then you can use the following types of expressions:

setjmp (env)
! setjmp (env)
setjmp (env) == x
setjmp (env) != x
setjmp (env) >= x
setjmp (env) <= x
setjmp (env) > x
setjmp (env) < x
x == setjmp (env)
x != setjmp (env)
x >= setjmp (env)
x <= setjmp (env)
x > setjmp (env)
x < setjmp (env)

And you can use these as the _complete_ controlling expression of an if,
if/else, switch, for, while, do/while statement. Everything else invokes
undefined behavior.

Basically, setjmp has to store the processor status and longjmp has to
restore it so that execution resumes just after the setjmp call as if
nothing had happened. This can be simple on some architecture, but
difficult on another one. The C Standard must make sure that everything
can be implemented on any reasonable machine, so a compiler for an
architecture where setjmp is difficult to implement only needs to
produce correct code for this very small number of cases.

It is quite possible that with your compiler, much more complicated uses
of setjmp will work, but it wouldn't be portable. It is also possible
that on very common architectures certain complicated uses of setjmp
would be extremly difficult to implement. I can't think of such a case
at the moment, but setjmp/longjmp is tricky and it is quite possible
that there are such cases. If that were true, then the C Standard would
most likely make these complicated cases undefined behavior. But that
would be very complicated to do in the C Standard, so this simple set of
rules is better.

(What is quite interesting is that even very simple cases like
setjmp (env); /* Not inside a selection or iteration statement */
if (1 && setjmp (env)) { ... }
if (! (setjmp (env) == 0)) { ... }
invoke undefined behavior. )
Nov 13 '05 #7
On Fri, 28 Nov 2003, Christian Bau wrote:
(What is quite interesting is that even very simple cases like
setjmp (env); /* Not inside a selection or iteration statement */
if (1 && setjmp (env)) { ... }
if (! (setjmp (env) == 0)) { ... }
invoke undefined behavior. )


No, the first one is correct. See 7.6.1.1, "Environmental constraint."

--
au***@axis.com
Nov 13 '05 #8
On Fri, 28 Nov 2003, Richard Bos wrote:
Andreas Kahari <ak*******@freeshell.org> wrote:
So you're not allowed to store the return value of setjmp() in
any way?


Alas, no. Might've been useful in some cases, but no.


Well, if the set of possible return values is fairly small, you could use
a switch statement:

int err;

switch (setjmp(env)) {
case 0:
err = 0;
break;
case 1:
err = 1;
break;
case 2:
err = 2;
break;
case 3:
err = 3;
break;
}

--
au***@axis.com
Nov 13 '05 #9
Johan Aurer <au***@axis.com> wrote:
On Fri, 28 Nov 2003, Christian Bau wrote:
(What is quite interesting is that even very simple cases like
setjmp (env); /* Not inside a selection or iteration statement */
if (1 && setjmp (env)) { ... }
if (! (setjmp (env) == 0)) { ... }
invoke undefined behavior. )


No, the first one is correct. See 7.6.1.1, "Environmental constraint."


Since I'm unable to locate a section numbered 7.6.1.1 in either C89 or
C99, and the example exceeds the environmental limits imposed by C99
7.13.1.1#4, could you please elaborate on this?

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10
Christian Bau wrote:
(What is quite interesting is that even very simple cases like
setjmp (env); /* Not inside a selection or iteration statement */ [...] invoke undefined behavior. )


That's "the entire expression of an expression statement", which is
one of the valid contexts for a setjmp invocation.

Jeremy.
Nov 13 '05 #11
In <sl**********************@norge.freeshell.org> Andreas Kahari <ak*******@freeshell.org> writes:
In article <4T*******************@newsread2.news.pas.earthlin k.net>, Kevin Goodsell wrote:
Eric wrote:

[cut]
http://www.acm.uiuc.edu/webmonkeys/b...guide/2.8.html

[cut]
The example isn't even legal:

[cut]
Note that this does not allow a setjmp invocation to appear as the right
side of an assignment expression.


So you're not allowed to store the return value of setjmp() in
any way?


Right. The restrictions imposed by 7.13.1.1p4 may seem strange,
but they were carefully written to allow implementing the setjmp
macro on all the platforms in current use back when C89 was drafted.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Johan Aurer <au***@axis.com> wrote:
On Fri, 28 Nov 2003, Christian Bau wrote:
(What is quite interesting is that even very simple cases like
setjmp (env); /* Not inside a selection or iteration statement */
if (1 && setjmp (env)) { ... }
if (! (setjmp (env) == 0)) { ... }
invoke undefined behavior. )


No, the first one is correct. See 7.6.1.1, "Environmental constraint."


Since I'm unable to locate a section numbered 7.6.1.1 in either C89 or
C99, and the example exceeds the environmental limits imposed by C99
7.13.1.1#4, could you please elaborate on this?


Sorry, forget about what I've written. I misread Christians comment
in the first example, you are right, Johann. My apologies for
messing up. (However, there's still no section 7.6.1.1, AFAICT).

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #13
In <an********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
Johan Aurer <au***@axis.com> wrote:
On Fri, 28 Nov 2003, Christian Bau wrote:
> (What is quite interesting is that even very simple cases like
> setjmp (env); /* Not inside a selection or iteration statement */
> if (1 && setjmp (env)) { ... }
> if (! (setjmp (env) == 0)) { ... }
> invoke undefined behavior. )


No, the first one is correct. See 7.6.1.1, "Environmental constraint."


Since I'm unable to locate a section numbered 7.6.1.1 in either C89 or
C99, and the example exceeds the environmental limits imposed by C99
7.13.1.1#4, could you please elaborate on this?


It's the right C90 section number for the setjmp macro.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14
Da*****@cern.ch (Dan Pop) wrote:
Irrwahn Grausewitz <ir*******@freenet.de> writes: <snip>
Since I'm unable to locate a section numbered 7.6.1.1 in either C89 or
C99,

<snipped more nonsense I wrote>
It's the right C90 section number for the setjmp macro.


Ahh, Ok, I see. Unfortunately I only own a draft of C89 and copies
of C99 and C99:TC1.

Thanks & Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #15

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

Similar topics

9
by: Jon Perez | last post by:
I have a C extension function into which I pass a list of lists of tuples: , , , ] I then unpack the values (down to the tuple elements) into their C values using:
10
by: Bill Davidson | last post by:
Hi there, Please forgive me for posting this article on multiple groups. Being new in the newsgroups, I was not sure which group would have been appropriate for my question. Sorry. My...
3
by: Marc Schellens | last post by:
I would liketo handle a floating point exception in my program. As I learned I can do a longjump from the signal handler, but: 1. Will local objects be cleaned up properly? 2. Lets say I...
22
by: Nimmi Srivastav | last post by:
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...
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
13
by: Speed | last post by:
Hi, I was wondering if there is any way to catch exceptions without knowing in advance what errors may occur. What I mean to say is that is it possible to use try {} on a bunch of lines and...
5
by: manjuscripts | last post by:
how can I handle exceptions in C?
6
by: pal | last post by:
hi all, Can anybody explain about setjump and longjump functions if u have time to spend on this. Thanks, pal
10
by: pereges | last post by:
How to to go about this ? Suppose a malloc inside a recursive function has failed and you want to set the error flag and return it to the calling function(the one which called the recursive...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.