473,378 Members | 1,207 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,378 software developers and data experts.

problem with setjmp

hi all,

Consider the following C code:


void funct(){
}
int main(){
int x;
x=funct;
printf("%x\n",x);
return 0;
}

It prints the address of the function funct().It means that
the function funct() is in the process stack even before it is
called.So,it should be safe to call setjmp in the function
funct() in the following way:
jmp_buf env;

int funct(){

if(setjmp(env)==1)
printf("called by longjmp\n");
return 0;
}

int main(void){

funct();
longjmp(env,1);
return 0;
}

On execution,the above program crashes.Can anybody please tell me
that what is the problem.

Thanks.
Nov 14 '05 #1
5 1853

"candy" <ca********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
hi all,

Consider the following C code:


void funct(){
}
int main(){
int x;
x=funct;
printf("%x\n",x);
return 0;
}

It prints the address of the function funct().It means that
the function funct() is in the process stack even before it is
called.So,it should be safe to call setjmp in the function
funct() in the following way:
jmp_buf env;

int funct(){

if(setjmp(env)==1)
printf("called by longjmp\n");
return 0;
}

int main(void){

funct();
longjmp(env,1);
return 0;
}

On execution,the above program crashes.Can anybody please tell me
that what is the problem.

Thanks.


K&R2 table 5-1 addresses one of your problems, I believe. MPJ
Nov 14 '05 #2
On 28 Nov 2004 08:37:01 -0800, ca********@yahoo.com (candy) wrote in
comp.lang.c:
hi all,

Consider the following C code:


void funct(){
}
int main(){
int x;
x=funct;
This is both a constraint violation and produces undefined behavior.
If you have a compiler that accepts this, either it is actually not a
C compiler or it is not being invoked as a conforming C compiler.
printf("%x\n",x);
More undefined behavior here, the "%x" conversion specifier to the
printf() family of functions requires an unsigned int parameter, not a
signed int.
return 0;
}

It prints the address of the function funct().It means that
Whatever it does or does not do is not specified by the C language due
to the undefined behavior.
the function funct() is in the process stack even before it is
What exactly do you think a "process stack" is? I have never heard of
the term before.
called.So,it should be safe to call setjmp in the function
funct() in the following way:
Why do you think it is safe? This is not how the setjmp() and
longjmp() functions are defined to work.
jmp_buf env;

int funct(){

if(setjmp(env)==1)
printf("called by longjmp\n");
return 0;
}

int main(void){

funct();
longjmp(env,1);
return 0;
}

On execution,the above program crashes.Can anybody please tell me
that what is the problem.
The problem is that you need to get a good book or online reference on
C and read how setjmp() and longjmp() work.

The function performing a longjmp() must be called directly or
indirectly by the function that performed the setjmp() on the buffer.
setjmp/longjmp only works in one direction in a function call tree.
You are trying to go in the opposite direction, and it does not and
will not ever work that way.
Thanks.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
In article <f4**************************@posting.google.com >,
candy <ca********@yahoo.com> wrote:
Consider the following C code:

void funct(){
}

int main(){
int x;
x=funct;
printf("%x\n",x);
return 0;
}

It prints the address of the function funct().
No; it does not even compile:

% strictcc t.c
t.c: In function `main':
t.c:6: error: assignment makes integer from pointer without a cast
t.c:7: error: implicit declaration of function `printf'
%
[no executable produced]

On the IA64, for instance, funct has a 64-bit address, but x is
only 32 bits long. What happens when you put 64 bits into a 32-bit
holder?
It means that the function funct() is in the process stack even
before it is called.
No. Even where there is a "process stack", and even where function
addresses happen to fit in "int"s (e.g., IA32), there is no activation
record for funct() on that stack at that point.
So,it should be safe to call setjmp in the function funct() in the
following way:

jmp_buf env;

int funct(){

if(setjmp(env)==1)
printf("called by longjmp\n");
return 0;
}

int main(void){

funct();
longjmp(env,1);
return 0;
}

On execution,the above program crashes.Can anybody please tell me
that what is the problem.


The call to longjmp() attempts to jump into a function activation
that has already returned. This is not allowed in Standard C; the
effect is undefined. You are lucky that it crashed, rather than
doing some sort of by pretending to work.

(Some people sometimes attempt to [ab]use longjmp() to implement
thread context switchers. Whether this works is up to the
implementation: some implementations actually unwind the stack
inside longjmp(), looking for the corresponding setjmp(), and since
different threads have separate stacks, the unwound stack eventually
reaches its top and the program simply aborts or crashes. In
general, you are better off writing your thread-context-switch code
in machine-specific assembly-language -- you usually need some
anyway to save registers, for instance.)
--
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 #4
Jack Klein <ja*******@spamcop.net> wrote in message news:<0r********************************@4ax.com>. ..
On 28 Nov 2004 08:37:01 -0800, ca********@yahoo.com (candy) wrote in
comp.lang.c:
The problem is that you need to get a good book or online reference on
C and read how setjmp() and longjmp() work.

Ok,can you please tell me such a good link or an online reference where I can
read about setjmp.K&R has a very brief introduction of setjmp and longjmp
and the same is the case with the man pages.

Thanks in advance,
Alice
Nov 14 '05 #5
In <41**************************@posting.google.com > al***********@yahoo.com (alice) writes:
Jack Klein <ja*******@spamcop.net> wrote in message news:<0r********************************@4ax.com>. ..
On 28 Nov 2004 08:37:01 -0800, ca********@yahoo.com (candy) wrote in
comp.lang.c:
The problem is that you need to get a good book or online reference on
C and read how setjmp() and longjmp() work.

Ok,can you please tell me such a good link or an online reference where I can
read about setjmp.K&R has a very brief introduction of setjmp and longjmp
and the same is the case with the man pages.


The description from K&R2 might be short (less than half a page), but it
tells you everything you need to know. Pay attention to each and every
detail, even reputable authors like H&S have gotten one thing wrong in
their book.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6

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

Similar topics

6
by: someone | last post by:
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...
18
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...
20
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...
2
by: JS | last post by:
I have this struct: struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stak; };
8
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
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
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....
4
by: Richard Jones | last post by:
Currently ctypes can't play well with any C code that requires use of setjmp as part of its API. libpng is one of those libraries. Can anyone think of a reasonable solution to this? Perhaps...
5
by: Spiros Bousbouras | last post by:
In the following assume that a is int and env is of type jmp_buf a = setjmp(env) ; Is it legal ? It looks reasonable but it seems to violate what is mentioned under "Environmental limits" of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.