By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

problem with setjmp

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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 (4039.22'N, 11150.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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.