免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1622 | 回复: 3
打印 上一主题 下一主题

关于volatile的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-29 23:22 |只看该作者 |倒序浏览
今天看ANSI_C 99的时候看到一段:
Alternatively, an implementation might perform various optimizations within each translation unit, such
that the actual semantics would agree with the abstract semantics only when making function calls across
translation unit boundaries. In such an implementation, at the time of each function entry and function
return where the calling function and the called function are in different translation units, the values of all
externally linked objects and of all objects accessible via pointers therein would agree with the abstract
semantics. Furthermore, at the time of each such function entry the values of the parameters of the called
function and of all objects accessible via pointers therein would agree with the abstract semantics. In this
type of implementation, objects referred to by interrupt service routines activated by the signal function
would require explicit specification of volatile storage, as well as other implementation-defined
restrictions.

英语不太好,没明白abstract semantics和actual semantics具体指什么。对volatile的作用也不是很明白。
搜了下,说是告诉编译器不要放在寄存器,而是直接从对应内存中读。有些明白,但不确定。
而且回头再看上面那段时还是不着边绪,希望能有人解释下或给提示下...
先谢谢了...

[ 本帖最后由 zzdts 于 2007-3-29 23:30 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-03-29 23:40 |只看该作者
这段话我也看不太懂。
但是我帮你解释一下这个关键字(不一定说得很对)

通常对一个局部变量来说
int i;
如果编译器觉得你用的太频繁了,那么它会找一个空寄存器,以后当你引用i的时候,它都用这个寄存器。因为编译器知道你哪儿会用i所以它只要最后回写到i就可以了,不会影响程序的运行。这样优化能省很多个时钟。
但是这里做了一个假设,那就是,每次i的值都让编译器控制。

但是有些情况则不属于这种情况,比如i是个存储映射的io寄存器,想象它用32位线连接了别的地方,所以,即便是你

while(1)
printf("%d",i);
这个时候打印出来的值也不是固定不变的,很显然,因为程序不改变i它的值也会因为连线别的地方而改变,而且编译器根本不知道它会不会变,所以你如果要实时的打印i,i必须加这个关键字,

当然,i通常 会是个指针,而且初始化为某个已知的值的

论坛徽章:
0
3 [报告]
发表于 2007-03-30 00:04 |只看该作者
能不能这么说:
假设有一个程序 a,里面有 int i 这种东西,那么在另一个程序 b里,如果 b 有 访问 &a 的行为的话应该是会被阻止的。
但当这个 i 为 volatile i 的时候,b 访问 &a 就可以被认为是允许咯?

如果是这样的话,那么对 abstract semantics是不是可以理解为程序语义上的值,而 actual semantics 则是实际物理设备上的值。这样的话,那段话我理解是 出于某种目的两者的值是需要统一的,但当实际运行中,一个interrupt可能使得 actual 的值变化了,而程序引用的依然是 register里的值,这就使得 abstract和 actual变得不统一了,所以为保持统一性引入 volatile的存在。

不知道对不对?

论坛徽章:
0
4 [报告]
发表于 2007-03-30 00:17 |只看该作者
.

semantic 在语言学 (linguistics) 中表示语义。计算机程序设计语言里有很多是和(人类)语言学相通的。abstract semantics 和 actual semantics 分别代表抽象语义和实际语义。

abstract semantics,此处指经过转换(或翻译)前较为高级的语义。
actual semantics,这里说的是转换(或翻译)后抽象程度降低的语义。

.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP