gpt4 book ai didi

c - 为什么将函数参数标记为 volatile

转载 作者:太空狗 更新时间:2023-10-29 16:28:37 24 4
gpt4 key购买 nike

我目前正在阅读 PostgreSql代码。以下是缓冲区管理器的摘录:

static void WaitIO(volatile BufferDesc *buf);
static bool StartBufferIO(volatile BufferDesc *buf, bool forInput);
static void TerminateBufferIO(volatile BufferDesc *buf, bool clear_dirty,

我知道 volatile 关键字通常用于设备驱动程序和嵌入式系统。有关键字的解释。

When the keyword volatile is used in the type definition it is giving an indication to the compiler on how it should handle the variable. Primarily it is telling the compiler that the value of the variable may change at any time as a result of actions external to the program or current line of execution.(Source)

那么为什么某些函数参数被声明为 volatile 呢?我不希望 DMA 改变指针位置。那么这里发生了什么?

最佳答案

volatile BufferDesc *buf 表示buf指向的数据是易失的,而不是buf<包含的指针 是不稳定的。 (这将是 BufferDesc * volatile buf。)

来自 the page you linked to :

On the other hand, if you have a pointer variable where the address itself was volatile but the memory pointed to was not then we have:

int * volatile x;

关于你问题的这一部分:

So why are certain function arguments declared as volatile?

大概是因为它指向的数据可能会以编译器不一定知道的方式发生变化。 volatile 关键字用于防止编译器应用假设数据不会以它不知道的方式发生变化的优化。

关于c - 为什么将函数参数标记为 volatile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9363899/

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