Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using setjmp

Reply
Thread Tools

using setjmp

 
 
JS
Guest
Posts: n/a
 
      03-23-2005
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[1024];
};


struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));


if(setjmp(pcb_pointer->state)) {
current->start_routine(current->arg);
printf("Thread returned\n");
exit(0);
}


 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      03-23-2005
In article <d1sjvi$5db$>, JS <dsa.@asdf.com> wrote:
>When setjmp is called how can the if statement evaluate to true or false
>when setjmp only returns 0 or non-zero?


Have a coffee, go outside, and sit under a tree (or, if the weather
fails to be cooperative, at least get away from the computer), and
contemplate the difference between true/false and nonzero/zero, and you
will be enlightened.


dave

--
Dave Vandervies
>The Kremlin has a mother these days?

After a fflush(stdin), it might end up having three mothers.
--Joona I Palaste and Gordon Burditt in comp.lang.c
 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      03-23-2005
>When setjmp is called how can the if statement evaluate to true or false
>when setjmp only returns 0 or non-zero?


There is no true or false in C89. if statements execute the "then" clause
(even though there's no "then" keyword) if the condition evaluates to
non-zero.

While C99 has true and false, this has nothing to do with if statements.

Gordon L. Burditt
 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      03-23-2005

"Dave Vandervies" <> skrev i en meddelelse
news:d1sl51$u98$...
> In article <d1sjvi$5db$>, JS <dsa.@asdf.com> wrote:
> >When setjmp is called how can the if statement evaluate to true or false
> >when setjmp only returns 0 or non-zero?

>
> Have a coffee, go outside, and sit under a tree (or, if the weather
> fails to be cooperative, at least get away from the computer), and
> contemplate the difference between true/false and nonzero/zero, and you
> will be enlightened.



Well I have only Java experience and not yet found anything about this
definition in K&R.


 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      03-23-2005

"Gordon Burditt" <> skrev i en meddelelse
news:4241d80d$0$88029$...
> >When setjmp is called how can the if statement evaluate to true or false
> >when setjmp only returns 0 or non-zero?

>
> There is no true or false in C89. if statements execute the "then" clause
> (even though there's no "then" keyword) if the condition evaluates to
> non-zero.
>
> While C99 has true and false, this has nothing to do with if statements.
>
> Gordon L. Burditt


Where can I find C89 and C99??


 
Reply With Quote
 
Mark Odell
Guest
Posts: n/a
 
      03-23-2005
JS wrote:
>>>When setjmp is called how can the if statement evaluate to true or false
>>>when setjmp only returns 0 or non-zero?

>>
>>Have a coffee, go outside, and sit under a tree (or, if the weather
>>fails to be cooperative, at least get away from the computer), and
>>contemplate the difference between true/false and nonzero/zero, and you
>>will be enlightened.

>
>
>
> Well I have only Java experience and not yet found anything about this
> definition in K&R.


I have found that 0 (zero) and !0 (not zero) work well for false and
true checking.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-23-2005
Mark Odell <> writes:

> I have found that 0 (zero) and !0 (not zero) work well for false and
> true checking.


I have found that 1 works well as !0.
--
Ben Pfaff
email:
web: http://benpfaff.org
 
Reply With Quote
 
Mark Odell
Guest
Posts: n/a
 
      03-23-2005
Ben Pfaff wrote:
> Mark Odell <> writes:
>
>
>>I have found that 0 (zero) and !0 (not zero) work well for false and
>>true checking.

>
>
> I have found that 1 works well as !0.


Yeah but that breaks my "no numbers except zero" rule. Besides,
sometimes 1 looks like l (ell).
 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      03-23-2005
I found this example:

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

jmp_buf ex;

static int foo (int a, int b)
{
if (!b)
longjmp (ex, 1); /* THROW */
else
return a/b;
}

int main (void)
{
int x = 0, y = 1, z = 0;
if (setjmp (ex) == 0) /* TRY : longjmp branches back to here */
{
x = foo(y, z);
}
else /* CATCH */
{
printf ("Exception: attempt to divide by zero\n");
}
}

When foo is called after setjmp has been called, longjmp is called. Then
control jumps to setjmp but this time setjmp does not return 0 and
therefore: printf ("Exception: attempt to divide by zero\n"); is executed.

But I don't understand why setjmp don't return 0 after the longjmp call. Is
it because the second parameter to longjmp is used as the return value for
setjmp?

Or does the second paramter replace 0 in: if (setjmp (ex) == 0)?


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-23-2005
JS wrote:
> When setjmp is called how can the if statement evaluate to true or false
> when setjmp only returns 0 or non-zero?


Since false is 0 and true is non-zero, I fail to see your problem.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ctypes and setjmp Richard Jones Python 4 10-07-2006 03:19 AM
Is setjmp/longjmp ok? Michael B Allen C Programming 11 05-04-2004 08:57 PM
help: why setjmp/longjmp take STRUCT jmp_buf as parameter, not a pointer? someone C Programming 5 05-01-2004 02:47 AM
setjmp, longjmp Mantorok Redgormor C Programming 2 11-12-2003 04:59 PM
How dirty is setjmp+fopen+longjmp ? Thomas Baruchel C Programming 2 10-02-2003 04:56 PM



Advertisments