13.5 谈谈C语言关键字”volatile”的意义(或重要性)?

解答

关键字volatile的作用是指示编译器,即使代码不对变量做任何改动,该变量的值仍可能被外界修改。操作系统、硬件或其他线程都可能修改该变量。该变量的值由可能遭受意料之外的修改,因此,每一次使用时,编译器都会重新从内存中获取这个值。

volatile的意思是”易变的”,因为访问寄存器比访问内存要快得多, 所以编译器一般都会做减少存取内存的优化。volatile 这个关键字会提醒编译器,它声明的变量随时可能发生变化(在外部被修改), 因此,与该变量相关的代码不要进行编译优化,以免出错。

声明一个volatile变量:

volatile int x;
int volatile x;

声明一个指针,指向volatile型的内存(即指针指向的内存中的变量随时可能变化):

volatile int *x;
int volatile *x

指向非volatile数据的volatile指针很少见,但也是可行的,声明一个volatile指针,指向非volatile内存::

int* volatile x;

声明一个volatile指针,指向volatile内存(即指针和指针所指物都随机可能变化):(volatile与const类似)

volatile int * volatile x;
int volatile * volatile x;

volatile在声明上的使用和const是一样的。volatile在*号左边, 修饰的是指针所指物;在*号右边修饰的是指针。

用volatile修饰的变量相关的代码不会被编译器优化,那么它有什么好处呢? 来看下面的例子:

int opt = ;
void Fn(void){
start:
if (opt == ) goto start;
else break;
}

上述代码看起来就是一个无限循环的节奏,编译器可能会将它优化成下面的样子:

void Fn(void){
start:
int opt = ;
if (true)
goto start;
}

由于程序中并没有对opt进行修改,因此将if中的条件设置为恒真。这样一来, 就陷入了无限循环中。但是,如果我们给opt加上volatile修饰, 表明外部程序有可能对它进行修改。那么,编译器就不会做上述优化, 上述程序在opt被外部程序修改后将跳出循环。此外, 当我们在一个多线程程序中声明了一些全局变量,且任何一个线程都可以修改这些变量时, 关键字volatile也会派上用场。在这种情况下, 我们就要明确地告诉编译器不要对这些全局变量的相关代码做优化。

careercup-C和C++ 13.5的更多相关文章

  1. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  2. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  3. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  4. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  5. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  6. [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝

    13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...

  7. [CareerCup] 13.5 Volatile Keyword 关键字volatile

    13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...

  8. [CareerCup] 13.6 Virtual Destructor 虚析构函数

    13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...

  9. [CareerCup] 13.7 Node Pointer 节点指针

    13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...

  10. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

随机推荐

  1. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...

  2. 【转】Android - Button(按钮)的响应点击事件的4种写法

    原文网址:http://www.yrom.net/blog/2011/12/12/android-4-onclicklistener-of-button/ Button控件setOnclickList ...

  3. (八)学习MVC之三级联动

    1.新建项目,MVC选择基本模板 2.新建类:Model/Student.cs,数据库信息有三个实体:分别是年级.班级和学生. using System; using System.Collectio ...

  4. java.lang和java.lang.annotation中实现Annotation的类小结

    加了注解,等于打上了某种标记,没加,则等于没有某种标记,以后,其他程序可以用反射来了解你的类上面有无何种标记,看你有什么标记,就去干相应的事.标记可以加在类,方法,字段,包上,方法的参数上. (1)  ...

  5. 在Linux运行期间升级Linux系统(Uboot+kernel+Rootfs)

    版本:v1.2   Crifan Li 摘要 本文主要介绍了如何在嵌入式Linux系统运行的时候,进行升级整个Linux系统,包括uboot,kernel和rootfs.以及简介Linux中的已有的通 ...

  6. android——创建camera应用(译)

     只是选择相机部分来翻译.下面是主要内容 有些开发者可能需要Camera的接口,来定制自己程序的外观和特殊功能.创建自定义的Camera界面比使用using an Intent需要编写更多的代码,但是 ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.1

    Let $A=A_1\oplus A_2$. Show that (1). $W(A)$ is the convex hull of $W(A_1)$ and $W(A_2)$; i.e., the ...

  8. JSP文件上传代码

    一.首先建立一个上传的界面,取名为a.jsp,代码如下 <%@ page contentType="text/html; charset=utf-8" language=&q ...

  9. POJ 3345-Bribing FIPA(树状背包)

    题意: 有n个国家投票,要得到一个国家的投票有一定的花费,如果给到一个国家的票同时也得到了它所有附属国的票,给出国家关系树,求至少得到m票的最小花费. 分析:基础树状背包,dp[i][j],以i为根的 ...

  10. 远程调试树莓PI

    非官方 参考  http://linuxtortures.blogspot.jp/2012/06/cross-compiling-and-cross-debugging-c.html 注意: 建立 / ...