博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 挥发注解_C中的挥发性预选赛
阅读量:2534 次
发布时间:2019-05-11

本文共 5429 字,大约阅读时间需要 18 分钟。

java 挥发注解

什么是C中的限定词? (What is Qualifiers in C?)

Qualifiers in C are the keywords which are used to modify the properties of a variable.There are two types of qualifiers available in C language. They are const and volatile.

C中的限定词是用于修改变量属性的关键字。C语言中有两种类型的限定词。 它们是const 和易失的

什么是const? (What is Const?)

Constants are normal variables. Once the value of a constant is defined, it cannot be modified later by the program. They refer to fixed values. They are also called as literals.

常数是普通变量。 一旦定义了常数的值,程序以后就不能修改它。 它们指的是固定值。 它们也称为文字。

Declaration:

宣言:

const data_type variable_name;const data_type *variable_name;

什么是挥发物? (What is Volatile?)

The value of a keyword defined as volatile cannot be changed explicitly by the program. But these variable values might keep on changing without any explicit assignment by the program. These types of qualifiers are called volatile.

程序无法显式更改定义为volatile的关键字的值。 但是这些变量值可能会不断变化,而无需程序进行任何明确的分配。 这些类型的限定词称为volatile。

Declaration:

宣言:

volatile data_type variable_name;volatile data_type *variable_name;

什么是C语言中的挥发性限定词? (What is Volatile Qualifier in C?)

Volatile Qualifier is a very important topic which needs to be understood by every embedded system programmer as well as an interview point of view. It is one of the vaguest and not clearly understood topic.

挥发性限定词是一个非常重要的主题,每个嵌入式系统程序员都需要理解它,并需要接受访谈的观点。 这是最模糊且不清楚的话题之一。

Have you ever faced this issue, when you are testing your code independently or without any compiler optimization, it works perfectly fine but as soon as you integrate it with system code or perform compiler optimization, the things start to crash and no longer provides the same output?

您是否曾经遇到过这个问题,当您独立地测试代码或不进行任何编译器优化时,它可以正常工作,但是一旦将其与系统代码集成或执行编译器优化,事情就会开始崩溃,不再提供相同的功能输出?

This might be due to the volatile qualifier. It’s not so straight forward to have a sample working C program which can easily showcase the exact effect of “volatile” keyword. You probably won’t be able to use a variable which is qualified as “volatile“ unless you’re doing some low-level hardware programming in C.

这可能是由于易变的限定词。 拥有一个可以正常显示“ volatile”关键字的确切效果的示例工作C程序并不是那么简单。 除非您在C中进行一些底层硬件编程,否则您可能将无法使用被定义为“易失性”的变量。

A volatile variable is one that can change unexpectedly, it means the compiler cannot make any assumption about its value.

volatile变量是可以意外更改的变量,这意味着编译器无法对其值进行任何假设。

Volatile keyword is intended to prevent the compiler to perform any optimization on that object, that can change in ways which cannot be determined by compiler.

Volatile关键字旨在防止编译器对该对象执行任何优化,而优化可能会以编译器无法确定的方式改变。

The object can be a memory, register, SFR.

该对象可以是存储器,寄存器SFR。

The picture shows the ways through which objects can be updated.

该图显示了可以更新对象的方式。

volatile qualifiers in C, updation of the volatile variable

OBJECT UPDATION

对象更新

何时使用挥发性限定剂? (When to use Volatile Qualifier?)

Let’s try to understand this with a simple example.

让我们尝试通过一个简单的例子来理解这一点。

Say, we have a memory location and we need to continuously monitor it until it becomes zero.

假设我们有一个内存位置,我们需要对其进行连续监视,直到它变为零为止。

Here is the code:

这是代码:

#define MEMORY_ADDRESS 0x1000Uint main(void){    int value = 0;    int*p = (int*)MEMORY_ADDRESS;    while(1)    {        value = *p;        if(value) break;    }    return 0;}

We had defined memory location with macro(MEMORY_ADDRESS) and a pointer which point to this address. In the while(1) loop we are monitoring the value pointed by the pointer p. If at any point of time the value becomes non zero we will break the loop.

我们已经使用宏(MEMORY_ADDRESS)和指向该地址的指针定义了内存位置。 在while(1)循环中,我们正在监视指针p指向的值。 如果在任何时间点该值都不为零,我们将中断循环。

This simple piece of code works perfectly fine if there is no optimization done by the compiler. It will always go and read what is the value stored at the address pointed by pointer p.

如果编译器没有进行任何优化,那么这段简单的代码可以很好地工作。 它将总是去读取指针p指向的地址中存储的值。

Now, if you apply compiler optimization here and the value is updated by the interrupt or DMS which is not under compiler’s scope. Then compiler optimizes it by keeping a copy of the first read to avoid multiple read operation which is time-consuming. In that case, even if the value at this address changes but it will never be updated here.

现在,如果您在此处应用编译器优化,并且该值由中断或不在编译器范围内的DMS更新。 然后,编译器通过保留第一个读取的副本来对其进行优化,以避免耗时的多次读取操作。 在这种情况下,即使该地址的值发生变化,也不会在此更新。

To avoid such situation we need to explicitly tell the compiler that the pointer p is volatile and don’t perform any optimization on that.

为了避免这种情况,我们需要明确地告诉编译器指针p是易失的,并且不对该指针进行任何优化。

By the thumb rule, anything which can change beyond the compilers scope you should declare it as “volatile”.

根据经验法则,任何可能超出编译器范围的更改都应将其声明为“易失性”。

过度使用挥发物的后果 (Consequences of over usage of volatile)

Volatile variable forces compiler not to keep a copy and always get the fresh value from memory and register which takes more clock cycles. For real-time application, you should always perform timing analysis on a volatile variable to check it fulfills the timing requirement.

易失性变量迫使编译器不保留副本,并始终从内存和寄存器中获取新值,这需要更多的时钟周期。 对于实时应用程序,您应始终对易失性变量执行时序分析,以检查其是否满足时序要求。

Now at last but not least, there is one interesting thing that we would like to share. Consider the code below:

现在,最后但并非最不重要的一点是,我们想分享一件事。 考虑下面的代码:

const volatile int Variable;

Can you guess what this variable means? How it can be constant and variable at the same time?

你能猜出这个变量是什么意思吗? 如何同时保持不变和可变?

It means it’s a volatile integer that you are not allowed to write but it can be changed by the outside world. You can use this for read-only or status register.

这意味着它是一个不允许写入的易失性整数,但外界可以更改它。 您可以将其用于只读或状态寄存器。

翻译自:

java 挥发注解

转载地址:http://rdlzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>