RE: const and volatile keywords in C

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 20 Jul 2010 13:56:00 -0400

Volatile implies to the compiler that a simple SSA optimization can not be 
accurately carried out, like so.

If I have this code:

int x = 3, y=5, z;
z = x+y;
x = 10;
z = x+y;

The compiler can rewrite that as the following: *note: actual compiler writers 
give me a break, I know it's not proper SSA, get over
it*

x1=3
y1=5
z1 = x1+y1;
x2 = 10;
z2 = x2+y1;


As you can see, each time the value of x or z changes, the subscript 
incremented one. This allows the compiler to then be able to
rearrange lines of code, knowing it doesn't have any data dependencies that it 
would be violating. If you mark x as volatile, it
can't make those assumptions. Every single time you reference x, even if in the 
exact previous line, you assigned a value to x, the
compiler has to go check x again, and can't go off of a cached value in a 
register.

Now, it sounds like you do already understand volatile anyways, but I wanted to 
give that explanation as a way of putting us both on
the same page.

As far as const volatile, it might seem like this is a bit of a misnomer. You 
might think the compiler will ignore the volatile, as
const has much higher priority, semantically speaking; thus, the volatile is 
simply ignored, since because the value is const, it
can't be changed, but that's not true.

Here's what it means.


The const says the code can't change the value of x, and the volatile says not 
to cache x because it can be changed, but by what,
you might ask? Well, by something else. Let's say a driver is updating that 
value, a hardware interrupt is firing, whatever. So the
code can't change that const, but it's volatile which means it's storage area's 
value, actually can be changed by external
influences.

Hope this helps?


Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Øyvind Lode
Sent: Tuesday, July 20, 2010 12:38 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: const and volatile keywords in C

Hi all:

I know that the "const" keyword declares a value that cannot be change by the 
program and this may lead to better performance.
If volatile is used the value may be changed by the program, but it may also be 
changed by some outside influence.

volatile const int index3 = 12;
volatile int index4;

The difference between the two as I see it is that the first one use both 
volatile and const and assigns 12 to an integer type to
the identifier named
index3 and the second only uses volatile and declares an integer type 
identified as index4 but does not assign a value.

So if I do:

volatile const index3 = 12;
volatile int index4 = 12;

Now I have two int types with two identifiers named index3 and index4 with the 
same value of 12.
What's not clear to me is why only volatile is used and what's the difference 
between using both volatile const to only use
volatile?


__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: