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 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  2. Java [Leetcode 39]Combination Sum

    题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in  ...

  3. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  4. CCR

    不用任何与创建线程.资源互斥有关系的API写多线程程序 这次的例子,是一个很简单的控制台,她将面对瞬间提交的百万的数据,而面不改色(CPU.内存非常平稳),队列中始终只保存最新的数据,每次只处理cpu ...

  5. HDU 2196-Computer(树形dp)

    题意: 给出电脑网络连接树,求每个节点的为起点的最长距离 分析: 这道题开始状态想不出来,放了一段时间,后来注意到例题上有这道题,每个节点的最长距离可由父节点的最长距离,次长距离,和子节点的最长距离( ...

  6. 一个简单的appium脚本

    这是一个简单的appium脚本,测试amazon购物过程的,过程包括搜索商品,然后从结果列表中选取中意的商品,然后加入购物车,等等. 它是一个最原始的脚本,对测试元素.数据.报告等未作任何的封装,后面 ...

  7. xmbc 资源

    http://raspberrypi.diandian.com/post/2013-02-25/40048470423 http://blog.csdn.net/lincyang/article/de ...

  8. 设计模式_Flyweight_享元模式

    形象例子: 每天跟MM发短信,手指都累死了,最近买了个新手机,可以把一些常用的句子存在手机里,要用的时候,直接拿出来,在前面加上MM的名字就可以发送了,再不用一个字一个字敲了.共享的句子就是Flywe ...

  9. wuzhicms 无规律推荐位标签的嵌套使用

    这种布局的页面,要做推荐位的话,往往需要做3个推荐位.但这样就得维护3个推荐位内容. 其实可以通过简单粗暴的方式 一个推荐位即可搞定. 如下代码:  {wz:content action=" ...

  10. JavaScript中值的真真假假(true and false)

    值为flase的有: false 0 "" //空串 null undefined NaN 除了以上的之外的都是ture,包括"0" (zero in quot ...