是否使用“指向 volatile 的指针"?始终阻止编译器优化? [英] Does using "pointer to volatile" prevent compiler optimizations at all times?

查看:34
本文介绍了是否使用“指向 volatile 的指针"?始终阻止编译器优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题来了:你的程序暂时使用一些敏感数据并希望在不再需要时将其删除.对自身使用 std::fill() 并不总是有帮助 - 编译器可能会决定 稍后不访问内存块,因此擦除它是浪费时间并消除擦除代码.

Here's the problem: your program temporarily uses some sensitive data and wants to erase it when it's no longer needed. Using std::fill() on itself won't always help - the compiler might decide that the memory block is not accessed later, so erasing it is a waste of time and eliminate erasing code.

用户 ybungalobill 建议 使用 volatile 关键字:

User ybungalobill suggests using volatile keyword:

{
  char buffer[size];
  //obtain and use password
  std::fill_n( (volatile char*)buffer, size, 0);
}

目的是在看到 volatile 关键字后,编译器不会尝试消除对 std::fill_n() 的调用.

The intent is that upon seeing the volatile keyword the compiler will not try to eliminate the call to std::fill_n().

volatile 关键字是否总是会阻止编译器消除这种内存修改代码?

Will volatile keyword always prevent the compiler from such memory modifying code elimination?

推荐答案

编译器可以自由优化您的代码因为buffer 不是易失性对象.

The compiler is free to optimize your code out because buffer is not a volatile object.

标准只要求编译器严格遵守易失性对象的语义.这是 C++03 所说的

The Standard only requires a compiler to strictly adhere to semantics for volatile objects. Here is what C++03 says

对一致实现的最低要求是:

The least requirements on a conforming implementation are:

  • 在序列点,易失性对象是稳定的,因为之前的评估是完整的并且尚未进行后续评估.[...]

抽象机的可观察行为是它对易失性数据的读写顺序和调用库 I/O 函数

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions

在您的示例中,您所拥有的是使用易失性左值对非易失性对象进行读写.C++0x 删除了我上面引用的第二个文本,因为它是多余的.C++0x 只是说

In your example, what you have are reads and writes using volatile lvalues to non-volatile objects. C++0x removed the second text I quoted above, because it's redundant. C++0x just says

对一致实现的最低要求是:

The least requirements on a conforming implementation are:

  • 对 volatile 对象的访问严格按照抽象机器的规则进行评估.[...]

这些统称为程序的可观察行为.

虽然有人可能会争辩说易失性数据"可能意味着易失性左值访问的数据",这仍然是一个很大的问题,但 C++0x 的措辞消除了对您的代码的所有疑虑,并清楚地允许实现对其进行优化离开.

While one may argue that "volatile data" could maybe mean "data accessed by volatile lvalues", which would still be quite a stretch, the C++0x wording removed all doubts about your code and clearly allows implementations to optimize it away.

但正如人们向我指出的那样,这在实践中可能无关紧要.优化这种事情的编译器很可能会违背程序员的意图(为什么有人会有一个指向 volatile 的指针),因此可能会包含一个错误.尽管如此,我还是遇到过编译器供应商在面对关于他们过度激进优化的错误报告时引用这些段落的经历.最后, volatile 是固有的平台特定的,无论如何你都应该仔细检查结果.

But as people pointed out to me, It probably does not matter in practice. A compiler that optimizes such a thing will most probably go against the programmers intention (why would someone have a pointer to volatile otherwise) and so would probably contain a bug. Still, I have experienced compiler vendors that cited these paragraphs when they were faced with bugreports about their over-aggressive optimizations. In the end, volatile is inherent platform specific and you are supposed to double check the result anyway.

这篇关于是否使用“指向 volatile 的指针"?始终阻止编译器优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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