什么是挥发性放大器之间的差异;的extern? [英] What is the difference between volatile & extern?

查看:72
本文介绍了什么是挥发性放大器之间的差异;的extern?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天就回来,我有一次采访,但是,我仍然正在寻找答案。
我想了解使用volatile关键字的意义。

Few days back i had an interview but, still I am searching for the answer. I would like to understand the significance of using volatile keyword.

查找低于code:两个不同的情景

Find the code below: Two different scenario.

//project1
//File1.c

int abc;//Global variable
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
  printf("abc == 3");
}
else
{
  printf("abc != 3");
}
/*So if or else part will not be optimized 
because "abc" can not be predicted, 
the value can chage at any point of time */




//Project2
//file1.c

volatile int abc;//Global variable with volatile keyword

/*And this variable is getting used in some other files too.*/

if(abc == 3) //Say
{
  printf("abc == 3");
}
else
{
  printf("abc != 3");
}
/*So if or else part will not be optimized 
because "abc" can not be predicted as it is declared as volatile,
the value can chage at any point of time */

为什么我们应该使用volatile关键字呢?

Why we should use volatile keyword instead?

推荐答案

由于托尼德尔罗伊在其评论解释, EXTERN 挥发性有很大的不同。

As Tony Delroy explained in its comment, extern and volatile are quite different.

挥发性关键字保护您的变量被积极优化。优化的变量可以是无形的其他线程,并永远不会到达主内存。有时,编译器可以甚至挤完全一个变量,如果不是需要它。编译器基础的猜测与源$ C ​​$ C作为其唯一的输入。有时,是一个可以改变你的变量值的一些外部事件。它可能是一个硬件设备或其他处理,例如。

Volatile keyword protects your variable from being aggressively optimised. An optimised variable can be invisible to other threads and never reach main memory. Sometimes, compiler can even squeeze entirely a variable if it's not needed. Compiler base its guess with your source code as its only input. Sometimes, there is some external events which can change your variable value. It could be an hardware device or an other process, for instance.

=>具体而言,编译器将禁用一些的优化这个变量,因此它可以表现为你想让它。

=> Concretely, compiler disables some optimisations to this variable, so it can behave as you want it to.

EXTERN 是不是缓存内存VS。 EXTERN 只是有关访问该生活在一个其他目标文件的变量。请参见这个简单的例子什么样的​​组装$的C $ C是一个的extern 访问产生的。
那些的extern 变量是在他们自己的目标文件,只要它是可能的优化。有没有关于保护与否的问题。

Extern is not about cache vs memory. Extern is just about accessing a variable which lives in an other object files. See this short example of what kind of assembly code is generated for an extern access. Those extern variables are optimised in their own object file as far as it's possible. There's no question about to protect it or not.

=>具体地,编译器指示外部基准需要在链接时要解决的

=> Concretely, compiler indicates an external reference needing to be solved at link time

这篇关于什么是挥发性放大器之间的差异;的extern?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆