473,661 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Volatile

Dear All,
Where to use volatile variable?

Thanks
Rajesh
Nov 13 '05 #1
9 3556
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*)<farmemory location>
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****@trident infosol.com> wrote in message
news:b2******** *************** ***@posting.goo gle.com...
Dear All,
Where to use volatile variable?

Thanks
Rajesh

Nov 13 '05 #2
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.

Nov 13 '05 #3


"Rajesh" <ra****@trident infosol.com> wrote in message
news:b2******** *************** ***@posting.goo gle.com...
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-----

Nov 13 '05 #4
In <b2************ **************@ posting.google. com> ra****@tridenti nfosol.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
Nov 13 '05 #5
Da*****@cern.ch (Dan Pop) writes:
In <b2************ **************@ posting.google. com>
ra****@tridenti nfosol.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_Keit h) 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"
Nov 13 '05 #6
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:
Da*****@cern.c h (Dan Pop) writes:
In <b2************ **************@ posting.google. com>
ra****@tridenti nfosol.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
implementati on 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
Nov 13 '05 #7
Da*****@cern.ch (Dan Pop) writes:
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:
Da*****@cern.c h (Dan Pop) writes:
In <b2************ **************@ posting.google. com>
ra****@tridenti nfosol.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
implementati on 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_Keit h) 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"
Nov 13 '05 #8
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:
Da*****@cern.c h (Dan Pop) writes:
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:
>Da*****@cern.c h (Dan Pop) writes:
>> In <b2************ **************@ posting.google. com>
>> ra****@tridenti nfosol.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
>implementati on 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
circumstance s.


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
Nov 13 '05 #9
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_Keit h) 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"
Nov 13 '05 #10

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

Similar topics

4
8123
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 of a function argument of "volatile" type. What's the purpose there?
8
2638
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
7906
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 pointer to that buffer declared as
14
11961
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 can be accessed as an atomic entity, even in the presence of asynchronous interrupts." Does this mean that the use of "volatile" in the above declaration is redundant? (It sure sounds that way to me.)
14
19323
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 program flow and interrupts. At the organisation I work, instead of declaring a variable volatile, it is casted to volatile when necessary:
17
2338
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 that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)." Does that mean that in the following code, *p does not have to be evaluated since its side...
18
21547
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 memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data pointed to by the source pointer is volatile and not the pointer itself?
94
30273
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
3
3603
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 about the language / usage rather than any thread specific question. Sorry - if posted my mistake. I understand that in C++ volatile objects ( those non-primitive type instances qualified with 'volatile' ) can actually call only those member...
10
3425
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 volatile) and a print thread (one variable volatile, the other, not). There is no difference in the behavior of the volatile and nonvolatile thread. I'm compiling this with gcc, using the -O2 and -pthreads flags. The sudocode is at the end....
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.