我如何申报使用malloc是在C挥发创建数组++ [英] How do I declare an array created using malloc to be volatile in c++

查看:131
本文介绍了我如何申报使用malloc是在C挥发创建数组++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我presume了下面就给我10挥发性整数

 挥发性INT富[10];

不过,我不认为下面会做同样的事情。

 挥发为int * foo的;
富=的malloc(sizeof的(INT)* 10);

请纠正我,如果我错了这件事,我怎么能有使用malloc项目的挥发性数组。

感谢。


解决方案

  INT挥发性* foo的;

从右读向左foo是一个指向挥发性INT

所以无论你诠释美孚通过访问,整型会挥发。

P.S。

 为int * foo的波动; //foo是挥发性指向int

==

 挥发为int * foo的; // foo是一个指向一个int,挥发性

含义foo是不稳定。第二种情况是真的只是一个普通从右到左规则剩。
在使用习惯要吸取的教训是获得

 字符常量* foo的;

而不是更常见的

 为const char * foo的;

如果你想喜欢更复杂的东西指针函数返回指向int的指针,以任何意义。

P.S,这是一个biggy(和主要的原因我添加一个答案):

我注意到,你列入多线程作为标记。你是否意识到,挥发性做的好一点的/没有关于多线程?

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.

解决方案

int volatile * foo;

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.

int * volatile foo; // "foo is a volatile pointer to an int"

==

volatile int * foo; // foo is a pointer to an int, volatile

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

char const * foo;

instead of the more common

const char * foo;

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?

这篇关于我如何申报使用malloc是在C挥发创建数组++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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