波动性变量

发布于 2024-08-10 16:42:14 字数 33 浏览 4 评论 0原文

易失性变量存储在程序存储器的什么位置(在哪个部分)?

Where is a volatile variable stored in the program memory (in which section)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

还给你自由 2024-08-17 16:42:14

易失性是一个类型限定符< /a> 不是存储类说明符,因此它无法确定存储位置根本;它影响变量类型的定义,而不是其存储。

它只是强制编译器从变量的存储位置(无论在哪里)显式读取类型易失性的变量,而不是假设之前读取了寄存器中的某些值例如仍然有效。

volatile is a type qualifier not a storage class specifier, so it does not determine storage location at all; it affects the definition of a variable's type, not its storage.

It simply forces the compiler to explicitly read a variable whose type is volatile from the variable's storage location (wherever that may be) rather than assuming that some previously read value in a register for example remains valid.

尾戒 2024-08-17 16:42:14

在 C 中,易失性只是告诉编译器——“你没有足够的知识来假设这个变量的值没有改变”。没有“部分”,例如 BSS、CSS。

将其视为编译器的一个标志,以防止某些类型的优化。它在嵌入式编程中非常方便,其中某个地址的内存可能会由于硬件设备输入而“改变”。

这是一个很好的解释: http://www.embedded.com/columns/programmingpointers/ 174300478?_requestid=137658

In C volatile just tells the compiler - "You don't have enough knowledge to assume the value of this variable hasn't changed". There is no "section" eg BSS, CSS for it.

Consider it a flag to the compiler to prevent certain types of optimisations. Its very handy in embedded programming, where memory at a certain address may "change" due to a hardware device input.

Here's a good explanation: http://www.embedded.com/columns/programmingpointers/174300478?_requestid=137658

一张白纸 2024-08-17 16:42:14

变量的波动性不会改变变量的存储位置。它改变的是有关读取和写入的访问方式的语义。

我不相信 C 标准对 volatile 的实现有任何说明。但通常情况下,易失性保证变量写操作的释放语义,并获取变量读操作的语义。但这并不适用于每个实现,您应该阅读特定编译器的保证

The volatility of a variable does not change the place in which a variable is stored. What it changes is the semantics around how it is accessed with respect to reads and writes.

I do not believe the C standard says anything about the implementation of volatile. But typically, volatile guarantees release semantics for write operations on a variable and aquire semantics on read operations of a variable. This will not be true for every implementation though and you should read up on what your specific compiler guarantees

过期情话 2024-08-17 16:42:14

易失性与存储类别无关。

易失性只是告诉编译器或强制编译器“不对该变量进行优化”。
因此编译器不会优化该变量的代码并从指定位置读取值,而不是通过保存先前值的内部寄存器。

因此,通过将变量声明为 volatile.. 它可以保证您将获得最新值,该值可能会被外部事件更改。

如果没有将该变量声明为易失性,您的代码可能会正常工作,但有时可能无法获得正确的值。
因此,为了避免这种情况,我们应该将变量声明为 volatile。

易失性通常在处理外部事件时使用,例如硬件相关引脚的中断。

例子。
读取 adc 值。

const voltile 意味着
您无法在代码中修改或更改该变量的值。只有外部事件可以改变该值。

控制器引脚通常定义为易失性。
可能是通过声明变量作为易失性控制器将执行“通过引脚读取”而不是“通过锁存器读取”......这是我的假设。可能是错误的......

但是当选择变量作为 volatile 时仍然存在很多混乱。

每当变量的值可能意外改变时,就应该将变量声明为 volatile。实际上,只有三种类型的变量可以更改:

  1. 内存映射外设寄存器

  2. 由中断服务例程修改的全局变量

  3. 多线程应用程序中的全局变量

链接:http://eetimes.com/discussion/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword

因此提供了在这种情况下,将变量设置为 volatile。

volatile has nothing to deal with storage class.

volatile just tells the compiler or force the compiler to "not to do the optimization" for that variable.
so compiler would not optimize the code for that variable and reading the value from the specified location, not through interal register which holds the previous value.

So, by declaring variable as volatile.. it gives garrantee that you will get the latest value, which may be alterred by an external event.

your code may be work fine if haven't declare that variable as volatile, but there may be chance of not getting correct value sometimes..
so to avoid that we should declare variable as volatile.

volatile is generally used when dealing with external events, like interrupts of hardware related pins.

example.
reading adc values.

const voltile means
you can not modify or alter the value of that variable in code. only external event can change the value.

controller pins are generally defines as volatile.
may be by declaring variable as volatile controller will do "read by pin" not "read by latch"... this is my assumtion. may be wrong...

but still there is lots of confusion when to choose variable as volatile..

A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:

  1. Memory-mapped peripheral registers

  2. Global variables modified by an interrupt service routine

  3. Global variables within a multi-threaded application

Link : http://eetimes.com/discussion/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword

So It is proffered to variable as volatile in such cases.

最美不过初阳 2024-08-17 16:42:14

没有理由将易失性变量存储在内存的任何“特殊”部分中。它通常与任何其他变量(包括非易失性变量)存储在一起。如果某个编译器决定将易失性变量存储在内存的某个特殊部分 - 没有什么可以阻止它这样做。但在语言层面上,这是完全没有理由的。

你为什么问这样的问题?是什么让您认为它应该存储在内存的某个特殊部分?

There's no reason for a volatile variable to be stored in any "special" section of memory. It is normally stored together with any other variables, including non-volatile ones. If some compiler decides to store volatile variables in some special section of memory - there's nothing to prevent it from doing so. But at the language level there's absolutely no reason for this.

Why are you asking such a question? What made you think that it should be stored in some special section of memory?

木槿暧夏七纪年 2024-08-17 16:42:14

C/C++ 规范中使用“易失性”来允许使用内存映射设备。它指示编译器不要优化使用此关键字定义的变量,只是因为该变量似乎没有在编译器可见的代码中更改其状态。

"Volatile" was used in C/C++ specifications to allow use of memory mapped devices. It directs the compiler not to optimize the variable defined with this keyword, just because the variable doesn't seem to change its state in compiler-visible code.

行雁书 2024-08-17 16:42:14

使用 longjmp() 时,所有在 setjump() 之前定义的自动变量和寄存器变量,以及在 setjmp()longjmp( ) 将恢复为 setjmp() 发生之前保存的值,即“回滚”,以防止您将自动变量声明为 易失性

BN 仅当您告诉编译器进行优化时才会发生这种情况

when using longjmp() all the auto and register variables defined before setjump(), and changed between setjmp() and longjmp() will be restored to the values they held before setjmp() took place e.i "roll back", to prevent that you declare your auto variables as volatile.

BN this only happens when you tell the compiler to do its optimizations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文