常量与放大器之间的差异;常量挥发性 [英] Difference between const & const volatile

查看:97
本文介绍了常量与放大器之间的差异;常量挥发性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们声明一个变量挥发性每一个新鲜值更新时间结果
如果一个变量声明为常量那么该变量的值不会改变。

If we declare a variable as volatile every time the fresh value is updated
If we declare a variable as const then the value of that variable will not be changed

然后 const的挥发INT温度; 结果
有什么用声明变量温度如上?结果的
如果我们声明为会发生什么 const int的温度

Then const volatile int temp;
What is the use of declaring the variable temp as above?
What happens if we declare as const int temp?

推荐答案

这是对象标记为 const的挥发将不会被允许由code更改(一个错误会因提高到常量预选赛) - 至少在那个特定的名称/指针

An object marked as const volatile will not be permitted to be changed by the code (an error will be raised due to the const qualifier) - at least through that particular name/pointer.

挥发性预选赛的一部分,也就是说,编译器不能优化或重新排序访问对象。

The volatile part of the qualifier means that the compiler cannot optimize or reorder access to the object.

在嵌入式系统中,这通常用于访问可以读取和由硬件更新硬件寄存器,但没有任何意义写(或可能写入错误)。

In an embedded system, this is typically used to access hardware registers that can be read and are updated by the hardware, but make no sense to write to (or might be an error to write to).

这是实施例可能是一个串行端口的状态寄存器。各个位将表明,如果一个字符正在等待被读取或发送寄存器已准备好接受一个新的字符(即 - 它是空的)。此状态寄存器的每个读,可能会导致不同的值取决于什么其他发生在串行端口硬件

An example might be the status register for a serial port. Various bits will indicate if a character is waiting to be read or if the transmit register is ready to accept a new character (ie., - it's empty). Each read of this status register could result in a different value depending on what else has occurred in the serial port hardware.

这是没有意义的写状态寄存器(根据具体的硬件规格),但你需要确保每个硬件的实际读取寄存器结果读 - 使用从$缓存值p $ pvious读不会告诉你有关的硬件状态的变化。

It makes no sense to write to the status register (depending on the particular hardware spec), but you need to make sure that each read of the register results in an actual read of the hardware - using a cached value from a previous read won't tell you about changes in the hardware state.

一个简单的例子:

unsigned int const volatile *status_reg; // assume these are assigned to point to the 
unsigned char const volatile *recv_reg;  //   correct hardware addresses


#define UART_CHAR_READY 0x00000001

int get_next_char()
{
    while ((*status_reg & UART_CHAR_READY) == 0) {
        // do nothing but spin
    }

    return *recv_reg;
}

如果这些指针不标记为挥发性,可能出现一对夫妇的问题:

If these pointers were not marked as being volatile, a couple problems might occur:


  • while循环测试可能读取状态寄存器只有一次,因为编译器可以假设无论它指向永远不会改变(有没有在while循环测试或循环本身可能会改变它)。如果你在没有字符UART硬件等待进入的功能,你可能会在一个无限循环,当接收到一个字符甚至从来没有停止结束。

  • 接收寄存器的读取可以由编译器while循环前移到 - 再次因为没有什么的功能,表明<​​code> * recv_reg 被改变环,没有理由不能进入循环前阅读。

  • the while loop test might read the status register only once, since the compiler could assume that whatever it pointed to would never change (there's nothing in the while loop test or loop itself that could change it). If you entered the function when there was no character waiting in UART hardware, you might end up in an infinite loop that never stopped even when a character was received.
  • the read of the receive register could be moved by the compiler to before the while loop - again because there's nothing in the function that indicates that *recv_reg is changed by the loop, there's no reason it can't be read before entering the loop.

挥发性预选赛确保这些优化不是由编译器执行。

The volatile qualifiers ensures that these optimizations are not performed by the compiler.

这篇关于常量与放大器之间的差异;常量挥发性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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