挥发性和“需要的副作用”

编程入门 行业动态 更新时间:2024-10-27 19:29:09
本文介绍了挥发性和“需要的副作用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

C99标准规定: "在抽象机器中,所有表达式按语义评估为指定 。实际实现不需要评估表达式的部分 ,如果它可以推断出它的值没有被使用,并且 不会产生所需的副作用(包括由 调用函数或访问volatile对象。) 这是否意味着在下面的代码中,* p不需要 是否因为其副作用并非真正需要而被评估?: extern volatile int * p; int main(void) { * p; 返回0; }

解决方案

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

C99标准声明:"在抽象机器中,所有表达式都按语义评估为。实际实现不需要评估表达式的部分,如果它可以推断出它的值没有被使用,并且没有产生所需的副作用(包括由调用函数或访问引起的任何副作用)一个易变的对象)。 这是否意味着在下面的代码中,* p不需要进行评估,因为它的副作用并不是真正需要的?: extern volatile int * p; int main(void) { * p; 返回0; } 恰恰相反:由于'p''指向'int'',因为是易变的,并且因为访问易失性对象是 副作用,`* p''必须进行评估 - 或者无论如何, 必须执行它所暗示的访问权限。例如,这段代码可能是一个名为IgnoreStatus的程序的一部分,它的整个目的是取消一个拒绝 做任何事情,直到某人阅读了内存映射状态寄存器的内容。 - Er*********@sun

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

di ************ *@aol 写道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 < www.ghoti/~kst> 圣地亚哥超级计算机中心< *> < users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。

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

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

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

C99标准规定:"在抽象机器中,所有表达式都按语义评估为指定的。实际实现不需要评估表达式的部分,如果它可以推断出它的值没有被使用,并且没有产生所需的副作用(包括由调用函数或访问引起的任何副作用)一个易变的对象)。 这是否意味着在下面的代码中,* p不需要进行评估,因为它的副作用并不是真正需要的?: extern volatile int * p; int main(void) { * p; 返回0; } 恰恰相反:由于p指向int,是易变的,并且因为访问易失性对象是一种副作用, * p''必须进行评估 - 或者无论如何,必须执行它所暗示的访问。例如,这段代码可能是名为IgnoreStatus的程序的一部分,该程序的全部目的是解开一个拒绝做任何事情的设备,直到它为止。有人读了它的内存映射状态寄存器。

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

对(回到'')。然而,以可移植的方式对易失性的理由相当困难。该标准要求 ,抽象机器必须进行的所有访问必须实际制作,但由于实现得到定义 什么" ;接入"意思是...... - Er **** *****@sun

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 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

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

di*************@aol 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 <www.ghoti/~kst> San Diego Supercomputer Center <*> <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> writes:

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

The C99 standard states:"In the abstract machine, all expressions are evaluated as specifiedby the semantics. An actual implementation need not evaluate partof an expression if it can deduce that its value is not used andthat no needed side effects are produced (including any caused bycalling a function or accessing a volatile object)." Does that mean that in the following code, *p does not have tobe 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'' thatis volatile, and since accessing a volatile object is aside-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, whoseentire purpose is to un-wedge a device that''s refusing todo anything until it''s been tickled by somebody readingits 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

更多推荐

挥发性和“需要的副作用”

本文发布于:2023-05-27 19:06:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/299895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:挥发性   副作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!