Advertisements

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.

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.

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

Ravi <> scribbled the following:
> On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
> <> 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.

> 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.

On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
>On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
><> 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.

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" <> wrote in message
news:...
> 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;}

On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
<> wrote:
>On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
>
>>On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
>><> 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.

In message <bmcle8$va0$0@216.39.134.220>
Barry Schwarz <> wrote:
> On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
>
> >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.

volatile solves a real-world problem in low-level programming. It exists to
stop clever compilers from over-optimising code. You won't see it discussed
here very much for that very reason - comp.lang.c doesn't generally do
real-world problems, and doesn't concern itself with things like
optimisation.

The idea here is that "serial_port" is pointing at a memory-mapped read- and
write-sensitive I/O port. Without the volatile qualifier, the compiler would
be free to assume that repeatedly reading from that address would always
return the same value, and that multiple writes were irrelevant. It might end
up optimising it to something like:

void say_hello(void)
{
char temp = *serial_port
while (temp != '>')
;

*serial_port = 'o';
}

which was clearly not the intent.

Another possible use of volatile is when you might be sharing a variable
with another thread or an interrupt routine, so it is important that the
variable's content be kept up-to-date and should be rechecked every time it
is used.

In <> Ravi <> writes:
>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.

If you compile it with gcc without enabling optimisation, it will work
as expected. With -O2, it will stay forever in the while loop, because
the compiler assumes that the value of gotsig cannot change inside the
loop, so there is no need to check its value more than once: the loop is
either skipped or it becomes an infinite loop.

To avoid this, you must tell to the compiler that it cannot assume that
the value of gotsig cannot change inside the loop (otherwise, a perfectly
reasonable assumption). To achieve this, you declare gotsig like this:

volatile sig_atomic_t gotsig = 0;

Now, the program will work as expected at any optimisation level (at least
as far as gcc is concerned ;-)

Another typical example is a program performing memory tests. Such
programs write certain values to the memory, then read them back and
check to see if they get the same values. From the compiler's point of
view, such an operation is completely pointless: if you write something
you're guaranteed to read back the same value, so it could optimise the
whole code away. To avoid that, you have to either make the pointer
volatile or the object it points to or both, but it's probably more
efficient to make only the object pointed to volatile.

On Mon, 13 Oct 2003 13:53:58 +0530, Ravi <> wrote:
>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
><> wrote:
>
snip
>>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.
>
>Linux, Windows or DOS.
>
>TIA.

Your not welcome. What part of the next paragraph which you
conveniently cut out (which said "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.") did you not comprehend.

On Mon, 13 Oct 2003 12:58:10 +0100, Kevin Bracey
<> wrote:
>In message <bmcle8$va0$0@216.39.134.220>
> Barry Schwarz <> wrote:
>
>> On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
>>
>> >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.
>
>Blimey, we're all being unhelpful today, aren't we? A whole keyword pretty
>much declared off-topic...

Do you enjoy taking things out of context or is it just a bad habit?
After several examples, the OP still wanted a "proper" one that
related to something he was familiar with. If you have one that is
not implementation specific and not a duplicate of one already
provided, by all means let us see it. The memory mapped I/O idea was
previously discussed and already rejected by the OP as not
sufficiently informative.

In <bmfn99$dio$1@216.39.134.185> Barry Schwarz <> writes:
>On Mon, 13 Oct 2003 13:53:58 +0530, Ravi <> wrote:
>
>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
>><> wrote:
>>
>snip
>>>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.
>>
>>Linux, Windows or DOS.
>>
>>TIA.
>
>Your not welcome. What part of the next paragraph which you
>conveniently cut out (which said "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.") did you not comprehend.

(Dan Pop) wrote:
>>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
>>><> wrote:
<snip>
>>>>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.
<snip>
>
>What part of my example was implementation dependent?

On 14 Oct 2003 12:31:29 GMT, (Dan Pop) wrote:
>In <bmfn99$dio$1@216.39.134.185> Barry Schwarz <> writes:
>
>>On Mon, 13 Oct 2003 13:53:58 +0530, Ravi <> wrote:
>>
>>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
>>><> wrote:
>>>
>>snip
>>>>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.
>>>
>>>Linux, Windows or DOS.
>>>
>>>TIA.
>>
>>Your not welcome. What part of the next paragraph which you
>>conveniently cut out (which said "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.") did you not comprehend.
>
>What part of my example was implementation dependent?
>
>Dan

Dan

I responded to Ravi. I don't recall any text in his message relating
to anything you did.

Barry Schwarz <> wrote:
>On 14 Oct 2003 12:31:29 GMT, (Dan Pop) wrote:
>
>>>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
>>>><> wrote:
>>>>>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.
<snip>
>>What part of my example was implementation dependent?
>>
>I responded to Ravi. I don't recall any text in his message relating
>to anything you did.

I assume he was aiming at your claim that "Any proper example would
probably be implementation dependent." and the fact that he gave such
an example in his reply to the OP (M.ID bmeg7g$jsh$ ).

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!