Volatile?

M

Malcolm McLean

Shraddha said:
What does volatile keyword actually used for?
It means "read this value from memory every time you use it". Normally all
you will achieve is to slow down the computer. However sometimes the
variable might be modified by something outside of the program. Also
occasionally you want to force a memory read for timing purposes.

For instance if we make a loop counter "volatile" then effectively the
compiler cannot optimise the loop away, because it doesn't know how many
times it will need to step through it. So we might have a fairer comparison
of run times.
 
J

Jack Klein

It means "read this value from memory every time you use it".

[snip]

That's part of what it means, but at least as important is:

Write all modified values to the object, and write them in the order
in which the writes appear in the source.

Consider:

extern volatile unsigned char *Uart_Tx_Register;

void send_char(char ch)
{
if ('\n' == ch)
*Uart_Tx_Register = '\r';
*Uart_Tx_Register = ch;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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

The Semantics of 'volatile' 73
Volatile variable 1
Volatile 2
volatile 4
Volatile and Registers 2
use of volatile ? 21
volatile keyword for C++ member functions 15
No way to add volatile access? 23

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top