1<html lang="en"> 2<head> 3<title>C++ Volatiles - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" title="C++ Extensions"> 9<link rel="next" href="Restricted-Pointers.html#Restricted-Pointers" title="Restricted Pointers"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 131998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 142010 Free Software Foundation, Inc. 15 16Permission is granted to copy, distribute and/or modify this document 17under the terms of the GNU Free Documentation License, Version 1.3 or 18any later version published by the Free Software Foundation; with the 19Invariant Sections being ``Funding Free Software'', the Front-Cover 20Texts being (a) (see below), and with the Back-Cover Texts being (b) 21(see below). A copy of the license is included in the section entitled 22``GNU Free Documentation License''. 23 24(a) The FSF's Front-Cover Text is: 25 26 A GNU Manual 27 28(b) The FSF's Back-Cover Text is: 29 30 You have freedom to copy and modify this GNU Manual, like GNU 31 software. Copies published by the Free Software Foundation raise 32 funds for GNU development.--> 33<meta http-equiv="Content-Style-Type" content="text/css"> 34<style type="text/css"><!-- 35 pre.display { font-family:inherit } 36 pre.format { font-family:inherit } 37 pre.smalldisplay { font-family:inherit; font-size:smaller } 38 pre.smallformat { font-family:inherit; font-size:smaller } 39 pre.smallexample { font-size:smaller } 40 pre.smalllisp { font-size:smaller } 41 span.sc { font-variant:small-caps } 42 span.roman { font-family:serif; font-weight:normal; } 43 span.sansserif { font-family:sans-serif; font-weight:normal; } 44--></style> 45<link rel="stylesheet" type="text/css" href="../cs.css"> 46</head> 47<body> 48<div class="node"> 49<a name="C++-Volatiles"></a> 50<a name="C_002b_002b-Volatiles"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Restricted-Pointers.html#Restricted-Pointers">Restricted Pointers</a>, 53Up: <a rel="up" accesskey="u" href="C_002b_002b-Extensions.html#C_002b_002b-Extensions">C++ Extensions</a> 54<hr> 55</div> 56 57<h3 class="section">7.1 When is a Volatile C++ Object Accessed?</h3> 58 59<p><a name="index-accessing-volatiles-3255"></a><a name="index-volatile-read-3256"></a><a name="index-volatile-write-3257"></a><a name="index-volatile-access-3258"></a> 60The C++ standard differs from the C standard in its treatment of 61volatile objects. It fails to specify what constitutes a volatile 62access, except to say that C++ should behave in a similar manner to C 63with respect to volatiles, where possible. However, the different 64lvalueness of expressions between C and C++ complicate the behavior. 65G++ behaves the same as GCC for volatile access, See <a href="C-Extensions.html#C-Extensions">Volatiles</a>, for a description of GCC's behavior. 66 67 <p>The C and C++ language specifications differ when an object is 68accessed in a void context: 69 70<pre class="smallexample"> volatile int *src = <var>somevalue</var>; 71 *src; 72</pre> 73 <p>The C++ standard specifies that such expressions do not undergo lvalue 74to rvalue conversion, and that the type of the dereferenced object may 75be incomplete. The C++ standard does not specify explicitly that it 76is lvalue to rvalue conversion which is responsible for causing an 77access. There is reason to believe that it is, because otherwise 78certain simple expressions become undefined. However, because it 79would surprise most programmers, G++ treats dereferencing a pointer to 80volatile object of complete type as GCC would do for an equivalent 81type in C. When the object has incomplete type, G++ issues a 82warning; if you wish to force an error, you must force a conversion to 83rvalue with, for instance, a static cast. 84 85 <p>When using a reference to volatile, G++ does not treat equivalent 86expressions as accesses to volatiles, but instead issues a warning that 87no volatile is accessed. The rationale for this is that otherwise it 88becomes difficult to determine where volatile access occur, and not 89possible to ignore the return value from functions returning volatile 90references. Again, if you wish to force a read, cast the reference to 91an rvalue. 92 93 <p>G++ implements the same behavior as GCC does when assigning to a 94volatile object – there is no reread of the assigned-to object, the 95assigned rvalue is reused. Note that in C++ assignment expressions 96are lvalues, and if used as an lvalue, the volatile object will be 97referred to. For instance, <var>vref</var> will refer to <var>vobj</var>, as 98expected, in the following example: 99 100<pre class="smallexample"> volatile int vobj; 101 volatile int &vref = vobj = <var>something</var>; 102</pre> 103 </body></html> 104 105