常量与功放之间使用高度差;用C挥发性预选赛? [英] Usage wise difference between const & volatile qualifier in C?

查看:124
本文介绍了常量与功放之间使用高度差;用C挥发性预选赛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过问题的答案了@
 <一href=\"http://stackoverflow.com/questions/2089464/what-are-the-differences-between-const-and-volatile-pointer-in-c\">What在C const和volatile指针之间的区别是什么?
我理解的解释是:

I have already gone through the answer of question @ What are the differences between const and volatile pointer in C? I understand the explanation that:

const修饰意味着这个code不能改变变量的值,但是,这并不意味着价值无法通过此code以外的方式来改变。
然而,挥发性说,这个数据可能被别人改变等编译器不会做出这些数据做任何假设。

The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. However, volatile says "this data might be changed by someone else" and so the compiler will not make any assumptions about that data.

这意味着,这两个类型的变量可以由外部事件而改变。

Which implies that both type of variables can be changed by external event.

不过,又在哪里是常量和放大器的使用的区别;挥发性?

But,then where is the difference in usage of const & volatile?

在C,并编译器优化为const的工作?

In C, does compiler optimizations work for const?

推荐答案

挥发性和const在许多方面是不同的,它们是两个明显不同的特点。

volatile and const are different in many ways, they are two distinctively different features.

声明一个变量只是为const从来没有的意思是我希望这个变量在程序之外进行修改,我不知道你在哪里得到了这个想法。如果你希望一个const变量为code之外进行修改,它必须被声明为挥发性常量或编译器可能会假设变量是永远不会改变。

Declaring a variable just as const never means "I expect this variable to be modified outside the program", I'm not sure where you got that idea from. If you expect a const variable to be modified outside the code, it must be declared as volatile const or the compiler may assume that the variable is never changed.

在默认情况下,普通的常量变量就像任何类型的变量,他们根本无法通过程序本身进行修改。

By default, plain const variables are just like any kind of variable, they simply can't be modified by the program itself.

正如对于普通变量,常量变量的行为在很大程度上取决于其范围时声明。通常它们在文件范围内声明,然后只是它们(可能)保存在内存中的不同部分他们的行为具有静态存储时间为其他变量。如果他们在局部范围内声明的,他们可能会改变从时间到的时候,他们所在的函数被调用时。

Just as for plain variables, const variable behavior depends a lot on in which scope they are declared. Most often they are declared at file scope and then they behave as other variables with static storage duration, except they are (likely) saved at a different part of the memory. If they are declared at local scope, they may change from time to time when the function where they reside is called.

所以,有很多在那里了const变量可以被优化的情况下。一个常见的​​优化是字符串池,在这里如果同样的常量字符串在code出现两次编译器检查,然后使用相同的地址他们。假如你希望这样的字符串从外部源更改,但没有宣布他们为挥发性,你会得到奇怪的错误。

So there are plenty of cases where const variables may be optimized. One common optimization is "string pools", where the compiler checks if the same constant string literal appears twice in the code, and then uses the same address for them. Had you expected such strings to be changed from an external source, but didn't declare them as volatile, you'd get strange bugs.

至于挥发性的变量,它们可以由外部来源修改,但它们也可以修改由程序,不像常量变量

As for volatile variables, they may be modified by external sources, but they may also be modified by the program, unlike const variables.

这篇关于常量与功放之间使用高度差;用C挥发性预选赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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