Dear All,
Where to use volatile variable?
Thanks
Rajesh 9 3528
You can use the volatile in cases where the variable is pointing to a
location beyond the control of the program that declares the variable.
Hence the you indicate the compiler not to perform code optimizations on
that variable.And also the change in the value of the variable is not under
your programs control.
Example:
char far* ptr = (char far*)<farmemorylocation>
for(int k=0;k<10;k++)
int i=*ptr;
Here the code optimizer might do the following:
int i=*ptr;
Since the same location is read again and again....
But this might not serve your cause.Since the location "ptr" might be
changed by some other process;the above optimization does not make any
sense.
To avoid the compiler doing this you must indicate that the
location is volatile.
Regards,
Sriram Rajagopalan.
"Rajesh" <ra****@tridentinfosol.com> wrote in message
news:b2**************************@posting.google.c om... Dear All,
Where to use volatile variable?
Thanks Rajesh
Rajesh wrote: Dear All,
Where to use volatile variable?
For example when the variable needs to be read from memory
every time it is used. Normally the compiler will try to
make it so that variables are stored in registers.
This might be because the variables can change without the
program knowing because of they monitor some hardware.
--
Thomas.
"Rajesh" <ra****@tridentinfosol.com> wrote in message
news:b2**************************@posting.google.c om... Dear All,
Where to use volatile variable?
Thanks Rajesh
Volatile is used in hardware drivers and ports.
When incoming data is put into memory, you set that memory word / buffer to
volatile to indicate that to the compiler.
--
hash:a
-----BEGIN PGP SIGNATURE-----
a
-----END PGP SIGNATURE-----
In <b2**************************@posting.google.com > ra****@tridentinfosol.com (Rajesh) writes: Where to use volatile variable?
Anywhere the value of an object may change behind compiler's back.
My favourite example:
#include <stdio.h>
#include <signal.h>
volatile 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;
}
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) writes: In <b2**************************@posting.google.com > ra****@tridentinfosol.com (Rajesh) writes:
Where to use volatile variable?
Anywhere the value of an object may change behind compiler's back. My favourite example:
#include <stdio.h> #include <signal.h>
volatile 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; }
I don't see a guarantee in the standard that an object of type
sig_atomic_t can hold the value SIGINT (though I don't know of an
implementation where it can't).
--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes: Da*****@cern.ch (Dan Pop) writes: In <b2**************************@posting.google.com > ra****@tridentinfosol.com (Rajesh) writes:
>Where to use volatile variable?
Anywhere the value of an object may change behind compiler's back. My favourite example:
#include <stdio.h> #include <signal.h>
volatile 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; }
I don't see a guarantee in the standard that an object of type sig_atomic_t can hold the value SIGINT (though I don't know of an implementation where it can't).
If it cannot, the result will be implementation-defined. The program's
overall behaviour is not affected. In C89, at least.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) writes: In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes:Da*****@cern.ch (Dan Pop) writes: In <b2**************************@posting.google.com > ra****@tridentinfosol.com (Rajesh) writes:
>Where to use volatile variable?
Anywhere the value of an object may change behind compiler's back. My favourite example:
#include <stdio.h> #include <signal.h>
volatile 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; }
I don't see a guarantee in the standard that an object of type sig_atomic_t can hold the value SIGINT (though I don't know of an implementation where it can't).
If it cannot, the result will be implementation-defined. The program's overall behaviour is not affected. In C89, at least.
In C89, if the value of SIGINT doesn't fit in a sig_atomic_t, the
program's output will be incorrect; it might print, for example,
The program received signal 42.
even though the actual value of SIGINT might be, for example, 298. It
doesn't affect the program's flow of control (if that's what you mean
by "overall behavior"), but it certainly affects its behavior.
In C99 the conversion of the value of SIGINT to sig_atomic_t could
even raise an implementation-defined signal under certain
circumstances.
--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes: Da*****@cern.ch (Dan Pop) writes: In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes: >Da*****@cern.ch (Dan Pop) writes: >> In <b2**************************@posting.google.com > >> ra****@tridentinfosol.com (Rajesh) writes: >> >> >Where to use volatile variable? >> >> Anywhere the value of an object may change behind compiler's back. >> My favourite example: >> >> #include <stdio.h> >> #include <signal.h> >> >> volatile 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; >> } > >I don't see a guarantee in the standard that an object of type >sig_atomic_t can hold the value SIGINT (though I don't know of an >implementation where it can't). If it cannot, the result will be implementation-defined. The program's overall behaviour is not affected. In C89, at least.
In C89, if the value of SIGINT doesn't fit in a sig_atomic_t, the program's output will be incorrect; it might print, for example,
The program received signal 42.
even though the actual value of SIGINT might be, for example, 298. It doesn't affect the program's flow of control (if that's what you mean by "overall behavior"), but it certainly affects its behavior.
Who cares? The program still behaves as expected, and the probability
of this happening is zilch. You'd have a point if it were a real
life program (and not a demo) and if the wrong value would cause
unexpected/unintended behaviour.
In C99 the conversion of the value of SIGINT to sig_atomic_t could even raise an implementation-defined signal under certain circumstances.
I'm willing to bet no conforming C99 implementation will take advantage
of this piece of brain damage in the C99 standard, that breaks *correct*
C89 code, for no redeeming advantages.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) writes: In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes:
[...] In C89, if the value of SIGINT doesn't fit in a sig_atomic_t, the program's output will be incorrect; it might print, for example,
The program received signal 42.
even though the actual value of SIGINT might be, for example, 298. It doesn't affect the program's flow of control (if that's what you mean by "overall behavior"), but it certainly affects its behavior.
Who cares? The program still behaves as expected, and the probability of this happening is zilch. You'd have a point if it were a real life program (and not a demo) and if the wrong value would cause unexpected/unintended behaviour.
I care; I'm surprised you don't. Producing incorrect output hardly
qualifies as expected behavior.
The point is simply that sig_atomic_t is not a suitable type for
storing signal numbers.
--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;
|
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...
|
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...
|
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...
|
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...
|
by: Mark |
last post by:
Hi List,
I want to write a function to copy some data out of a hardware buffer.
The hardware can change the contents of this buffer without it being
written to by my function. I want to use...
|
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...
|
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...
|
by: S James S Stapleton |
last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been
testing to figure out what volatile does (fairly simple code, uses
pthreads). I have an update thread (variables passed as...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |