⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 volatiles.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>Using the GNU Compiler Collection (GCC)</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="Using the GNU Compiler Collection (GCC)">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,

1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.2 or

any later version published by the Free Software Foundation; with the

Invariant Sections being "GNU General Public License" and "Funding

Free Software", the Front-Cover texts being (a) (see below), and with

the Back-Cover Texts being (b) (see below).  A copy of the license is

included in the section entitled "GNU Free Documentation License".



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development.-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Volatiles">Volatiles</a>,

Next:<a rel="next" accesskey="n" href="Restricted-Pointers.html#Restricted%20Pointers">Restricted Pointers</a>,

Previous:<a rel="previous" accesskey="p" href="Min-and-Max.html#Min%20and%20Max">Min and Max</a>,

Up:<a rel="up" accesskey="u" href="C---Extensions.html#C++%20Extensions">C++ Extensions</a>

<hr><br>

</div>



<h3 class="section">When is a Volatile Object Accessed?</h3>



   <p>Both the C and C++ standard have the concept of volatile objects.  These

are normally accessed by pointers and used for accessing hardware.  The

standards encourage compilers to refrain from optimizations

concerning accesses to volatile objects that it might perform on

non-volatile objects.  The C standard leaves it implementation defined

as to what constitutes a volatile access.  The C++ standard omits to

specify this, except to say that C++ should behave in a similar manner

to C with respect to volatiles, where possible.  The minimum either

standard specifies is that at a sequence point all previous accesses to

volatile objects have stabilized and no subsequent accesses have

occurred.  Thus an implementation is free to reorder and combine

volatile accesses which occur between sequence points, but cannot do so

for accesses across a sequence point.  The use of volatiles does not

allow you to violate the restriction on updating objects multiple times

within a sequence point.



   <p>In most expressions, it is intuitively obvious what is a read and what is

a write.  For instance



<pre class="example">     volatile int *dst = <var>somevalue</var>;

     volatile int *src = <var>someothervalue</var>;

     *dst = *src;

     </pre>



<p>will cause a read of the volatile object pointed to by <var>src</var> and stores the

value into the volatile object pointed to by <var>dst</var>.  There is no

guarantee that these reads and writes are atomic, especially for objects

larger than <code>int</code>.



   <p>Less obvious expressions are where something which looks like an access

is used in a void context.  An example would be,



<pre class="example">     volatile int *src = <var>somevalue</var>;

     *src;

     </pre>



   <p>With C, such expressions are rvalues, and as rvalues cause a read of

the object, GCC interprets this as a read of the volatile being pointed

to.  The C++ standard specifies that such expressions do not undergo

lvalue to rvalue conversion, and that the type of the dereferenced

object may be incomplete.  The C++ standard does not specify explicitly

that it is this lvalue to rvalue conversion which is responsible for

causing an access.  However, there is reason to believe that it is,

because otherwise certain simple expressions become undefined.  However,

because it would surprise most programmers, G++ treats dereferencing a

pointer to volatile object of complete type in a void context as a read

of the object.  When the object has incomplete type, G++ issues a

warning.



<pre class="example">     struct S;

     struct T {int m;};

     volatile S *ptr1 = <var>somevalue</var>;

     volatile T *ptr2 = <var>somevalue</var>;

     *ptr1;

     *ptr2;

     </pre>



   <p>In this example, a warning is issued for <code>*ptr1</code>, and <code>*ptr2</code>

causes a read of the object pointed to.  If you wish to force an error on

the first case, you must force a conversion to rvalue with, for instance

a static cast, <code>static_cast&lt;S&gt;(*ptr1)</code>.



   <p>When using a reference to volatile, G++ does not treat equivalent

expressions as accesses to volatiles, but instead issues a warning that

no volatile is accessed.  The rationale for this is that otherwise it

becomes difficult to determine where volatile access occur, and not

possible to ignore the return value from functions returning volatile

references.  Again, if you wish to force a read, cast the reference to

an rvalue.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -