Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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

like image 212
raullp Avatar asked Jan 18 '10 21:01

raullp


2 Answers

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.

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.

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.

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
like image 58
jason Avatar answered Nov 14 '22 08:11

jason


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

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

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.

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.

like image 27
Jerry Coffin Avatar answered Nov 14 '22 09:11

Jerry Coffin