Cheiron
Cheiron

Reputation: 3746

Necessity of volatile keyword when defning a pointer with a hard address

I am currently working with the Altera Nios II and this gives me the possibility to, for example, connect all the red LEDs to an int* with given memory address. In all the examples this looks like the following:

volatile int * ledR = (int*) 0x00093050;

It is always prefixed with the volatile keyword, why? As far as I know the volatile keyword only tells the compiler to not assume anything about the variable, but the compile won't just assume that this pointer should be removed, right?

Upvotes: 2

Views: 773

Answers (5)

supercat
supercat

Reputation: 81247

Many compilers used to treat all objects with hard-coded addresses as volatile, since doing otherwise would require extra work for both the compiler and the person using it, while offering little benefit. Anyone writing a compiler for embedded or systems programming use would be well-advised to at minimum include a mode which treats all hard-coded fetches as volatile given the corpus of code which omitted the keyword for purposes of brevity before compilers started requiring it, but anyone writing code for embedded systems would be well advised to include volatile in all hard-coded-address register declaration so as to ensure correct behavior with compilers whose that attempt to consolidate operations at hard-coded addresses.

Upvotes: 0

Gauthier
Gauthier

Reputation: 41975

volatile forces the compiler to actually do the operations on the object that is volatile, even if it could see it as unnecessary. In your example, volatile refers to the object being pointed at.

For registers:

  • it forces a read of the actual register. If you read the volatile identifier several times, that's how many times the HW register will be read. No caching in a local variable. It might be important for several reasons: the value (for example of a GPIO) might change, some registers have special actions on read.

  • it forces a write. If you write to an address but do not reuse this content, the compiler might in some cases think that the write was unnecessary and remove it altogether.

Upvotes: 2

Srikanth
Srikanth

Reputation: 445

Mainly when dealing with hardware registers , we need to use volatile keyword. This makes the compiler to fetch the register everytime when you invoke the address , rather than using the local copy of that variable .

Upvotes: 4

Chinna
Chinna

Reputation: 4002

volatile int * ledR = (int*) 0x00093050;

volatile in above statement says to compiler that, value at the address 0x00093050 should not cached. Here volatile is not for pointer, it is for the value at the address which is held by pointer.

Upvotes: 1

Lundin
Lundin

Reputation: 214475

There is countless of reading on the Internet regarding how the volatile keyword works...

It is always prefixed with the volatile keyword, why?

Because the code must read/write the value of the actual hardware register. If not for volatile, the optimizer might decide to read the hardware register, then store the value away inside a local variable in RAM, and then use that variable from now on whenever you try to access the register. Which would of course break the whole program. There are many such similar bugs that the optimizer can cause.

So the volatile keyword tells the compiler that it isn't allowed to perform any optimizations involving a particular variable.

Upvotes: 0

Related Questions