473,378 Members | 1,422 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,378 software developers and data experts.

volatile

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?
class vClass
{
void foo() volatile {}
void boo(){}
};

int main()
{
volatile vClass v;

v.foo(); //no problem
v.boo(); //compile error
...
}

Thanks for your help!

Jul 19 '05 #1
4 8080
newsock wrote:
Why need to qualify a member function "volatile"?
Volatile is an attribute that precludes the compiler from making false
optimizations.

Consider this code.

int i = 0;

void thead1_func()
{
i = 1;
}

void thread2_func()
{
while ( ! i )
{
// wait for i to change
// oops - compiler optimizes
// reads to i away because ...
// nothing in this loop changes
// memory
}
}
However if we had declared

volatile int i = 0;

.... then the compiler would not be free to make this optimization.

Volatile is also heavily used in drivers because values of memory mapped
registers may change on their own !

Why need to qualify a object "volatile"?
When the state of the object may change when a different thread changes
it's contents.

When need to "const_cast" away "volatile" of an object and a member
function?
When you know that the object's state can't change without the current
thread making the change.

I also saw some code of a function argument of "volatile" type. What's the
purpose there?
class vClass
{
void foo() volatile {}
void boo(){}
};
I remember running into this once and I punted on it so I don't know for
sure but I suspect that it is similar to const in behaviour i.e. member
functions that may cause or depend on volatile behaviour are by
definition volatile and hence the object is volatile. But it's only a
guess.


int main()
{
volatile vClass v;

v.foo(); //no problem
v.boo(); //compile error
...
}

Jul 19 '05 #2
"newsock" <ne*****@hotmail.com> writes:
Why need to qualify a member function "volatile"?
It has a similar effect to declaring a member function "const":
The "this" pointer will point to a "volatile"-qualified equivalent
of the class type.
Why need to qualify a object "volatile"?
It forces the object's value to actually be checked each and
every time it is read. For example, in the following:

int foo = 2;

std::cout << foo;

A compiler would be well within its rights to optimize the second
line to:

std::cout << 2;

Since it knows that foo was not changed from its initial value
before the output. However, if it were declared:

volatile int foo = 2;

std::cout << foo;

The volatile keyword says that foo may be changed by some
mechanism *outside* of the program, so the compiler had better
double-check the value of foo before deciding to output it to
std::cout.

This is mainly useful if the is registered somehow by address
with an external process/thread, or in code such as the
following:

volatile int *foo = reinterpret_cast<int *>( 0x01f0 );
*foo = 2;
std::cout << *foo;

In the above example, foo is now a pointer to volatile int, whose
location is a hard-coded value that perhaps has some special
meaning in the system for which this particular program was
written: it might be that this value is mapped to (say) the
number of milliseconds this process has been running; or maybe
it's a pixel in video memory. Your program doesn't "know" this,
but it does know that this is a "special" int, and it should
assume that its value may have changed while it wasn't paying
attention.
When need to "const_cast" away "volatile" of an object and a member
function?
It's never needed; but provided that you were *sure* that an
object really isn't going to be changed on the sly, you could
cast it away for a potential efficiency boost.
I also saw some code of a function argument of "volatile" type. What's the
purpose there?
Are you sure? It has no purpose there: it is thrown out (as is
const). However, a parameter with a type *derived* from a
volatile type (say, pointer-to-volatile-something-or-other) could
be significant.
class vClass
{
void foo() volatile {}
void boo(){}
};

int main()
{
volatile vClass v;

v.foo(); //no problem
v.boo(); //compile error
Right. Same reason why it would fail if you changed the instances
of "volatile" in this snippet to "const": "this" points to a
volatile vClass, but boo() doesn't "accept" a volatile "this"; a
compiler would typically compile boo() with optimizations that
don't require that members of vClass (had they existed) be
checked for on-the-sly changes, which would not be acceptable for
instances that have been declared with the "volatile" qualifier.
Thanks for your help!


No prob!

--
Micah J. Cowan
mi***@cowan.name
Jul 19 '05 #3

Gianni Mariani wrote: ....

"plonk."

regards,
alexander.
Jul 19 '05 #4
Alexander Terekhov <te******@web.de> writes:
Gianni Mariani wrote: ....

"plonk."
Why? I saw nothing in Gianni's post that merited kill-filing.
regards,


(Did you really mean that? ;-) )

--
Micah J. Cowan
mi***@cowan.name
Jul 19 '05 #5

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

Similar topics

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...
1
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
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...

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.