gpt4 book ai didi

c++ - 给优化器更大的 "License"

转载 作者:太空狗 更新时间:2023-10-29 20:36:56 24 4
gpt4 key购买 nike

我知道 volatile 变量背后的概念。基本上,对该变量的所有读取和写入操作都必须 发生。有没有一种方法可以让优化器仅执行所有写入并假设读取将始终保持不变(除非通过写入进行修改)。

另外(沿着同一行)有一种方法可以为编译器定义一种新的内存类型来存储变量。例如,如果我有一个带 SD 卡的微 Controller ,我能否将 SD 卡定义为存储内存的地方(或者我是否明确必须自己完成所有读/写操作)。

郑重声明,我使用 gcc 作为我的编译器,如果有任何我可以专门(且仅)在 gcc 上做的事情的话

最佳答案

I know the concept behind volatile variables.

好的...

Basically all reads and writes to that variable must occur.

绝对不是“基本上”。

Is there a way to allow the optimizer to get away with only doing all of the writes and assuming that the reads will always stay the same (unless modified by writing).

没有

<- 剪断 ->

volatile 用于模拟对内存映射 I/O 的读/写访问。 “写入”此类 I/O 通常会触发电子设备中的事件,即使写入的值与之前写入的值相同。

volatile 没有其他用途 - 不,甚至在多线程中也没有(无论如何它都不会做你想做的事)。

来自§7.1.6.1

[ Note: volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation. Furthermore, for some implementations, volatile might indicate that special hardware instructions are required to access the object. See 1.9 for detailed semantics. In general, the semantics of volatile are intended to be the same in C++ as they are in C. — end note ]

这里的含义是,除非您确切了解实现对 volatile 变量的作用,否则您没有地方可以使用它。

它的使用是不可移植的,所以如果使用的话,应该包含在您正在处理的任何概念的特定于实现的特化中。

关于c++ - 给优化器更大的 "License",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36657121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com