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 ...
随机推荐
- C# List 用法与示例
Problem. You have questions about the List collection in the .NET Framework, which is located in the ...
- js如何判断是手机端还是PC端访问
function isMobile(){ var sUserAgent= navigator.userAgent.toLowerCase(), bIsIpad= sUserAgent.match(/i ...
- I.MX6 android shutdown 内核崩溃
/**************************************************************************** * I.MX6 android shutdo ...
- ASP.NET使用Jquery-Ajax向ashx传递参数中文出现乱码
今天遇到个问题,IE11下Jquery-Ajax向ashx传递参数中文出现乱码,但在谷歌.火狐.360等浏览器中没有乱码的问题,百度了好久最后发现使用escape()对参数值进行处理就可以了: 参考代 ...
- 【HTML】Advanced7:HTML5 Forms Pt. 2: Attributes and Data Lists
1.<label for"" ></label> <input type="email" placeholder=" & ...
- Python中的导入
转自:http://bingotree.cn/?p=569 参考<Python学习手册>,强烈建议看下这本书的相关章节. 在一些规模较大的项目中,经常可以看到通过imp.__import_ ...
- 2016"百度之星" - 复赛(Astar Round3) 1003 拍照
拍照 思路:先静态,离线树状数组,分别统计每个点向左向右能看到的船的数量.再枚举整个区间求最大值. 应为人和船都是动态的,假设船往左走,处理每个点看到向左最大船的数量,满足动态条件.就是向左的船一开始 ...
- Codeforces 603A Alternative Thinking
题意:给你一个01串,必须替换一次,且替换的为子串.问换完后,最大01串长度. #include <bits/stdc++.h> typedef long long ll; using n ...
- Spark系列(四)整体架构分析
架构流程图 说明 Driver端流程说明(Standalone模式) 使用spark-submit提交Spark应用程序Application. 通过反射的方式创建和构造一个DriverActor进 ...
- 宿主进程 vshost.exe
Hosting Process (vshost.exe) 宿主进程是VS的一个特性.可以提高调试的性能,可以进行部分信任调试(partial trust debugging),可以进行设计时表达式计算 ...