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
20 2323
JS wrote: 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)?
setjmp() is very peculiar. You call it like an ordinary
function, and it returns a value of zero. But if you later
call longjmp(), setjmp() returns a second time even though it
has not been called a second time. On this second return, it
yields the value that was given to longjmp() (except that
there's a special case: if you hand a zero to longjmp(),
setjmp() returns a one).
It is even possible to call longjmp() more than once,
causing setjmp() to return more than twice. Such tricks are
probably better used for obfuscation than for real code.
-- Er*********@sun .com
Mark Odell <od*******@hotm ail.com> writes: 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 contemplat e 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.
I've found that section 9 of the C FAQ works even better.
Anything that compares equal to 0 is considered false; anything that
compares unequal to 0 is considered true. Declaring your own FALSE
and TRUE values can be useful (if you don't have C99's <stdbool.h>),
but there's no point in doing anything more elaborate than 0 for FALSE
and 1 for TRUE. If you use FALSE and TRUE, use them only as values to
be assigned; never compare a logical value to FALSE or to TRUE
(especially to TRUE); 2 is "true", but it's not equal to TRUE. For
example, never write:
if (cond == TRUE) { ... }
Instead, just write:
if (cond) { ... }
Built-in operators (==, !=, <, et al) always yield 0 or 1, but
functions returning boolean values (like isdigit()) can only be
assumed to return 0 or non-0.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On Wed, 23 Mar 2005 18:29:31 -0500, Eric Sosman
<er*********@su n.com> wrote: It is even possible to call longjmp() more than once, causing setjmp() to return more than twice. Such tricks are probably better used for obfuscation than for real code.
I've seen it used in a type of state machine, something like:
jmp_buf jump;
void top(void)
{
switch (setjmp(jump))
{
case 0:
start();
case 1:
func1();
case 2:
func2();
/* ... */
case n:
funcn();
default:
break;
}
}
where each function either returned (in which case it fell through to
the next state) or it (or more likely a function it called) called
longjmp(jump, i); to go directly to state i. (In practice the states
were all using an enum rather than a literal number, but you get the
idea.) The advantage was that when calling down through a stack of
functions it didn't need a test after each one to see whether it should
return to a higher level, it just went straight there (there was no
local initialisation which needed tidying).
It's not an advised program structure, because it is very easily abused,
but in the circumstances it was rather elegant...
I've also seen it done in an implementation of exceptions in C, to
"re-throw" the exception at the current level, but there it was hidden
in the exception macros...
Chris C
On Wed, 23 Mar 2005 17:17:01 -0500, Mark Odell wrote: 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).
! is also similar.
There are a few numbers that are OK as constants in some circumstances,
including 1 and 10. E.g. y = x+1 is rarely going to be made clearer by
hiding the 1.
Lawrence
Lawrence Kirby wrote: 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).
! is also similar.
But !0 is unlikely to make sense if read as "ell-zero".
There are a few numbers that are OK as constants in some circumstances, including 1 and 10. E.g. y = x+1 is rarely going to be made clearer by hiding the 1.
Why + 1 I would ask. Why does 'y' need to be equal to 'x' + 1? How could
such an equation be written more self-explanatory? Clearly this is all
style-nitpicking but fun nonetheless.
--
- Mark
On Thu, 24 Mar 2005 08:51:51 -0500, Mark Odell
<od*******@hotm ail.com> wrote: Lawrence Kirby wrote:
There are a few numbers that are OK as constants in some circumstances, including 1 and 10. E.g. y = x+1 is rarely going to be made clearer by hiding the 1. Why + 1 I would ask. Why does 'y' need to be equal to 'x' + 1?
Because that's the formula! I often use something like:
y = sqrt(x*x + 1);
Or an index which needs to start at the next element. The 1 is never
going to change to anything else.
How could such an equation be written more self-explanatory?
Very likely it can't, if it's dealing with mathematics. Defining the
constant as ONE is silly, it makes it more obscure.
Clearly this is all style-nitpicking but fun nonetheless.
Another standard code snippet:
for (i = 0; i < n-1; ++i)
arr[i] = arr[i+1];
It's simple to understand, totally portable, and probably just as
efficient as any other form at shifting the contents of an array.
Anything more will make it less understandable, not more.
Using a #define for a complicated constant like PI makes sense (in
particular, it's easy to make it more precise if needed). Using one for
a number which may change makes sense (array limits, for instance).
Using it for the value 1 where it will never change is more confusing
than using a literal...
Chris C
Lawrence Kirby wrote: On Wed, 23 Mar 2005 17:17:01 -0500, Mark Odell wrote: 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).
! is also similar.
All you really need is #define ten 10. The rest can use (ten/ten)
or (ten-ten) etc. I kid you not. The DAC512 computer used this.
See:
<http://cbfalconer.home .att.net/firstpc/>
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Chris Croughton <ch***@keristor .net> writes:
[regarding setjmp/longjmp] The advantage was that when calling down through a stack of functions it didn't need a test after each one to see whether it should return to a higher level, it just went straight there (there was no local initialisation which needed tidying).
Use of a pool allocator in conjunction with setjmp/longjmp can
simplify code, even if it has many local allocations.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Robert Bachmann <ne**@rbach.pri v.at> wrote: You can buy the final C99 standard at: http://www.iso.org/ http://webstore.ansi.org/
Or, if you want a hard copy at a sane price and with a sane license, at
<http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470845732.html >.
Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
----------------------------------------------------------------- sj.c
|
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 hope will prove usefull). I will be very
gratefull for any help or pointers with this problem. I am...
|
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
|
by: candy |
last post by:
hi all,
Consider the following C code:
void funct(){
}
| |
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)
{
|
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 called from function "A"
that was called form the main(), will cause havoc in the stack. And it
makes...
|
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
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |