如何声明使用 malloc 创建的数组在 c++ 中是易失性的
我认为以下内容会给我 10 个易失性整数
volatile int foo[10];
但是,我不认为以下内容会做同样的事情。
volatile int* foo;
foo = malloc(sizeof(int)*10);
如果我对此以及如何使用 malloc 获得易失性项目数组有误,请纠正我。
谢谢。
I presume that the following will give me 10 volatile ints
volatile int foo[10];
However, I don't think the following will do the same thing.
volatile int* foo;
foo = malloc(sizeof(int)*10);
Please correct me if I am wrong about this and how I can have a volatile array of items using malloc.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从右向左读“foo 是一个指向 易失性 int 的指针”,
因此无论您通过 foo 访问什么 int,该 int 都将是易失性的。
PS
!=
意思 foo 是不稳定的。第二种情况实际上只是一般从右到左规则的遗留。
要吸取的教训是养成使用
而不是更常见的
习惯,如果您想要更复杂的东西,例如“指向函数的指针返回指向 int 的指针”,以使其有意义。
PS,这是一个大问题(也是我添加答案的主要原因):
我注意到您将“多线程”作为标签包含在内。您是否意识到 volatile 对于多线程几乎没有什么好处?
read from right to left "foo is a pointer to a volatile int"
so whatever int you access through foo, the int will be volatile.
P.S.
!=
Meaning foo is volatile. The second case is really just a leftover of the general right-to-left rule.
The lesson to be learned is get in the habit of using
instead of the more common
If you want more complicated things like "pointer to function returning pointer to int" to make any sense.
P.S., and this is a biggy (and the main reason I'm adding an answer):
I note that you included "multithreading" as a tag. Do you realize that volatile does little/nothing of good with respect to multithreading?
是要走的路。 易失性类型限定符的工作方式与const类型限定符类似。如果您想要一个指向整数常量数组的指针,您可以编写:
而 是
指向本身可以更改的整数的常量指针。 易失性的工作原理相同。
is the way to go. The volatile type qualifier works just like the const type qualifier. If you wanted a pointer to a constant array of integer you would write:
whereas
is a constant pointer to an integer that can itself be changed. volatile works the same way.
是的,那会起作用的。实际的
易失性
内存没有什么不同。它只是告诉编译器如何与该内存交互的一种方法。Yes, that will work. There is nothing different about the actual memory that is
volatile
. It is just a way to tell the compiler how to interact with that memory.我认为第二个声明指针是易失性的,而不是它指向的内容。为此,我认为应该是
这种语法可以被
gcc
接受,但我遇到了麻烦说服自己它做了任何不同的事情。我发现了与 gcc -O3 的区别(全面优化)。对于这个(愚蠢的)测试代码:
使用
易失性
,并省略不会改变的(x86)指令:没有易失性,它会跳过重新加载
p
:经过更多的科学实验试图找出差异,我的结论是没有差异。易失性关闭与变量相关的所有优化,该变量将重用随后设置的值。至少使用 x86 gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)。 :-)
I think the second declares the pointer to be volatile, not what it points to. To get that, I think it should be
This syntax is acceptable to
gcc
, but I'm having trouble convincing myself that it does anything different.I found a difference with
gcc -O3
(full optimization). For this (silly) test code:With
volatile
, and omitting (x86) instructions which don't change:Without volatile, it skips reloading
p
:After many more science experiments trying to find a difference, I conclude there is no difference.
volatile
turns off all optimizations related to the variable which would reuse a subsequently set value. At least with x86 gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33). :-)非常感谢 wallyk,我能够使用他的方法设计一些代码来生成一些程序集,以向自己证明不同指针方法之间的差异。
使用代码:并使用 -03 进行编译,
当 p 被简单地声明为指针时,我们陷入了一个无法摆脱的循环。请注意,如果这是一个多线程程序并且另一个线程写入 p[2] = 0,则程序将跳出 while 循环并正常终止。
请注意,L6 的唯一指令是转到 L6。
==
当 p 在这里是易失性指针时
,指针 p 在每次循环迭代时都会重新加载,因此数组项也会重新加载。但是,如果我们想要一个易失性整数数组,则这是不正确的,因为这是可能的:
并且会导致循环在多线程程序中无法终止。
==
最后,这是正确的解决方案,正如托尼很好地解释的那样。
在这种情况下,p[2] 的地址保存在寄存器值中,而不是从内存中加载,但 p[2] 的值会在每个循环周期从内存中重新加载。
另请注意,这
会产生编译错误。
Thanks very much to wallyk, I was able to devise some code use his method to generate some assembly to prove to myself the difference between the different pointer methods.
using the code: and compiling with -03
when p is simply declared as pointer, we get stuck in a loop that is impossible to get out of. Note that if this were a multithreaded program and a different thread wrote p[2] = 0, then the program would break out of the while loop and terminate normally.
notice that the only instruction for L6 is to goto L6.
==
when p is volatile pointer
here, the pointer p gets reloaded each loop iteration and as a consequence the array item also gets reloaded. However, this would not be correct if we wanted an array of volatile integers as this would be possible:
and would result in the loop that would be impossible to terminate in a multithreaded program.
==
finally, this is the correct solution as tony nicely explained.
In this case the the address of p[2] is kept in register value and not loaded from memory, but the value of p[2] is reloaded from memory on every loop cycle.
also note that
will generate a compile error.