volatile: a summary

Ross Cunniff cunniff at hpfcdc.HP.COM
Sat Jun 11 04:41:42 AEST 1988


In article <11884 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
> ...whenever a
> programmer can discover that *(int *)0xf0 is not volatile, but *(int *)
> 0xff4 is, by reading a manual, a hypothetical perfect compiler can do
> the same by reading `(the electronic version of) the manual'....
> ...The compiler's electronic
> version of the manual might even read
>
>	volatile 0xff0..0xf9f
>
> In this case, even though the word `volatile' appears *somewhere*, it
> is nowhere in the C source, and hence the language proper has no
> volatile keyword.
>
> For those who will claim that this is mere trickery, again, you have
> not thought hard enough about the issue.  Instead of `volatile
> 0xff0..0xf9f' the manual might instead say `ram 0x0..0xeff; ram
> 0x4000..0xbfffffff'.  Instead of saying what *is* volatile, this says
> what *cannot* *be* volatile.  And if the electronic manual does not
> exist, or if my hypothetical perfect compiler does not exist, as it
> does not, or even if it *cannot* exist, what then?  Is the volatile
> keyword necessary?  No.

This is all fine and dandy for those volatile hardware addresses which
a C program may access.  It doesn't do diddly-squat for the following
type of program (this example should but doesn't use volatile):

	/* foo.c */
	extern int *p;

	foo( x ) {
	    int i;

	    *p = x;

	    if( *p == x ) {
		/* With an optimizing compiler, this part of the */
		/* 'if' is likely ALWAYS to be executed, regardless */
		/* of whethter other processes are playing with */
		/* shared memory */
		printf( "Nobody else is plaing with shared memory\n" );
	    }
	    else {
		printf( "Wow!  somebody else is out there...\n" );
	    }
	}

	/* main.c */
	int *p;
	char *shm_get();

	main()
	{
	    p = (int *)shm_get( 3000, "foobar" );
	    foo( getpid() );
	}

Note that there is *NO WAY* for the compiler *OR* the linker to know
where the shm_get thingie is going to attach the shared memory segment.
Oh, sure, you could have a dictionary that says that 'volatile shm_get',
but if you're going to go that far, you might as well just add volatile
to the standard (oh, we're already doing that?).

				Ross Cunniff
				Hewlett-Packard HP-UX DCE lab
				...{ucbvax,hplabs}!hpda!cunniff
				cunniff%hpda at hplabs.ARPA



More information about the Comp.lang.c mailing list