reference to volatile becomes itself volatile

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Compiling the following with Debian g++ 4.4.5-8 on x86_64, using
-m64 -fno-exceptions -O2 -S:

static volatile char &ref_to_volatile =
  *reinterpret_cast<char *>(10000000000);
char
increment_at_ref_to_volatile()
{
  return ++ref_to_volatile;
}

generates code that first reads the reference from memory and
then accesses the volatile char thrice.  I'd like to understand
why it reads the reference from memory instead of inlining the
address constant in the instruction.

If I change the source to:

static volatile char *volatile const cv_ptr_to_volatile =
  reinterpret_cast<char *>(10000000000);
char
increment_at_cv_ptr_to_volatile()
{
  return ++*cv_ptr_to_volatile;
}

then the generated code is similar to what I got with
ref_to_volatile.  However, if I make it use any of these:

static volatile char *const const_ptr_to_volatile =
  reinterpret_cast<char *>(10000000000);
static char &ref_to_plain =
  *reinterpret_cast<char *>(10000000000);

then GCC does inline the address.  Thus, it looks like the
volatility of the referenced char propagates to the reference
itself.  I wonder if this is something required by the C++
standard or just a quirk of the GCC implementation.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux