The world is not ready for 'volatile'

Colin Plumb w-colinp at microsoft.UUCP
Thu Jan 5 11:01:03 AEST 1989


I can think of at least one example of a register volatile variable, if
you're playing with setjmp/longjmp.  We all know a non-volatile variable
can't be trusted across a setjmp, so given

register int x, y;
jmp_buf jbuf;

x = 0;
y = setjmp(jbuf);
x++;
if (y = 0)
	longjmp(jbuf, 1);
printf("x = %d\n", x);

This "should" print 2, but a compiler could legitimately print x = 1.
Obviously, x isn't aliased (register), and flow analysis (which isn't
required to understand setjmp and longjmp) shows it is set to 0, incremented
once, and printed.  

"Register volatile x" tells the compiler to try and store it somewhere
quick, but safe from setjmp/longjmp.  In most compilers, this would
basically turn off the register declaration, but in transputer compilers
I've used, "register" tells the compiler to keep the value in the 16 words
nearest the stack pointer, which are especially quick to access.  Here,
register and volatile both mean something.

Can anyone poke holes in this example?
-- 
	-Colin (uunet!microsof!w-colinp)



More information about the Comp.lang.c mailing list