473,326 Members | 2,110 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,326 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 8077
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...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.