473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help: why setjmp/longjmp take STRUCT jmp_buf as parameter, not a pointer?

I have *thought* that setjmp/longjmp() should take a pointer to
jmp_buf. And the calling function should hold the actual struct data.
But ... I trid on both Win32 and Linux, it seems that
setjmp/longjmp() are taking stuct:

c:\> dmc sj.c (Digital Mars Compiler Version 8.38n)
link sj,,,user32+ker nel32/noi;
c:\> sj.exe
sizeof(jmp_buf) = 64
----------------------------------------------------------------- sj.c
-----
#include <setjmp.h>
#include <stdio.h>

int main()
{
printf("sizeof( jmp_buf) = %d\n", sizeof(jmp_buf) );
return 0;
}
----------------------------------------------------------------- sj.c
-----

And on Linux:

[d] ./sj
sizeof(jmp_buf) = 156

I'm totally confused. The jmp_buf is used to store the program state
info in setjmp(), and the info will be used again when longjmp is
called.

However in C, struct is pass-by-copy; so the calling function's
jmp_buf will not be modified, because setjmp() can only modify it's
own copy of jmp_buf. Then where is the info stored, and how longjmp()
can use the saved info to jump back?

What's the magic behind, or I miss something?

Can you try my example? and can you give an explanation?

Thanks.
Nov 14 '05 #1
6 4840
someone wrote:

What's the magic behind, or I miss something?

It is simple as that. It is implementation-dependent. And I dont
think the C standard defines this.
For example, for me on Win32 i got it to be 64 ( I used MS VC++ IDE
,cl compiler )
On Solaris, I got 48 ( gcc ).

If you can add more info. about this, it would be helpful.

--
Karthik
Humans please 'removeme_' for my real email.
Nov 14 '05 #2

"someone" <so******@yahoo .com> a écrit dans le message de
news:eb******** *************** ***@posting.goo gle.com...

Hi,
I have *thought* that setjmp/longjmp() should take a pointer to
jmp_buf. And the calling function should hold the actual struct data.
But ... I trid on both Win32 and Linux, it seems that
setjmp/longjmp() are taking stuct:
setjmp/longjmp() are taking an argument of type jmp_buf, not a structure or
pointer to structure.

[snipped]

I'm totally confused. The jmp_buf is used to store the program state
info in setjmp(), and the info will be used again when longjmp is
called.

However in C, struct is pass-by-copy; so the calling function's
jmp_buf will not be modified, because setjmp() can only modify it's
own copy of jmp_buf. Then where is the info stored, and how longjmp()
can use the saved info to jump back?

What's the magic behind, or I miss something?


There is no magic behind, jmp_buf is in fact an array type.

Regis
Nov 14 '05 #3
"someone" <so******@yahoo .com> wrote in message
news:eb******** *************** ***@posting.goo gle.com...
I have *thought* that setjmp/longjmp() should take a pointer to
jmp_buf. And the calling function should hold the actual struct data.
But ... I trid on both Win32 and Linux, it seems that
setjmp/longjmp() are taking stuct:

c:\> dmc sj.c (Digital Mars Compiler Version 8.38n)
link sj,,,user32+ker nel32/noi;
c:\> sj.exe
sizeof(jmp_buf) = 64
....
I'm totally confused. The jmp_buf is used to store the program state
info in setjmp(), and the info will be used again when longjmp is
called.

However in C, struct is pass-by-copy; so the calling function's
jmp_buf will not be modified, because setjmp() can only modify it's
own copy of jmp_buf. Then where is the info stored, and how longjmp()
can use the saved info to jump back?

What's the magic behind, or I miss something?


jmp_buf is an array, not a struct.

--
Peter
Nov 14 '05 #4
In article <news:40******@ news.rivernet.c om.au>
Peter Nilsson <ai***@acay.com .au> writes:
jmp_buf is an array, not a struct.


Indeed. It may, of course, be a type-alias for an array of one
element, whose element-type is a structure type. This is the case
for every setjmp() implementation I have written (which is not
very many; mostly I have to work within someone else's initial
implementation) .

In other words:

typedef struct __jmp_buf jmp_buf[1];
struct __jmp_buf { ... contents ... };

is a valid implementation, but changing the first line to:

typedef struct __jmp_buf jmp_buf;

is not, even if the implementation happens to pass structures by
reference internally. Why? Becuase the C Standards say so:

7.10 Nonlocal jumps <setjmp.h>

[#1] The header <setjmp.h> defines the macro setjmp, and
declares one function and one type, for bypassing the normal
function call and return discipline.191

[#2] The type declared is

jmp_buf

which is an array type suitable for holding the information
needed to restore a calling environment. ...

The C standard says "this is an array type" so it had better be an
array type. An implementation can cheat if and only if the effect
of cheating is invisible, and array types have visible characteristics .
For instance, "sizeof instance" is usually different from "sizeof
(instance + 0)", and applying "+ 0" to an array object is always
valid:

#include <setjmp.h>
#include <stdio.h>

int main(void) {
jmp_buf x;
printf("sizes: %lu %lu\n", (unsigned long)sizeof x,
(unsigned long) sizeof(x + 0));
return 0;
}

This *must* compile and will virtually always print two different
numbers (in rare cases you might actually get "8 8" today). The
array object "x" has as its size the number of bytes in the entire
array, while adding zero causes the array object to undergo the
transition prescribed by The Rule about arrays and pointers in C,
so that sizeof (x + 0) is the number of bytes in a pointer to the
first element of the array. The second number is usually 4 or 8
today, while the first is often at least 12 and almost always at
least 8 (to hold stack and frame pointers, plus another 4 or more
for signal information in POSIX systems).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
so******@yahoo. com (someone) wrote:
# I have *thought* that setjmp/longjmp() should take a pointer to
# jmp_buf. And the calling function should hold the actual struct data.
# But ... I trid on both Win32 and Linux, it seems that
# setjmp/longjmp() are taking stuct:

setjmp/longjmp are magic, like stdargs. It might make sense as C code, it
might not: the source code that is there might be intecepted and handled
differently by the compiler.

It's like an eggroll. You eat it whole and don't worry about the individual
ingredients.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Nov 14 '05 #6
so******@yahoo. com (someone) wrote:
# I have *thought* that setjmp/longjmp() should take a pointer to
# jmp_buf. And the calling function should hold the actual struct data.
# But ... I trid on both Win32 and Linux, it seems that
# setjmp/longjmp() are taking stuct:

setjmp/longjmp are magic, like stdargs. It might make sense as C code, it
might not: the source code that is there might be intecepted and handled
differently by the compiler.

It's like an eggroll. You eat it whole and don't worry about the individual
ingredients.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Nov 14 '05 #7

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

Similar topics

18
2431
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in compatibility problems. The contents of the jmp_buf buffer are specific
20
2307
by: JS | last post by:
When setjmp is called how can the if statement evaluate to true or false when setjmp only returns 0 or non-zero? struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stack; };
2
1660
by: JS | last post by:
I have this struct: struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stak; };
8
2407
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) {
4
7558
by: Sreekanth | last post by:
Hi All I am trying to write a code in which I have to access an array of jmp_buf so i have declared it as jmp_buf mybuf Now when i am doing a longjmp like
15
2419
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers. They (my co-workers) say (with vehement ardor ;) ) that the usage of setjmp() emplyoyed in function"C" that was called from function "B" that was...
3
1892
by: no_click_there | last post by:
Hi, I'm learning to use the setjmp/longjmp functions and still can't really grasp what's going on here. I'd basically like to be able to jump back and forth from (or to) two given functions. Let's consider the following toy-example (yes, the example is stupid but never mind...) BEGIN CODE =====================
0
1272
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)...
0
8202
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. ...
0
8338
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...
1
7959
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...
0
8216
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...
0
6614
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...
0
5390
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...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1180
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...

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.