Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   volatile (http://www.velocityreviews.com/forums/t315739-volatile.html)

Ravi 10-12-2003 05:16 PM

volatile
 
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;}

Sheldon Simms 10-12-2003 06:35 PM

Re: volatile
 
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


Sheldon Simms 10-12-2003 06:37 PM

Re: volatile
 
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



Ravi 10-12-2003 07:03 PM

Re: volatile
 
On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
<sheldonsimms@yahoo.com> 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;}

Joona I Palaste 10-12-2003 07:13 PM

Re: volatile
 
Ravi <ravi@despammed.com> scribbled the following:
> On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
> <sheldonsimms@yahoo.com> 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 (palaste@cc.helsinki.fi) ---------------------------\
| 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

Michael Str. 10-12-2003 09:43 PM

Re: volatile
 
Sheldon Simms <sheldonsimms@yahoo.com> wrote in message news:<pan.2003.10.12.18.37.25.465264@yahoo.com>...
> 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

August Derleth 10-12-2003 10:22 PM

Re: volatile
 
Ravi <ravi@despammed.com> wrote in
news:e99jovo8ufe6rndg255q2l0usrsaqbedja@4ax.com 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.


Barry Schwarz 10-12-2003 10:43 PM

Re: volatile
 
On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <ravi@despammed.com> wrote:

>On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
><sheldonsimms@yahoo.com> 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>>

osmium 10-13-2003 12:53 AM

Re: volatile
 
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.



Ramaraj 10-13-2003 07:12 AM

Re: volatile
 
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" <ravi@despammed.com> wrote in message
news:k23jov0166kf0blsde8581cff7una9uh0l@4ax.com...
> 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;}







All times are GMT. The time now is 04:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.