Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   problem with setjmp (http://www.velocityreviews.com/forums/t435979-problem-with-setjmp.html)

candy 11-28-2004 04:37 PM

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.

Merrill & Michele 11-28-2004 06:01 PM

Re: problem with setjmp
 

"candy" <candy_init@yahoo.com> wrote in message
news:f47acabd.0411280837.3427d381@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



Jack Klein 11-28-2004 06:11 PM

Re: problem with setjmp
 
On 28 Nov 2004 08:37:01 -0800, candy_init@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

Chris Torek 11-28-2004 06:21 PM

Re: problem with setjmp
 
In article <f47acabd.0411280837.3427d381@posting.google.com >,
candy <candy_init@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.

alice 11-30-2004 03:09 PM

Re: problem with setjmp
 
Jack Klein <jackklein@spamcop.net> wrote in message news:<0r4kq0hfs5toui6g9mhulco7efn97r49ca@4ax.com>. ..
> On 28 Nov 2004 08:37:01 -0800, candy_init@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

Dan Pop 12-01-2004 02:17 PM

Re: problem with setjmp
 
In <41292dbb.0411300709.7ad51157@posting.google.com > alice_vas2001@yahoo.com (alice) writes:

>Jack Klein <jackklein@spamcop.net> wrote in message news:<0r4kq0hfs5toui6g9mhulco7efn97r49ca@4ax.com>. ..
>> On 28 Nov 2004 08:37:01 -0800, candy_init@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: Dan.Pop@ifh.de
Currently looking for a job in the European Union


All times are GMT. The time now is 09:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.