473,508 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

volatile inhibits side-effects

does volatile really inhibit side effects?
that is the rules for sequence points and side effects do not apply to
volatile objects?

--
nethlek
Nov 13 '05 #1
13 1931

"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
does volatile really inhibit side effects?
No. Where did you hear that?
that is the rules for sequence points and side effects do not apply to
volatile objects?


No. Where did you hear that?

'volatile' is intended for preventing possible compiler
optimization, so that a 'volatile' qualified object can
acquire a state from an external source without the compiler
'optimizing it away'.

-Mike
Nov 13 '05 #2
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<I1******************@newsread1.news.pas.eart hlink.net>...
"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
does volatile really inhibit side effects?


No. Where did you hear that?
that is the rules for sequence points and side effects do not apply to
volatile objects?


No. Where did you hear that?

'volatile' is intended for preventing possible compiler
optimization, so that a 'volatile' qualified object can
acquire a state from an external source without the compiler
'optimizing it away'.

-Mike


6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?

--
nethlek
Nov 13 '05 #3

"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<I1******************@newsread1.news.pas.eart hlink.net>...
"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
does volatile really inhibit side effects?


No. Where did you hear that?
that is the rules for sequence points and side effects do not apply to
volatile objects?


No. Where did you hear that?

'volatile' is intended for preventing possible compiler
optimization, so that a 'volatile' qualified object can
acquire a state from an external source without the compiler
'optimizing it away'.

-Mike


6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.


Right. But the use of the term 'side effect' here is not
what I 'traditionally' think of as 'side effect', i.e.
an executable construct such as obj++. Sorry for any
confusion.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


Yes, I suppose so, but I wouldn't testify to it in court. :-)
Let's see if some 'guru' will respond more definitively.

-Mike
Nov 13 '05 #4
Mantorok Redgormor wrote:


6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


Yes, but the statements in your first post don't follow from that. It's
not clear to me why you would conclude from the passage above that
'volatile' inhibits side-effects. Actually, it's not clear to me what
you mean by "inhibits side-effects", either.

The passage above is for situations such as memory-mapped I/O, when the
value at a particular memory location can change from one read to the
next, for example:

volatile char *vp = (char *)0x802a0010; /* Some device or port address */

char c1, c2;

c1 = *vp;
c2 = *vp;

Normally this could be optimized to something like this:

Get value from address vp, store in register A.
Store value from register A in c1.
Store value from register A in c2.

Because the compiler would normally assume that *vp cannot change unless
it emits code to change it. But since *vp is volatile, the compiler does
not make this assumption, and emits code to actually do two separate reads:

Get value from address vp, store in register A.
Store value from register A in c1.
Get value from address vp, store in register B.
Store value from register B in c2.

While I'm talking specifically about reading volatile objects, the write
situation is analogous. E.g., if you were to write to an object twice
without any kind of read in between, the compiler could assume the first
write is useless and eliminate it, but if the object is volatile, the
compiler won't make that assumption.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #5
Mac
On Sat, 22 Nov 2003 11:30:47 +0000, Mantorok Redgormor wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<I1******************@newsread1.news.pas.eart hlink.net>...
"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
> does volatile really inhibit side effects?
No. Where did you hear that?
> that is the rules for sequence points and side effects do not apply to
> volatile objects?


No. Where did you hear that?

'volatile' is intended for preventing possible compiler
optimization, so that a 'volatile' qualified object can
acquire a state from an external source without the compiler
'optimizing it away'.

-Mike


6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


Yes, but not by you, the programmer. My understanding of volatile is that
it is principally used when a hardware mechanism exists that can change
the data without the compiler knowing about it. In a way, it is wrong to
think in terms of sequence points, since the modification, if any, could
happen any time (in principal). It is completely outside the sequence
point frame of reference.

I don't see how this removes the requirement that you refrain from
modifying a variable twice between sequency points, etc.


--
nethlek


Mac
--

Nov 13 '05 #6
On Sat, 22 Nov 2003 11:30:47 -0800, Mantorok Redgormor wrote:
6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


I think you are reading this "backward". The standard is not trying to say
that if you declare a variable volatile, then the implementation is free
to change the variable whenever it wants to.

The point of volatile is that you, the programmer, use volatile to tell
the implementation that the value of the object can be changed "behind the
implementation's back".

The result of this is that when you declare an object volatile, the
implementation can not assume that it "knows" the value of the object.
Every time the object is referenced in your program, the implementation
must check the object to see if the value has changed. This prevents the
implementation from optimizing accesses to the variable by, for example,
keeping the value of the variable in a register. The implementation must
reload the value of the object every time it is referenced in the program.


Nov 13 '05 #7
Mantorok Redgormor wrote:

6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


PL/I has a similar attribute called ABNORMAL. One explanation I saw for
it said that for a statement such as J=I*I; the value of I should be
fetched from memory twice.

In C, it is sometimes used for memory mapped I/O devices, where each
load might return a different value. Consider a memory mapped hardware
random number generator, for example.

-- glen

Nov 13 '05 #8
In <pa****************************@bar.net> "Mac" <fo*@bar.net> writes:
On Sat, 22 Nov 2003 11:30:47 +0000, Mantorok Redgormor wrote:
6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


Yes, but not by you, the programmer. My understanding of volatile is that
it is principally used when a hardware mechanism exists that can change
the data without the compiler knowing about it.


It need not be a hardware mechanism, there is also a software mechanism
that can do that: it's called signal handler.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9
Volatile variables are useful in AST, thread, and shared memory programming.
Of course, if the variable manipulations are not atomic (which I may be
misinterpreting as your concept of modification between sequence points)
then other concepts, such as locking, become necessary (if allowable).

Volatile is a step towards opening a Pandoras box on Programming concepts
that can cause great headaches. :)

~~~^~~^~~^~~^~~^~~^^^~~~~^^~
Michael Steve
To err is human, to really screw something up requires a manager.

"glen herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message
news:M4iwb.221052$ao4.797519@attbi_s51...
Mantorok Redgormor wrote:

6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified between sequence points?


PL/I has a similar attribute called ABNORMAL. One explanation I saw for
it said that for a statement such as J=I*I; the value of I should be
fetched from memory twice.

In C, it is sometimes used for memory mapped I/O devices, where each
load might return a different value. Consider a memory mapped hardware
random number generator, for example.

-- glen

Nov 13 '05 #10
Dan Pop wrote:

(after someone wrote)
Yes, but not by you, the programmer. My understanding of volatile is that
it is principally used when a hardware mechanism exists that can change
the data without the compiler knowing about it.
It need not be a hardware mechanism, there is also a software mechanism
that can do that: it's called signal handler.


Well, yes, but a signal handler won't be invoked without a signal, which
usually requires some hardware.

-- glen

Nov 13 '05 #11
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
Dan Pop wrote:

(after someone wrote)
Yes, but not by you, the programmer. My understanding of volatile is that
it is principally used when a hardware mechanism exists that can change
the data without the compiler knowing about it.

It need not be a hardware mechanism, there is also a software mechanism
that can do that: it's called signal handler.


Well, yes, but a signal handler won't be invoked without a signal, which
usually requires some hardware.


A signal may well be generated by invocation of the raise() function.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #12
In <lUBwb.224931$9E1.1239324@attbi_s52> glen herrmannsfeldt <ga*@ugcs.caltech.edu> writes:
Dan Pop wrote:

(after someone wrote)
Yes, but not by you, the programmer. My understanding of volatile is that
it is principally used when a hardware mechanism exists that can change
the data without the compiler knowing about it.

It need not be a hardware mechanism, there is also a software mechanism
that can do that: it's called signal handler.


Well, yes, but a signal handler won't be invoked without a signal, which
usually requires some hardware.


Running the program requires some hardware in the first place. Most
standard signals don't require any additional hardware: SIGABRT,
SIGFPE, SIGILL, SIGSEGV, SIGTERM. It's only SIGINT that is typically
(but not necessarily) associated with the presence of a keyboard.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13
Mac
On Mon, 24 Nov 2003 16:19:43 +0000, Dan Pop wrote:
In <pa****************************@bar.net> "Mac" <fo*@bar.net> writes:
On Sat, 22 Nov 2003 11:30:47 +0000, Mantorok Redgormor wrote:
6.7.3#6
An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects.

wouldn't this mean that an object which is volatile could even be modified
between sequence points?


Yes, but not by you, the programmer. My understanding of volatile is that
it is principally used when a hardware mechanism exists that can change
the data without the compiler knowing about it.


It need not be a hardware mechanism, there is also a software mechanism
that can do that: it's called signal handler.

Dan


Thanks. I hadn't thought of that (obviously).

Mac
--

Nov 13 '05 #14

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

Similar topics

8
397
by: David Rasmussen | last post by:
What is the difference in meaning and in consequences between struct S { volatile A* a; }; and struct S
9
2748
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
8
2609
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
7894
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
19235
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...
6
2320
by: bwaichu | last post by:
Can someone help me out here? This is one of those areas of C that I am fuzzy on. I'm not quite sure when I should use volatile. If you could provide an example, that would help out a lot. I...
94
30186
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...
20
1790
by: parasprajapati2001 | last post by:
can any one give an example which use volatile keyword ?
3
3579
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...
16
1804
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
0
7224
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,...
0
7379
jinu1996
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...
1
7038
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...
0
7493
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...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4706
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.