473,320 Members | 2,104 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,320 software developers and data experts.

Volatile

Dear All,
Where to use volatile variable?

Thanks
Rajesh
Nov 13 '05 #1
9 3524
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

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****@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-----

Nov 13 '05 #4
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
Nov 13 '05 #5
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"
Nov 13 '05 #6
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
Nov 13 '05 #7
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"
Nov 13 '05 #8
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
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_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"
Nov 13 '05 #10

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...
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...
18
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...
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...
10
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.