volatile

R

Ravi

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

Sheldon Simms

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
 
S

Sheldon Simms

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

Ravi

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

Joona I Palaste

Ravi said:
On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms

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.

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 ([email protected]) ---------------------------\
| 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
 
A

August Derleth

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

Barry Schwarz

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

osmium

August said:
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.
 
R

Ramaraj

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
 
R

Ravi

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

Kevin Bracey

In message <[email protected]>
Barry Schwarz said:
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...

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.

Here's a pretty realistic example:

volatile char *serial_port;

/* Wait until we receive '>' over the serial port,
* then send "Hello".
*/
void say_hello(void)
{
while (*serial_port != '>')
;

*serial_port = 'H';
*serial_port = 'e';
*serial_port = 'l';
*serial_port = 'l';
*serial_port = 'o';
}

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

Dan Pop

In said:
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 following example:

#include <stdio.h>
#include <signal.h>

sig_atomic_t gotsig = 0;

void handler(int signo)
{
gotsig = signo;
}

int main()
{
signal(SIGINT, handler);
puts("Press the interrupt key to exit.");
while (gotsig == 0) ;
printf ("The program received signal %d.\n", (int)gotsig);
return 0;
}

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.

Dan
 
B

Barry Schwarz

On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
snip

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.


<<Remove the del for email>>
 
B

Barry Schwarz

In message <[email protected]>


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.


<<Remove the del for email>>
 
D

Dan Pop

In said:
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
 
I

Irrwahn Grausewitz

What part of my example was implementation dependent?

OK, I'm ready to burn my fingers by possibly typing nonsense:

IIRC, according to ISO/IEC 9899:1999 7.14 and 7.14.1

signal(SIGINT, handler);

has several implementation defined aspects.

Regards
 
B

Barry Schwarz

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.


<<Remove the del for email>>
 
I

Irrwahn Grausewitz

Barry Schwarz said:
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 [email protected] ).

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top