Behaviour of setjmp/longjmp and registers

Tim Moore moore%cdr.utah.edu at wasatch.UUCP
Sat Jan 21 07:21:19 AEST 1989


In article <25 at torsqnt.UUCP> david at torsqnt.UUCP (David Haynes) writes:

	#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?
	
	-david-

Gcc follows the ANSI standard which, when strictly interpreted, says
that "the only automatic variables guaranteed to remain valid are
those declared volatile" (quoted from the gcc manual). Traditional
compilers put local variables in the stack in functions that call
setjump. If you look at the assembly code output of  Sequent,
Ultrix, Vax C, and gcc with the "-traditional" flag you will see that
j isn't really being stored in a register.
			-Tim Moore
	4560 M.E.B.		   internet:moore at cs.utah.edu
	University of Utah	   ABUSENET:{ut-sally,hplabs}!utah-cs!moore
	Salt Lake City, UT 84112



More information about the Comp.lang.c mailing list