473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Volatile specifier

What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?
Nov 14 '05 #1
5 2723

"aruna" <ar********@yahoo.co.in> wrote in message
news:a2**************************@posting.google.c om...
What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?

Did you read any FAQs about this? What are your specific questions? I
won't mention my personal reasons for using it, as they aren't likely to
intersect with the interests of 98% of the readers of this NG, and don't
conform to Standard C.
Nov 14 '05 #2
aruna wrote:

What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?
[... and questions of a similar level in many more messages ...]


Aruna, have you done any research on these questions before
posting them? Have you read a C textbook or tutorial? Have
you read the comp.lang.c Frequently Asked Questions document?
If so and if you are confused by what these sources have told
you, *then* take your question to comp.lang.c, and explain
the nature of your confusion.

Do not try to treat comp.lang.c or any other forum as a
substitute for study. Usenet is an ineffective vehicle for
basic instruction.

--
Er*********@sun.com
Nov 14 '05 #3
aruna <ar********@yahoo.co.in> spoke thus:
What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?


1) It's a "qualifier", not a "specifier".
2) Read a C book. Or the comp.lang.c FAQ. Or its archives.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
aruna wrote:
What are the advantages of using volatile? Here are some:
1. The compiler does not "optimize-out" the variable that
is declared as volatile.
2. The compiler refreshes variables marked as volatile
before using them.

What are the practical situations where you use
volatile specifier?

My experience, in embedded systems, is to use volatile
when the hardware can change the value of a memory
location without the program's consent or knowledge.

I/O devices, such as a UART, is one example. The
value in the Receive Register depends on the other
end of the connection. A host computer could send
a character over and it resides in the Receive
Register, without the knowledge of the executing
program.

Sensors and triggers are also another example.
The sensor could be triggered without the program's
knowledge.

Message queues are another one. Your program could
receive a message from another task without any
prior knowledge and at unexpected times.

Memory locations that are shared by your program
and either a hardware device or another task.
Stuff could be placed into the shared memory
without your program knowing about it.

See also:
mutex
lock
semaphore
signal

I've seen replies about volatile being used for
multi-threading, but that is outside my knowledge
base. :-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #5
Thomas Matthews wrote:

I've seen replies about volatile being used for
multi-threading, but that is outside my knowledge
base. :-)


The fundamental concept remains the same there still. If multiple
threads are accessing a variable, it is important that the thread (when
it sees a variable x ) , actually gets the most recent value of x. This
would be taken care by the compiler to make sure that it refreshes
before fetching the value for the current thread, without doing any
optimization, thereby preserving the semantics of the multi-threaded
program.

- Karthik.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: newsock | last post by:
Why need to qualify a member function "volatile"? Why need to qualify a object "volatile"? When need to "const_cast" away "volatile" of an object and a member function? I also saw some code...
5
by: Joe Pizzi | last post by:
Is there any difference between the following two definitions? If so, can you explain them to me? typedef volatile struct { int x; } VS_Def; typedef struct {
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
5
by: ben | last post by:
Hello All, I am trying to make sense of a bit of syntax, is there a guru out there that can clear this up for me. I have a buffer declared as static volatile u8 buffer; and I have a...
14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
3
by: Rakesh Kumar | last post by:
Hi - I am actually trying to get my feet in multi-threaded C++ programming. While I am aware that the C++ standard does not talk about threads (at least, for now - in C++03) - my question is more...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.