在这个例子中挥发性的影响 [英] effect of volatile in this example

查看:74
本文介绍了在这个例子中挥发性的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void foo(void){

unsigned int * ptr =(unsigned int *)0x200;


* ptr = 4;


返回;

}


我在嵌入式环境中完成大部分工作所以我不是那么严格

熟悉所有的标准,所以可能设置一个指向

0x200的指针就像我做的那样是未定义的行为。如果没有,如果我声明

指针就像我没有使用volatile限定符一样,那么一致的

实现是否可以优化上面的赋值语句?


谢谢,


Josh

void foo(void) {
unsigned int *ptr = (unsigned int *)0x200;

*ptr = 4;

return;
}

I do most of my work in embedded environments so I''m not so strictly
familiar with all of the Standard, so maybe setting up a pointer to
0x200 like I did is undefined behavior. If not, can a conforming
implementation optmize the assignment statement above out if I declare
the pointer like I''ve done without the volatile qualifier?

Thanks,

Josh

推荐答案

2月1日,上午8:53,joshc < josh.cu ... @ gmail.comwrote:
On Feb 1, 8:53 am, "joshc" <josh.cu...@gmail.comwrote:

void foo(void){

unsigned int * ptr =(unsigned int *)0x200;


* ptr = 4;


返回;


}


我在嵌入式环境中完成大部分工作所以我不是那么严格

熟悉所有的标准,所以可能设置指针

0x200就像我做的那样是未定义的行为。如果没有,如果我声明

指针就像我没有使用volatile限定符一样,那么一致的

实现是否可以优化上面的赋值语句?


谢谢,


Josh
void foo(void) {
unsigned int *ptr = (unsigned int *)0x200;

*ptr = 4;

return;

}

I do most of my work in embedded environments so I''m not so strictly
familiar with all of the Standard, so maybe setting up a pointer to
0x200 like I did is undefined behavior. If not, can a conforming
implementation optmize the assignment statement above out if I declare
the pointer like I''ve done without the volatile qualifier?

Thanks,

Josh



我觉得没有什么不对,就Standrad而言关心。但是,

它确实依赖于平台(可能是地址0x200是由OS保留的
,所以你正在与内部进行交互)。不使用

Volatile也不会伤害。但是,如果你需要更多

信息,请详细说明执行此事件时发生的事情

程序。

谢谢,

Raman Chalotra

I Thing there is nothing wrong, as far as standrad is concerned. But,
it do have dependency on the Platform( may be address 0x200 is
reserved by OS, So you are interfaring with internals ). Not using
Volatile, is not going to harm either.But, If you need more
information, Describe exactly what happened on execution of this
program.
Thanks,
Raman Chalotra


joshc写道:
joshc wrote:

void foo(void){

unsigned int * ptr =(unsigned int *)0x200;


* ptr = 4;


返回;

}


我在嵌入式环境中完成大部分工作所以我不是所以严格地说明了所有标准的b $ b,所以也许设置一个指向

0x200的指针就像我做的那样是未定义的行为。如果没有,如果我声明

指针就像我没有使用volatile限定符一样,那么一致的

实现是否可以优化上面的赋值语句?
void foo(void) {
unsigned int *ptr = (unsigned int *)0x200;

*ptr = 4;

return;
}

I do most of my work in embedded environments so I''m not so strictly
familiar with all of the Standard, so maybe setting up a pointer to
0x200 like I did is undefined behavior. If not, can a conforming
implementation optmize the assignment statement above out if I declare
the pointer like I''ve done without the volatile qualifier?



只有当实施

可以确定它没有必要的副作用时,才能删除该分配。在这个

的情况下,将值4存储在引用的位置是一个副 -

效果,因此实现将以某种方式需要知道

副作用是不必要的,也就是说,储值

不会以任何方式影响程序的输出。由于该程序其他模块的一些

可能无法编译或

甚至写入后天,这类事情

需要一定程度的先见之明。至少在Carnac the Magnificent进入

编译器编写业务之前,你应该保证安全。


但是如果有的话关于位置0x200的一些特别之处 -

这是一个I / O寄存器或者其他一些 - 为什么不在
中使用`volatile''第一个放置并停止担心?这是它的目的之一,

,它符合你的需要,所以...确实,实现 -

定义的雾围绕着大多数用途` volatile'',包括这个

一个,但编译器编写者有动机做正确的

的事情。只要他们能够。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid

The assignment can be removed only if the implementation
can determine that it has no necessary side-effects. In this
case, storing the value 4 in the referenced location is a side-
effect, so the implementation would somehow need to know that
the side-effect is unnecessary, that is, that the stored value
cannot in any way influence the program''s output. Since some
of the other modules of the program might not be compiled or
even written until the day after tomorrow, that sort of thing
requires a degree of prescience not usually found. You should
be safe at least until Carnac the Magnificent goes into the
compiler-writing business.

But if there''s something special about the location 0x200 --
it''s an I/O register or some such -- why not use `volatile'' in
the first place and stop worrying? That''s one of its purposes,
and it fits your need, so ... It''s true that implementation-
defined fog surrounds most uses of `volatile'', including this
one, but compiler writers have an incentive to "do the right
thing" as far as they''re able.

--
Eric Sosman
es*****@acm-dot-org.invalid


" Eric Sosman" < es ***** @ acm-dot-org.invalidwrote in message

news:xa ********************* *********@comcast.com。 ..
"Eric Sosman" <es*****@acm-dot-org.invalidwrote in message
news:xa******************************@comcast.com. ..

你应该

至少在Carnac the Magnificent进入

编译器编写之前是安全的商业。
You should
be safe at least until Carnac the Magnificent goes into the
compiler-writing business.



如果卡纳克真的很棒,那就不应该有问题了。他/ b $ b应该能够确定所有副作用的影响。


-

David T. Ashley(dt *@e3ft.com)
http://www.e3ft.com (咨询主页)
http://www.dtashley.com (个人主页Page)
http://gpl.e3ft.com (GPL出版物和项目)

And if Carnac really is magnificent, there shouldn''t be a problem. He
should be able to determine the implications of all side-effects.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)


这篇关于在这个例子中挥发性的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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