挥发性和“需要的副作用” [英] volatile and "needed side effects"

查看:73
本文介绍了挥发性和“需要的副作用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99标准规定:

"在抽象机器中,所有表达式按语义评估为指定

。实际实现不需要评估表达式的部分

,如果它可以推断出它的值没有被使用,并且

不会产生所需的副作用(包括由<引起的任何副作用) br />
调用函数或访问volatile对象。)

这是否意味着在下面的代码中,* p不需要

是否因为其副作用并非真正需要而被评估?:


extern volatile int * p;


int main(void)

{

* p;

返回0;

}

解决方案



di ** ***********@aol.com 在10月10日15:26写道:

C99标准声明:
"在抽象机器中,所有表达式都按语义评估为
。实际实现不需要评估表达式的部分,如果它可以推断出它的值没有被使用,并且没有产生所需的副作用(包括由调用函数或访问引起的任何副作用)一个易变的对象)。

这是否意味着在下面的代码中,* p不需要进行评估,因为它的副作用并不是真正需要的?:

extern volatile int * p;

int main(void)
{
* p;
返回0;
}



恰恰相反:由于'p''指向'int'',因为
是易变的,并且因为访问易失性对象是

副作用,`* p''必须进行评估 - 或者无论如何,

必须执行它所暗示的访问权限。例如,这段代码可能是一个名为IgnoreStatus的程序的一部分,它的整个目的是取消一个拒绝
做任何事情,直到某人阅读了内存映射状态寄存器的内容。


-
Er*********@sun.com


Eric Sosman< er ********* @ sun.com>写道:

di ************ *@aol.com 写道On 10/10/05 15:26,:

C99标准规定:
"在抽象机器中,所有表达式都被评估为由语义指定
。实际实现不需要评估表达式的部分,如果它可以推断出它的值没有被使用,并且没有产生所需的副作用(包括由调用函数或访问引起的任何副作用)一个易变的对象)。

这是否意味着在下面的代码中,* p不需要进行评估,因为它的副作用并不是真正需要的?:

extern volatile int * p;

int main(void)
{
* p;
返回0;
}


恰恰相反:由于p指向int,
是易变的,并且因为访问易失性对象是一种副作用, * p''必须进行评估 - 或者无论如何,
必须执行它所暗示的访问。例如,这段代码可能是名为IgnoreStatus的程序的一部分,该程序的全部目的是解开一个拒绝做任何事情的设备,直到它为止。有人读了它的内存映射状态寄存器。




正确 - 除非编译器知道访问* p并不是

实际上具有所需的副作用。但是,由于用户声明它是不稳定的,因此用户不会为了证明这一点而付出代价,特别是因为它可能是错的。


-

Keith Thompson(The_Other_Keith) ks * **@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。





Keith Thompson写道On 10/10/05 16:31,:

Eric Sosman< er ********* @ sun.com>写道:

di ************* @ aol.com写道On 10/10/05 15:26,:

C99标准规定:
"在抽象机器中,所有表达式都按语义评估为指定的。实际实现不需要评估表达式的部分,如果它可以推断出它的值没有被使用,并且没有产生所需的副作用(包括由调用函数或访问引起的任何副作用)一个易变的对象)。

这是否意味着在下面的代码中,* p不需要进行评估,因为它的副作用并不是真正需要的?:

extern volatile int * p;

int main(void)
{
* p;
返回0;
}


恰恰相反:由于p指向int,
是易变的,并且因为访问易失性对象是一种副作用, * p''必须进行评估 - 或者无论如何,
必须执行它所暗示的访问。例如,这段代码可能是名为IgnoreStatus的程序的一部分,该程序的全部目的是解开一个拒绝做任何事情的设备,直到它为止。有人读了它的内存映射状态寄存器。



正确 - 除非编译器知道访问* p实际上没有需要的副作用。但是由于用户没有按照自己的方式声明它是不稳定的,所以编译器不太可能去努力证明这一点,特别是因为它可能是错误的。




对(回到'')。然而,以可移植的方式对易失性的理由相当困难。该标准要求

,抽象机器必须进行的所有访问必须实际制作
,但由于实现得到定义

什么" ;接入"意思是......


-
Er **** *****@sun.com


The C99 standard states:
"In the abstract machine, all expressions are evaluated as specified
by the semantics. An actual implementation need not evaluate part
of an expression if it can deduce that its value is not used and
that no needed side effects are produced (including any caused by
calling a function or accessing a volatile object)."
Does that mean that in the following code, *p does not have to
be evaluated since its side effects are not truly needed?:

extern volatile int *p;

int main(void)
{
*p;
return 0;
}

解决方案



di*************@aol.com wrote On 10/10/05 15:26,:

The C99 standard states:
"In the abstract machine, all expressions are evaluated as specified
by the semantics. An actual implementation need not evaluate part
of an expression if it can deduce that its value is not used and
that no needed side effects are produced (including any caused by
calling a function or accessing a volatile object)."
Does that mean that in the following code, *p does not have to
be evaluated since its side effects are not truly needed?:

extern volatile int *p;

int main(void)
{
*p;
return 0;
}



Just the opposite: Since `p'' points to an `int'' that
is volatile, and since accessing a volatile object is a
side-effect, `*p'' must be evaluated -- or at any rate,
the access it implies must be performed. This code might,
for example, be part of a program named IgnoreStatus, whose
entire purpose is to un-wedge a device that''s refusing to
do anything until it''s been tickled by somebody reading
its memory-mapped status register.

--
Er*********@sun.com


Eric Sosman <er*********@sun.com> writes:

di*************@aol.com wrote On 10/10/05 15:26,:

The C99 standard states:
"In the abstract machine, all expressions are evaluated as specified
by the semantics. An actual implementation need not evaluate part
of an expression if it can deduce that its value is not used and
that no needed side effects are produced (including any caused by
calling a function or accessing a volatile object)."
Does that mean that in the following code, *p does not have to
be evaluated since its side effects are not truly needed?:

extern volatile int *p;

int main(void)
{
*p;
return 0;
}



Just the opposite: Since `p'' points to an `int'' that
is volatile, and since accessing a volatile object is a
side-effect, `*p'' must be evaluated -- or at any rate,
the access it implies must be performed. This code might,
for example, be part of a program named IgnoreStatus, whose
entire purpose is to un-wedge a device that''s refusing to
do anything until it''s been tickled by somebody reading
its memory-mapped status register.



Right -- unless the compiler knows somehow that accessing *p doesn''t
actually have a "needed side effect". But since the user went out of
his way to declare it volatile, it''s unlikely that a compiler would go
to the effort to prove this, especially since it might be wrong.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




Keith Thompson wrote On 10/10/05 16:31,:

Eric Sosman <er*********@sun.com> writes:

di*************@aol.com wrote On 10/10/05 15:26,:

The C99 standard states:
"In the abstract machine, all expressions are evaluated as specified
by the semantics. An actual implementation need not evaluate part
of an expression if it can deduce that its value is not used and
that no needed side effects are produced (including any caused by
calling a function or accessing a volatile object)."
Does that mean that in the following code, *p does not have to
be evaluated since its side effects are not truly needed?:

extern volatile int *p;

int main(void)
{
*p;
return 0;
}



Just the opposite: Since `p'' points to an `int'' that
is volatile, and since accessing a volatile object is a
side-effect, `*p'' must be evaluated -- or at any rate,
the access it implies must be performed. This code might,
for example, be part of a program named IgnoreStatus, whose
entire purpose is to un-wedge a device that''s refusing to
do anything until it''s been tickled by somebody reading
its memory-mapped status register.


Right -- unless the compiler knows somehow that accessing *p doesn''t
actually have a "needed side effect". But since the user went out of
his way to declare it volatile, it''s unlikely that a compiler would go
to the effort to prove this, especially since it might be wrong.



Right (back at''cha). It is, however, rather difficult to
reason about volatile in a portable way. The Standard requires
that all the accesses the abstract machine would make must
actually be made, but since the implementation gets to define
what "access" means ...

--
Er*********@sun.com


这篇关于挥发性和“需要的副作用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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