Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > volatile

Reply
Thread Tools

volatile

 
 
Ravi
Guest
Posts: n/a
 
      10-12-2003
I have read about this qualifier in many books and have read
things like "would not let the compiler optimize" "can be
modified externally" etc. But I am not able to understand it
well.

Could someone give me a small example and explain the
meaning and use of the volatile qualifier.

TIA.

--
main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
 
Reply With Quote
 
 
 
 
Sheldon Simms
Guest
Posts: n/a
 
      10-12-2003
On Sun, 12 Oct 2003 22:46:05 +0530, Ravi wrote:

> I have read about this qualifier in many books and have read
> things like "would not let the compiler optimize" "can be
> modified externally" etc. But I am not able to understand it
> well.
>
> Could someone give me a small example and explain the
> meaning and use of the volatile qualifier.


Consider the program

int x;
int main (void)
{
int i;
for (i = 0; i < 4; ++i)
x = i;
return x;
}

When a compiler optimizes this program, it is free to change it
to something like

int main (void)
{
return 3;
}

because this program has the same behavior as the original one.
When the original program is changed to

volatile int x;
int main (void)
{
int i;
for (i = 0; i < 4; ++i)
x = i;
return x;
}

then the assignments to x must take place. They may not be
optimized away. The best a compiler could do to optimize
the code now is something like:

volatile int x;
int main (void)
{
x = 0;
x = 1;
x = 2;
x = 3;
return 3;
}

Footnote 114 in the C standard explains it like this:

A volatile declaration may be used to describe an object
corresponding to a memory-mapped input/output port or an
object accessed by an asynchronously interrupting function.
Actions on objects so declared shall not be optimized out
by an implementation or reordered except as permitted by
the rules for evaluating expressions.

-Sheldon

 
Reply With Quote
 
 
 
 
Sheldon Simms
Guest
Posts: n/a
 
      10-12-2003
On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms wrote:

> On Sun, 12 Oct 2003 22:46:05 +0530, Ravi wrote:
>
>> I have read about this qualifier in many books and have read
>> things like "would not let the compiler optimize" "can be
>> modified externally" etc. But I am not able to understand it
>> well.
>>
>> Could someone give me a small example and explain the
>> meaning and use of the volatile qualifier.

>
> Consider the program
>
> int x;
> int main (void)
> {
> int i;
> for (i = 0; i < 4; ++i)
> x = i;
> return x;
> }
>
> When a compiler optimizes this program, it is free to change it
> to something like
>
> int main (void)
> {
> return 3;
> }
>
> because this program has the same behavior as the original one.
> When the original program is changed to
>
> volatile int x;
> int main (void)
> {
> int i;
> for (i = 0; i < 4; ++i)
> x = i;
> return x;
> }
>
> then the assignments to x must take place. They may not be
> optimized away. The best a compiler could do to optimize
> the code now is something like:
>
> volatile int x;
> int main (void)
> {
> x = 0;
> x = 1;
> x = 2;
> x = 3;
> return 3;
> }


Oops-- this should be

volatile int x;
int main (void)
{
x = 0;
x = 1;
x = 2;
x = 3;
return x;
}

>
> Footnote 114 in the C standard explains it like this:
>
> A volatile declaration may be used to describe an object
> corresponding to a memory-mapped input/output port or an
> object accessed by an asynchronously interrupting function.
> Actions on objects so declared shall not be optimized out
> by an implementation or reordered except as permitted by
> the rules for evaluating expressions.
>
> -Sheldon


 
Reply With Quote
 
Ravi
Guest
Posts: n/a
 
      10-12-2003
On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
<(E-Mail Removed)> wrote:

>Footnote 114 in the C standard explains it like this:
>
> A volatile declaration may be used to describe an object
> corresponding to a memory-mapped input/output port or an
> object accessed by an asynchronously interrupting function.
> Actions on objects so declared shall not be optimized out
> by an implementation or reordered except as permitted by
> the rules for evaluating expressions.

I've read these things.


I understand this. But I want a proper example clearly
telling me the use of declaring a variable volatile etc.

TIA.

--
main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-12-2003
Ravi <(E-Mail Removed)> scribbled the following:
> On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
> <(E-Mail Removed)> wrote:


>>Footnote 114 in the C standard explains it like this:
>>
>> A volatile declaration may be used to describe an object
>> corresponding to a memory-mapped input/output port or an
>> object accessed by an asynchronously interrupting function.
>> Actions on objects so declared shall not be optimized out
>> by an implementation or reordered except as permitted by
>> the rules for evaluating expressions.

> I've read these things.


> I understand this. But I want a proper example clearly
> telling me the use of declaring a variable volatile etc.


> TIA.


Didn't Sheldon just give you one?

Another one might be that some pointer variable holds a memory address
where an external hardware device keeps reading stuff from. The C
program periodically assigns new values to this address.
Because the C program never actually *reads* from this address, an
optimising compiler might optimise the entire assignments away, leaving
the external hardware device with nothing to read. Therefore the
pointer variable needs to be declared volatile, telling the compiler
to keep the assignments in even though they appear to serve no
purpose.

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
 
Reply With Quote
 
Michael Str.
Guest
Posts: n/a
 
      10-12-2003
Sheldon Simms <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms wrote:
>
> > On Sun, 12 Oct 2003 22:46:05 +0530, Ravi wrote:
> >
> >> I have read about this qualifier in many books and have read
> >> things like "would not let the compiler optimize" "can be
> >> modified externally" etc. But I am not able to understand it
> >> well.
> >>
> >> Could someone give me a small example and explain the
> >> meaning and use of the volatile qualifier.

> >
> > Consider the program
> >
> > int x;
> > int main (void)
> > {
> > int i;
> > for (i = 0; i < 4; ++i)
> > x = i;
> > return x;
> > }
> >
> > When a compiler optimizes this program, it is free to change it
> > to something like
> >
> > int main (void)
> > {
> > return 3;
> > }
> >
> > because this program has the same behavior as the original one.
> > When the original program is changed to
> >
> > volatile int x;
> > int main (void)
> > {
> > int i;
> > for (i = 0; i < 4; ++i)
> > x = i;
> > return x;
> > }
> >
> > then the assignments to x must take place. They may not be
> > optimized away. The best a compiler could do to optimize
> > the code now is something like:
> >
> > volatile int x;
> > int main (void)
> > {
> > x = 0;
> > x = 1;
> > x = 2;
> > x = 3;
> > return 3;
> > }

>
> Oops-- this should be
>
> volatile int x;
> int main (void)
> {
> x = 0;
> x = 1;
> x = 2;
> x = 3;
> return x;
> }
>
> >
> > Footnote 114 in the C standard explains it like this:
> >
> > A volatile declaration may be used to describe an object
> > corresponding to a memory-mapped input/output port or an
> > object accessed by an asynchronously interrupting function.
> > Actions on objects so declared shall not be optimized out
> > by an implementation or reordered except as permitted by
> > the rules for evaluating expressions.
> >
> > -Sheldon


I think good description of volatile can be found in:
http://www.embedded.com/story/OEG20010615S0107

Regards
Michael
 
Reply With Quote
 
August Derleth
Guest
Posts: n/a
 
      10-12-2003
Ravi <(E-Mail Removed)> wrote in
news:(E-Mail Removed) on Sun 12 Oct 2003
01:15:44p:


> I understand this. But I want a proper example clearly
> telling me the use of declaring a variable volatile etc.


What, exactly, is unclear? To make a variable volatile, simply use the
keyword 'volatile'. Then use it as you normally would use a variable of
that type. There's no special tricks, just a hint to the compiler that it
should not perform certain optimizations (if, indeed, it optimizes at
all).

Other posts in the thread tell you what that can actually mean in your
code, and I hope you realize that overuse of the qualifier can lead to
code that the compiler basically cannot improve because so many
optimizations are no longer available. Of course, the qualifier can also
make things possible that the compiler would otherwise optimize away.

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-12-2003
On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <(E-Mail Removed)> wrote:

>On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
><(E-Mail Removed)> wrote:
>
>>Footnote 114 in the C standard explains it like this:
>>
>> A volatile declaration may be used to describe an object
>> corresponding to a memory-mapped input/output port or an
>> object accessed by an asynchronously interrupting function.
>> Actions on objects so declared shall not be optimized out
>> by an implementation or reordered except as permitted by
>> the rules for evaluating expressions.

>I've read these things.
>
>
>I understand this. But I want a proper example clearly
>telling me the use of declaring a variable volatile etc.


Any proper example would probably be implementation dependent. You
haven't told us what system you are using or which other ones you are
familiar enough with to understand the example if presented.

And if you did it would be off-topic here so you are better off asking
in a group devoted to your system or one you understand.


<<Remove the del for email>>
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-13-2003
August Derleth writes:

> There's no special tricks, just a hint to the compiler that it
> should not perform certain optimizations (if, indeed, it optimizes at
> all).


It is not a hint! It is a directive.


 
Reply With Quote
 
Ramaraj
Guest
Posts: n/a
 
      10-13-2003
Hi All,
The volatile Keyword is frequently used in the application like drivres
for the memory mapped device etc. if the execution control is one
function/block and some external events changes the values of volatile
variable without interrupting the current execution functional/block. so
that optimization of the compiler is resticted here bcoz the voalitile
variable is runtime entity it
can be changes(regardless functional execution control) .

I can give the example as follows

we can bind the volatile variable "i" to clock tick and the variable changes
during the without knowing the current execution functional or block


"Ravi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have read about this qualifier in many books and have read
> things like "would not let the compiler optimize" "can be
> modified externally" etc. But I am not able to understand it
> well.
>
> Could someone give me a small example and explain the
> meaning and use of the volatile qualifier.
>
> TIA.
>
> --
> main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
> int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0xffc Thread 0x228 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-30-2006 03:16 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 1 03-24-2006 04:20 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-24-2006 02:22 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM
Re: Volatile? Knute Johnson Java 17 07-03-2003 03:31 AM



Advertisments