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

📄 volatiles.html

📁 自己收集的linux入门到学懂高级编程书集 包括linux程序设计第三版
💻 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.6"><!--Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.   <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below).  A copy of the license isincluded 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.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!--  pre.display { font-family:inherit }  pre.format  { font-family:inherit }  pre.smalldisplay { font-family:inherit; font-size:smaller }  pre.smallformat  { font-family:inherit; font-size:smaller }  pre.smallexample { font-size:smaller }  pre.smalllisp    { font-size:smaller }--></style></head><body><div class="node"><p>Node:&nbsp;<a name="Volatiles">Volatiles</a>,Next:&nbsp;<a rel="next" accesskey="n" href="Restricted-Pointers.html#Restricted%20Pointers">Restricted Pointers</a>,Previous:&nbsp;<a rel="previous" accesskey="p" href="Min-and-Max.html#Min%20and%20Max">Min and Max</a>,Up:&nbsp;<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.  Theseare normally accessed by pointers and used for accessing hardware.  Thestandards encourage compilers to refrain from optimizationsconcerning accesses to volatile objects that it might perform onnon-volatile objects.  The C standard leaves it implementation definedas to what constitutes a volatile access.  The C++ standard omits tospecify this, except to say that C++ should behave in a similar mannerto C with respect to volatiles, where possible.  The minimum eitherstandard specifies is that at a sequence point all previous accesses tovolatile objects have stabilized and no subsequent accesses haveoccurred.  Thus an implementation is free to reorder and combinevolatile accesses which occur between sequence points, but cannot do sofor accesses across a sequence point.  The use of volatiles does notallow you to violate the restriction on updating objects multiple timeswithin a sequence point.   <p>In most expressions, it is intuitively obvious what is a read and what isa write.  For instance<pre class="smallexample">     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 thevalue into the volatile object pointed to by <var>dst</var>.  There is noguarantee that these reads and writes are atomic, especially for objectslarger than <code>int</code>.   <p>Less obvious expressions are where something which looks like an accessis used in a void context.  An example would be,<pre class="smallexample">     volatile int *src = <var>somevalue</var>;     *src;     </pre>   <p>With C, such expressions are rvalues, and as rvalues cause a read ofthe object, GCC interprets this as a read of the volatile being pointedto.  The C++ standard specifies that such expressions do not undergolvalue to rvalue conversion, and that the type of the dereferencedobject may be incomplete.  The C++ standard does not specify explicitlythat it is this lvalue to rvalue conversion which is responsible forcausing 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 apointer to volatile object of complete type in a void context as a readof the object.  When the object has incomplete type, G++ issues awarning.<pre class="smallexample">     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 onthe first case, you must force a conversion to rvalue with, for instancea static cast, <code>static_cast&lt;S&gt;(*ptr1)</code>.   <p>When using a reference to volatile, G++ does not treat equivalentexpressions as accesses to volatiles, but instead issues a warning thatno volatile is accessed.  The rationale for this is that otherwise itbecomes difficult to determine where volatile access occur, and notpossible to ignore the return value from functions returning volatilereferences.  Again, if you wish to force a read, cast the reference toan rvalue.   </body></html>

⌨️ 快捷键说明

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