Behaviour of setjmp/longjmp and registers

Geoff Rimmer geoff at warwick.UUCP
Tue Jan 24 00:49:03 AEST 1989


In article <25 at torsqnt.UUCP> david at torsqnt.UUCP (David Haynes) writes:
>What should be the result of running the following program?
>
>#include <setjmp.h>
>
>main()
>{
>	register int j;
>	jmp_buf env;
>
>	j = 1;
>	if(setjmp(env) == 1) {
>		printf("j = %d\n", j);
>		exit(1);
>	}
>	printf("j = %d\n", j);
>	j += 3;
>	longjmp(env, 1);
>}
>
>Sequent, Ultrix and Vax C give results of j = 1, j = 4.
>Gcc gives a result of j = 1, j = 1.
>What does the ANSI standard say about this?

I don't know what the ANSI standard says, but our manual page for
setjmp() mentions in passing that

	register variables have unpredictable values code after the
	return from longjmp 

So, it's probably just luck that the Sequent, Ultrix and Vax C
compiler give what you'd expect from a non-register variable: 
j = 1, j = 4 

Geoff

	------------------------------------------------------------
	Geoff Rimmer, Computer Science, Warwick University, England.
			geoff at uk.ac.warwick.emerald

	"Oo, have we got a video?"
	"If anyone else asks me that question, I'm going to put
	head through the window."
		- The Young Ones, 1984 (?)
	------------------------------------------------------------



More information about the Comp.lang.c mailing list