access to a volatile variable

Nathan Sidwell nathan@acm.org
Thu Jun 17 10:05:00 GMT 1999


David Whitten wrote:

> > volatile int *pvi;
> > struct foobar;
> > volatile foobar *pvfb;
> > foobar *pfb;
> >
> > int x;
> > x = *pvi; //access through pointer
> > *pfb = *pvfb; // error, dereference incomplete
> > *pvfb; // no error, no deref -- should warn (this construct doesn't make much
> > sense even if complete)

> 
> If you are writing code that the volatile element is a memory mapped
> I/O channel, this construct makes sense.  Just because you don't do
> anything with the result you read from the I/O channel doesn't mean that
> strobing the address bus to access a volatile will have no affect.
I understand what you're talking about, and Alexandre pointed it out. In your
case you'd be accessing via a `volatile int *' (or other _integral_ type), not
via
`volatile struct thing *'. Describing a device's memory layout with a struct
and then mapping it at a particular address I understand. Dinking particular
fields of that struct to strobe something I understand. I find it hard to
understand why you'd dink the *whole* struct in one go. But I'm not suggesting
gratuitously banning it.

Anyway, even just reading the volatile int as `*pvi;' will not cause a read of
the location pointed to. (It will in C but not in C++ [this is Mike's
objection].) If you want to strobe some location and discard the result, you
have to write

  int tmp;
  tmp = *pvi;

both
	*pvi;
and
	(void)*pvi;

won't do because the lvalue `*pvi' is not converted to an rvalue. This is a
surprise worthy of a warning (IMHO).

nathan

-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
        I have seen the death of PhotoShop -- it is called GIMP
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk


More information about the Gcc-bugs mailing list