简体   繁体   English

C中的const和volatile指针有什么区别?

[英]What are the differences between const and volatile pointer in C?

C中的const和volatile指针有什么区别?

The difference really comes down to the difference between const and volatile . 差异实际上归结为constvolatile之间的差异。 The only things these two concepts have in common is syntax. 这两个概念的共同点就是语法。 const is compiler-enforced and says "the programmer can not change this." const是编译器强制执行的并且说“程序员不能改变它”。 volatile says "this data might be changed by someone else" and so the compiler will not make any assumptions about that data. volatile表示“这些数据可能会被其他人更改”,因此编译器不会对该数据做出任何假设。 Without volatile the compiler might say "I put this data from memory into a register, and since I haven't done anything to that data, I'm sure it's the same and I don't need to read it into the register again." 如果没有volatile ,编译器可能会说“我将这些数据从内存放入寄存器,因为我没有对该数据做任何事情,我确信它是相同的,我不需要再次将它读入寄存器。 “ When the data is marked as volatile the compiler won't make such an assumption (because someone else might have changed the data) and so it will reread the data into the register. 当数据被标记为volatile ,编译器将不会做出这样的假设(因为其他人可能已经更改了数据),因此它会将数据重新读入寄存器。

Now, are you asking for the difference between 现在,你要求它们之间的区别

int *const p;

and

int *volatile q;

or the difference between 或者之间的区别

const int* p;

and

volatile int* q;

In the former case: p is a pointer to an int and where that pointer points can not be changed by the programmer whereas q is a pointer to an int and where that pointer points could be changed by someone other than the programmer so the compiler makes no assumptions about that pointer. 在前一种情况下: p是指向int指针,程序员不能更改指针点,而q是指向int的指针,并且指针指向的点可以由程序员以外的其他人更改,因此编译器会使没有关于那个指针的假设。

So: 所以:

int *const p = (int*)malloc(sizeof(int));
int *volatile q = (int*)malloc(sizeof(int));
*p = 17; // legal;
p = (int*)malloc(sizoef(int)); // not legal
*q = 17; // legal;
q = (int*)malloc(sizeof(int)); // legal

In the latter case: p is a pointer to an int and what p is pointing to can not be changed by the programmer whereas q is a pointer to an int and what q is pointing to could be changed by someone other than the programmer so the compiler makes no assumptions about that data. 在后一种情况下: p是指向int的指针, p指向的是p不能由程序员改变,而q是指向int的指针, q指向的是什么,可以由程序员以外的人改变,所以编译器不对该数据做任何假设。

int i = 17;
int j = 34;
const int *p = &i;
volatile int *q = &i;
*p = 51; // not legal
p = &j; // legal
*q = 51; // legal
q = &j; // legal

Normally, const or volatile applies to the pointee, not the pointer itself. 通常, constvolatile适用于指针,而不是指针本身。

const means you're not allowed to modify the pointee via that pointer. const意味着你不允许通过该指针修改指针。

volatile means somebody/something else might modify the pointee, even though your code doesn't. volatile意味着某人/别人可能会修改指针,即使你的代码没有。 It also means that writing to the variable might do something more than just store a value to be retrieved the next time that variable is used. 这也意味着写入变量可能不仅仅是存储一个值,以便在下次使用该变量时进行检索。 As a result, any time your code reads or writes a volatile value, the compiler is obliged to generate code that reads from (or writes to) actual memory, not just (for example) allocates a register for temporary use, and reads/writes the register. 因此,只要您的代码读取或写入易失性值,编译器就必须生成从实际内存中读取(或写入)的代码,而不仅仅是(例如)分配临时使用的寄存器,以及读取/写入登记册。

Edit: Note that even though you're not allowed to modify the data via a const pointer, the data may still be modified by other means. 编辑:请注意,即使您不允许通过const指针修改数据,仍可以通过其他方式修改数据。 In fact, there are times that you can have a pointee that's both const and volatile , which means you can't change it, but somebody else might. 实际上,有时候你可以拥有一个const volatile的指针,这意味着你无法改变它,但其他人可能会改变它。

Here's an explanation of those two concepts 这是对这两个概念的解释

The const keyword specifies that the pointer cannot be modified after initialization; const关键字指定初始化后不能修改指针; the pointer is protected from modification thereafter. 此后保护指针不被修改。

The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application. volatile关键字指定与后面的名称关联的值可以通过除用户应用程序中的操作之外的操作进行修改。 Therefore, the volatile keyword is useful for declaring objects in shared memory that can be accessed by multiple processes or global data areas used for communication with interrupt service routines. 因此,volatile关键字对于声明共享内存中的对象非常有用,这些对象可以由多个进程或用于与中断服务例程通信的全局数据区域访问。

It comes from here 它来自这里

A const pointer (ie const char* s = .. ) points to data that can not be modified. const指针(即const char* s = .. )指向无法修改的数据。 A volatile pointer (ie volatile char* s = ... ) hints the compiler not to cache the data the pointer refers to in CPU registers, or elsewhere. 易失性指针(即volatile char* s = ... )提示编译器不要缓存指针在CPU寄存器或其他地方引用的数据。 Instead, they're reread from their original memory location every time they're needed. 相反,它们每次需要时都会从原始内存位置重新读取。 This is needed if the contents of the data might change outside the compiler's scope, for example through a second thread modifying it. 如果数据的内容可能在编译器的范围之外发生变化,例如通过修改它的第二个线程,则需要这样做。

Be careful, const char* and char* const are different things, same for the volatile qualifier. 注意, const char*char* const是不同的东西,对于volatile限定符也是如此。 If you're unsure, look them up. 如果您不确定,请查看它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM