473,583 Members | 3,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using setjmp

JS
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_routin e) (void *);
void *arg;
jmp_buf state;
int stack[1024];
};
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(s truct pcb));
if(setjmp(pcb_p ointer->state)) {
current->start_routine( current->arg);
printf("Thread returned\n");
exit(0);
}
Nov 14 '05 #1
20 2305
>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
Nov 14 '05 #2
In article <d1**********@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> 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 dj******@csclub .uwaterloo.caThe 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
Nov 14 '05 #3
JS

"Dave Vandervies" <dj******@csclu b.uwaterloo.ca> skrev i en meddelelse
news:d1******** **@rumours.uwat erloo.ca...
In article <d1**********@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> 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.
Nov 14 '05 #4
JS

"Gordon Burditt" <go****@hammy.b urditt.org> skrev i en meddelelse
news:42******** **************@ news.airnews.ne t...
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??
Nov 14 '05 #5
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.
Nov 14 '05 #6
Mark Odell <od*******@hotm ail.com> 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: bl*@cs.stanford .edu
web: http://benpfaff.org
Nov 14 '05 #7
Ben Pfaff wrote:
Mark Odell <od*******@hotm ail.com> 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).
Nov 14 '05 #8
JS
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)?
Nov 14 '05 #9
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.
Nov 14 '05 #10

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

Similar topics

2
2280
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
6
4839
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 setjmp/longjmp() are taking stuct: c:\> dmc sj.c (Digital Mars Compiler Version 8.38n) link sj,,,user32+kernel32/noi; c:\> sj.exe sizeof(jmp_buf) = 64...
4
2121
by: Jrferguson | last post by:
I have a C program that I am trying to port to a Motorola 68k based system. It makes use of setjmp and longjmp which are not supported by my C compiler. I understand the general principle behind setjmp/longjmp, but I am somewhat lost in the detail of how to actually implement this in assembler. (My compiler supports in-line assembler which I...
18
2430
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
5
1867
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
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) {
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...
4
2070
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 ctypes might be patched to offer setjmp support in foreign function definitions? Richard
5
1606
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 7.13.1.1 of n869 so my guess is that it's not legal. If it's not legal could someone give me some insight
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8317
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...
0
6574
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...
1
5695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5369
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
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
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.