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 ...
随机推荐
- LeetCode Intersection of Two Linked Lists (找交叉点)
题意: 给两个链表,他们的后部分可能有公共点.请返回第一个公共节点的地址?若不重叠就返回null. 思路: 用时间O(n)和空间O(1)的做法.此题数据弱有些弱. 方法(1)假设两个链表A和B,用两个 ...
- apache开源项目--Mahout
Apache Mahout 是 Apache Software Foundation (ASF) 开发的一个全新的开源项目,其主要目标是创建一些可伸缩的机器学习算法,供开发人员在 Apache 在许可 ...
- shell编程——if语句 if -z -n -f -eq -ne -lt
if 条件then Commandelse Commandfi 别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...
- 选择select框跳出信息
<html > <body > <select type="select" name=s1 onChange=alert("你选择了&quo ...
- java 基础之数据类型
java 数据类型这个地方面试的时候会被经常问到,很多人并不注意这个问题,今天带大家全面了解一下.java数据类型主要分:1.基本数据类型 2.引用数据类型 3.空类型 下面一一介绍. 基本数据类型包 ...
- puppet学习:类与类的依赖关系的问题
今天在部署Zabbix的Proxy时,在负责安装的Exec中去掉了一些无关的Package的依赖,结果,就出现了依赖关系的问题. 在zabbix::install中,我写的是require mysql ...
- Zabbix探索:LDAP的认证方式
这两天部署了Zabbix测试环境,终于用Puppet部署完成了.总是存在一些小问题,如服务不起动啦之类的. LDAP验证方式配置 刚刚配置Zabbix的用户管理,使用LDAP方式认证. 比较惊喜的是L ...
- 【原创】setjmp longjump一些注意点及使用方法
setjmp longjump一些注意点及使用方法 jmp_buf结构体的定义 #define _JBLEN 9typedef struct { int _jb[_JBLEN + 1]; } jmp ...
- HW7.3
public class Solution { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A' ...
- 各种排序算法代码(C语言版)
选择排序 #include <stdio.h> /* * 选择排序 * 稳定性:不稳定 * 时间复杂度:O(N^2) **/ void select_sort(int a[], int l ...