I have a function whose prototype is as follows:
我有一个函数的原型如下:
void foo(const char * data);
Elsewhere in my code, I have a global variable declared as follows
在代码的其他地方,我有一个全局变量,声明如下。
volatile char var[100];
Whenever I try to do this:
每当我这样做的时候:
foo(var);
The compiler throws up the following error message:
编译器弹出如下错误消息:
Argument of type "volatile char *" is incompatible with parameter of type "const char *"
“volatile char *”类型的参数与“const char *”类型的参数不兼容
Why is that the case? As I understand it, the variable in my function is not allowed to change the pointer or its contents. I understand that because my global variable is volatile, it could potentially change at any time, but seeing as it is perfectly legal to have a volatile const variable, I don't see why I am getting this compiler error.
为什么会这样呢?根据我的理解,函数中的变量不允许更改指针或其内容。我理解,因为我的全局变量是不稳定的,它随时可能发生变化,但是看到一个不稳定的const变量是完全合法的,我不明白为什么我得到了这个编译器错误。
Thanks
谢谢
--Amr
——Amr
3 个解决方案
#1
6
It's because implicit conversions can add qualifiers to the target of pointer types, but not remove them. So if you want your function to be able to accept volatile
and/or const
qualified pointers, you must declare it with both:
这是因为隐式转换可以向指针类型的目标添加限定符,但不能删除它们。因此,如果希望函数能够接受volatile和/或const限定指针,则必须同时声明:
void foo(const volatile char * data);
#2
5
Because accessing a volatile variable using pointer to non-volatile is wrong. Either the object is volatile and then it should be accessed as such everywhere or you can access it as non-volatile and then it should not be marked as such. Make up your mind.
因为使用指向非易失性的指针访问volatile变量是错误的。要么对象是易失性的,然后它应该在任何地方被访问或者你可以作为非易失性访问它然后它不应该被标记为易失性。下定决心吧。
#3
2
If you want to handle a volatile
argument in your function you must declare it as such:
如果您想在函数中处理一个易变参数,您必须这样声明:
void foo(const volatile char * data);
This would do the trick. But be aware that this also brings you all the overhead of volatile
to the implementation of foo
, i.e data[something]
will be reloaded from memory at any point that you access it.
这样就可以了。但是请注意,这也会给foo i的实现带来所有的volatile开销。e数据[某物]将在访问它的任何时刻从内存中重新加载。
(Generally volatile
is not so much of a good idea, unless you are doing device drivers or so. Even for parallel processing with threads it usually doesn't guarantee what you expect at a first site.)
(通常来说,volatile并不是个好主意,除非你使用的是设备驱动程序。即使使用线程进行并行处理,通常也不能保证您在第一个站点上所期望的结果。
#1
6
It's because implicit conversions can add qualifiers to the target of pointer types, but not remove them. So if you want your function to be able to accept volatile
and/or const
qualified pointers, you must declare it with both:
这是因为隐式转换可以向指针类型的目标添加限定符,但不能删除它们。因此,如果希望函数能够接受volatile和/或const限定指针,则必须同时声明:
void foo(const volatile char * data);
#2
5
Because accessing a volatile variable using pointer to non-volatile is wrong. Either the object is volatile and then it should be accessed as such everywhere or you can access it as non-volatile and then it should not be marked as such. Make up your mind.
因为使用指向非易失性的指针访问volatile变量是错误的。要么对象是易失性的,然后它应该在任何地方被访问或者你可以作为非易失性访问它然后它不应该被标记为易失性。下定决心吧。
#3
2
If you want to handle a volatile
argument in your function you must declare it as such:
如果您想在函数中处理一个易变参数,您必须这样声明:
void foo(const volatile char * data);
This would do the trick. But be aware that this also brings you all the overhead of volatile
to the implementation of foo
, i.e data[something]
will be reloaded from memory at any point that you access it.
这样就可以了。但是请注意,这也会给foo i的实现带来所有的volatile开销。e数据[某物]将在访问它的任何时刻从内存中重新加载。
(Generally volatile
is not so much of a good idea, unless you are doing device drivers or so. Even for parallel processing with threads it usually doesn't guarantee what you expect at a first site.)
(通常来说,volatile并不是个好主意,除非你使用的是设备驱动程序。即使使用线程进行并行处理,通常也不能保证您在第一个站点上所期望的结果。