[comp.lang.c] setjmp/longjmp question

ttwang@polyslo.CalPoly.EDU (Sir Hoyt) (02/25/89)

I am considering using setjmp/longjmp to implement exception handling in C.
It would be used like this:

int foo()
{
 /* variable allocation here */
 if (setjmp( ... ))
 { /* clean up the variables */ }
 else
 { /* normal codes */ }
}

Normal codes would including calling other subroutines.  When error occurs,
longjmp() will be used to jump back to foo().

I am aware that on the Sun, all register variables cannot be trusted after a
longjmp().

What I want to know is the behavior of setjmp/longjmp on other machines.
Would longjmp() clober all local variables if assuming RISC machine, and
automatic register allocation?

 -Thomas Wang ("I am, therefore I am."
                 - Akira               )
                                                     ttwang@polyslo.calpoly.edu

ark@alice.UUCP (Andrew Koenig) (02/26/89)

In article <8514@polyslo.CalPoly.EDU>, ttwang@polyslo.CalPoly.EDU (Sir Hoyt) writes:

> What I want to know is the behavior of setjmp/longjmp on other machines.
> Would longjmp() clober all local variables if assuming RISC machine, and
> automatic register allocation?

In ANSI C, longjmp is permitted to clobber all local variables that are
not declared volatile.  In earlier C implementations, longjmp tends
to clobber all register variables but leave the rest alone.
-- 
				--Andrew Koenig
				  ark@europa.att.com

henry@utzoo.uucp (Henry Spencer) (02/26/89)

In article <8514@polyslo.CalPoly.EDU> ttwang@polyslo.CalPoly.EDU (Thomas Wang) writes:
>What I want to know is the behavior of setjmp/longjmp on other machines.
>Would longjmp() clober all local variables if assuming RISC machine, and
>automatic register allocation?

It could.  Furthermore, it could happen on your Sun with future versions
of the compiler.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

tps@chem.ucsd.edu (Tom Stockfisch) (03/02/89)

In article <1989Feb26.012335.12938@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>In article <8514@polyslo.CalPoly.EDU> ttwang@polyslo.CalPoly.EDU (Thomas Wang) writes:
>>Would longjmp() clober all local variables...

>It could.


Even if the local variable is an array?

    
    
    
    
    
-- 

|| Tom Stockfisch, UCSD Chemistry	tps@chem.ucsd.edu

henry@utzoo.uucp (Henry Spencer) (03/04/89)

In article <419@chem.ucsd.EDU> tps@chem.ucsd.edu (Tom Stockfisch) writes:
>>>Would longjmp() clober all local variables...
>
>>It could.
>
>Even if the local variable is an array?

Yes.  Absolutely no promises are made about the fate of local variables,
in the absence of "volatile".  Don't forget that there are machines in
which registers are addressable, and on those machines it is possible
(although perhaps seldom sensible) to put (small) arrays in registers.
Also, it isn't necessarily true that arrays *must* be in addressable
storage.  In the general case, with pointers being passed around with
wild abandon, they have to be.  But many functions use arrays in much
more disciplined ways, such that the compiler can completely understand
what is going on.  In such cases, a clever compiler might be able to
put arrays into things like Cray vector registers.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

karl@haddock.ima.isc.com (Karl Heuer) (03/04/89)

>>>Would longjmp() clober all local variables...
>>It could.
>Even if the local variable is an array?

Yes, even then it could.  For example, an auto array with small dimension
might be cached into registers if all of the associated address arithmetic is
visible to the compiler at compile-time.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint