Take the 2-minute tour ×

volatile is to tell the compiler not to optimize the reference, so that every read/write does not use the value stored in register but does a real memory access. I can understand it is useful for some ordinary variable, but don't understand how volatile affects a pointer.

volatile int *p = some_addr;
int a = *p; // CPU always has to load the address, then does a memory access anyway, right?

What is the difference if it has been declared as int *p = some_addr?

share|improve this question

3 Answers 3

up vote 38 down vote accepted

A pointer of the form

volatile int* p;

is a pointer to an int that the compiler will treat as volatile. This means that the compiler will assume that it is possible for the variable that p is pointing at to have changed even if there is nothing in the source code to suggest that this might occur. For example, if I set p to point to a regular integer, then every time I read or write *p the compiler is aware that the value may have changed unexpectedly.

There is one more use case for a volatile int*: If you declare an int as volatile, then you should not point at it with a regular int*. For example, this is a bad idea:

volatile int myVolatileInt;
int* ptr = &myVolatileInt; // Bad idea!

The reason for this is that the C compiler no longer remembers that the variable pointed at by ptr is volatile, so it might cache the value of *p in a register incorrectly. In fact, in C++, the above code is an error. Instead, you should write

volatile int myVolatileInt;
volatile int* ptr = &myVolatileInt; // Much better!

Now, the compiler remembers that ptr points at a volatile int, so it won't (or shouldn't!) try to optimize accesses through *ptr.

One last detail - the pointer you discussed is a pointer to a volatile int. You can also do this:

int* volatile ptr;

This says that the pointer itself is volatile, which means that the compiler shouldn't try to cache the pointer in memory or try to optimize the pointer value because the pointer itself might get reassigned by something else (hardware, another thread, etc.) You can combine these together if you'd like to get this beast:

volatile int* volatile ptr;

This says that both the pointer and the pointee could get changed unexpectedly. The compiler can't optimize the pointer itself, and it can't optimize what's being pointed at.

Hope this helps!

share|improve this answer
I think you mean "you should NOT point at it with a regular int*" –  markgz Mar 29 '12 at 23:52
@markgz- Whoops! Yes, that's correct. Fixed. –  templatetypedef Mar 29 '12 at 23:52
I think it's an error in C as well, but C compilers are less prone to complain about type mismatches. –  Chris Lutz Mar 29 '12 at 23:53
Thanks. So it has no difference whether there is "volatile" for my example, right? But if there is another statemenet "int b = *p" following, it does make a difference, right? Specifically, "b" may be initialized using a register storeing "*p" instead of doing a real memory reference. –  Infinite Mar 29 '12 at 23:55
@SetTimer- The C spec is sufficiently general that ideas like "caches," "registers," and "memory references" don't exist. The compiler will probably not hold the variable in a register, but it could in theory be smart enough to notice that the int being pointed at is not marked volatile and thus would compile the code identically with and without volatile. –  templatetypedef Mar 29 '12 at 23:57

This code volatile int *p = some_addr declares a pointer to a volatile int. The pointer itself is not volatile.

In the unlikely event that you needed the pointer to be volatile as well as the int, you would need to use:

volatile int * volatile p;

I can't think of a situation where you would need to use that.

share|improve this answer
Example: I'm using 'volatile uint8_t* volatile pData' in ISR code that modifies the pointer and the data it points to. The pointer is set by the main code, and both pointer and data are read later. –  Christoph Feb 24 '13 at 0:24

A pointer is a variable that contains the address of another variable, so according to your description of volatile then the memory will be accessed twice.

share|improve this answer

Your Answer


