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

时间:2022-11-14 13:29:40

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

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

4 个解决方案

#1


16  

The difference really comes down to the difference between const and volatile. The only things these two concepts have in common is syntax. const is compiler-enforced and says "the programmer can not change this." volatile says "this data might be changed by someone else" and so the compiler will not make any assumptions about that data. 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." 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.

区别实际上在于const和volatile之间的区别。这两个概念唯一的共同点就是语法。const是编译器强制执行的,并且说“程序员不能改变这个”volatile说“这个数据可能会被其他人改变”,因此编译器不会对这个数据做出任何假设。如果没有volatile,编译器可能会说:“我将内存中的数据放入寄存器中,由于我没有对该数据做任何操作,所以我确信它是相同的,我不需要再将它读入寄存器中。”当数据标记为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是一个指向整型和点不能被改变,指针由程序员而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是一个指向整型和p是指由程序员不能改变而q是一个指向整型和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

#2


2  

Normally, const or volatile applies to the pointee, not the pointer itself.

通常,const或volatile应用于指针,而不是指针本身。

const means you're not allowed to modify the pointee via that pointer.

const意味着不允许您通过指针修改pointee。

volatile means somebody/something else might modify the pointee, even though your code doesn't. 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.

volatile表示某些人或其他事物可能会修改pointee,即使您的代码没有这样做。它还意味着对变量的写入可能不仅仅是存储下次使用该变量时要检索的值。因此,每当您的代码读取或写入一个volatile值时,编译器就必须生成从实际内存(或写入内存)读取(或写入)的代码,而不仅仅是(例如)分配一个寄存器用于临时使用,并读取/写入寄存器。

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. 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指针修改数据,也可以通过其他方式修改数据。事实上,有时你可以有一个点,它既复杂又不稳定,这意味着你不能改变它,但其他人可能会改变它。

#3


1  

Here's an explanation of those two concepts

这是对这两个概念的解释

The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter.

const关键字指定在初始化后不能修改指针;此后,该指针被保护不受修改。

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. 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关键字指定与后面的名称相关联的值可以通过用户应用程序中的操作以外的操作进行修改。因此,volatile关键字对于声明共享内存中的对象非常有用,这些对象可以被多个进程或用于与中断服务例程通信的全局数据区域访问。

It comes from here

它来自这里

#4


1  

A const pointer (i.e. const char* s = ..) points to data that can not be modified. A volatile pointer (i.e.volatile char* s = ...) hints the compiler not to cache the data the pointer refers to in CPU registers, or elsewhere. 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.

const指针(即const char* s = ..)指向不能修改的数据。一个不稳定的指针(即。volatile char* s =…)提示编译器不要缓存指针在CPU寄存器或其他地方引用的数据。相反,每次需要时,它们都会从原来的内存位置重新读取。如果数据的内容可能在编译器的作用域之外发生更改,例如通过第二个线程修改它,则需要这样做。

Be careful, const char* and char* const are different things, same for the volatilequalifier. If you're unsure, look them up.

注意,const char*和char* const是不同的东西,挥发性限定词也是一样的。如果你不确定,查一下。

#1


16  

The difference really comes down to the difference between const and volatile. The only things these two concepts have in common is syntax. const is compiler-enforced and says "the programmer can not change this." volatile says "this data might be changed by someone else" and so the compiler will not make any assumptions about that data. 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." 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.

区别实际上在于const和volatile之间的区别。这两个概念唯一的共同点就是语法。const是编译器强制执行的,并且说“程序员不能改变这个”volatile说“这个数据可能会被其他人改变”,因此编译器不会对这个数据做出任何假设。如果没有volatile,编译器可能会说:“我将内存中的数据放入寄存器中,由于我没有对该数据做任何操作,所以我确信它是相同的,我不需要再将它读入寄存器中。”当数据标记为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是一个指向整型和点不能被改变,指针由程序员而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是一个指向整型和p是指由程序员不能改变而q是一个指向整型和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

#2


2  

Normally, const or volatile applies to the pointee, not the pointer itself.

通常,const或volatile应用于指针,而不是指针本身。

const means you're not allowed to modify the pointee via that pointer.

const意味着不允许您通过指针修改pointee。

volatile means somebody/something else might modify the pointee, even though your code doesn't. 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.

volatile表示某些人或其他事物可能会修改pointee,即使您的代码没有这样做。它还意味着对变量的写入可能不仅仅是存储下次使用该变量时要检索的值。因此,每当您的代码读取或写入一个volatile值时,编译器就必须生成从实际内存(或写入内存)读取(或写入)的代码,而不仅仅是(例如)分配一个寄存器用于临时使用,并读取/写入寄存器。

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. 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指针修改数据,也可以通过其他方式修改数据。事实上,有时你可以有一个点,它既复杂又不稳定,这意味着你不能改变它,但其他人可能会改变它。

#3


1  

Here's an explanation of those two concepts

这是对这两个概念的解释

The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter.

const关键字指定在初始化后不能修改指针;此后,该指针被保护不受修改。

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. 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关键字指定与后面的名称相关联的值可以通过用户应用程序中的操作以外的操作进行修改。因此,volatile关键字对于声明共享内存中的对象非常有用,这些对象可以被多个进程或用于与中断服务例程通信的全局数据区域访问。

It comes from here

它来自这里

#4


1  

A const pointer (i.e. const char* s = ..) points to data that can not be modified. A volatile pointer (i.e.volatile char* s = ...) hints the compiler not to cache the data the pointer refers to in CPU registers, or elsewhere. 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.

const指针(即const char* s = ..)指向不能修改的数据。一个不稳定的指针(即。volatile char* s =…)提示编译器不要缓存指针在CPU寄存器或其他地方引用的数据。相反,每次需要时,它们都会从原来的内存位置重新读取。如果数据的内容可能在编译器的作用域之外发生更改,例如通过第二个线程修改它,则需要这样做。

Be careful, const char* and char* const are different things, same for the volatilequalifier. If you're unsure, look them up.

注意,const char*和char* const是不同的东西,挥发性限定词也是一样的。如果你不确定,查一下。