careercup-C和C++ 13.5
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的更多相关文章
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- [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 ...
- [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行,最 ...
- [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 ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...
- [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. 这道题问 ...
- [CareerCup] 13.5 Volatile Keyword 关键字volatile
13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...
- [CareerCup] 13.6 Virtual Destructor 虚析构函数
13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...
- [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 ...
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
随机推荐
- RPi 2B 中文语言包
/************************************************************************* * RPi 2B 中文语言包 * 声明: * 本文 ...
- 【jsonp】
function handleResponse(response) { alert("You’re at IP address " + response.ip + ", ...
- SharePoint 2010 自定义 字段 类型--------省市区联动
转:http://www.cnblogs.com/sp007/p/3384310.html 最近有几个朋友问到了有关自定义字段类型的问题,为了让更多的人了解自定义字段类型的方法,特写一篇博客与大家分享 ...
- [转]javascript console 函数详解 js开发调试的利器
javascript console 函数详解 js开发调试的利器 分步阅读 Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象,使用该 ...
- [转] Attach、Detach和DeleteObject
原文:Attach.Detach和DeleteObject,想飞的梦想 1.CWnd Attatch和Detach的关系 首先,要明白Windows对象和MFC对象的区别. MFC对象实际上并没有把整 ...
- HttpListener supports SSL only for localhost? install certificate
1.Start-All Programs - 2.execute below lines on that ‘Developer Command Prompt..’ tool makecert -n & ...
- hive的使用和深化理解
1.hive中的数据最终是存放在hdfs上的 2.hive本身不是关系型数据库,hive执行sql语句时会把sql语句翻译成mapreduce程序,然后将mapreduce程序提交到hadoop集群中 ...
- 字符串旋转(str.find()---KMP)
此题旋转带有技巧性,问题转化为常见的问题,熟练STL可以直接用str.find()函数,其是主要想用KMP算法实现字符串的查找算法... //如果对于一个字符串A,将A的前面任意一部分挪到后边去形成的 ...
- 2.3CUDA矩阵乘法
CPU 矩阵乘法 能相乘的两个矩阵,必须满足一个矩阵的行数和第二个矩阵的列数相同. A(N*P) * B(P*M) = C(N*M). 其中P是行数,N是列数, 从宽高的角度来说,即 A的宽度和B的高 ...
- Petshop学习第三天
ASP.NET缓存 ASP.NET充分利用缓存机制,通过某种方法,将系统需要的数据对象.Web页面存储在内存中,使得Web站点需要这些数据时,不经过繁琐的数据库连接.查询和复杂的逻辑运算,就可以触手可 ...