RE: const and volatile keywords in C

  • From: Øyvind Lode <oyvind.lode@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 21 Jul 2010 00:09:22 +0200

Hi Sina:
I appreciate your kind offer to call you, but I'm way over here in Norway.
I don't use Skype either.
And my oral English is even worse than my written one :)

I'll go back and read what you wrote again, but now it's time to go to bed
over here.
So I'll do it tomorrow.

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
Sent: 20. juli 2010 23:13
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: const and volatile keywords in C

No, I was saying that it might seem like a misnomer, but isnt' one. Go back
and read what I wrote again, but if it's not clear, and
I can totally see how it wouldn't be, please feel free to call me, (919)
345-3832, and I think voice could handle this better than
text, in like 2 minutes flat.

Does that work? I really want you to get this concept.

Take care,
Sina

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

I think so.
I think I understand const and volatile.
But looking at the example provided in the book were both were used at the
same time was very confusing.
I read the authors explanation of const and volatile several times, but the
example were he is using both const and volatile at the
same time did not make any sense to me.
So maybe I'm still not getting it?

You are saying that using volatile and const at the same time might be a
misnumer.
Does this mean it's a bug in the book?
Does this mean I should just forget about it and move on?

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Sina Bahram
Sent: 20. juli 2010 19:56
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: const and volatile keywords in C

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


__________
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


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

Other related posts: