473,661 Members | 2,477 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 1944

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
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******@mkwah ler.net> wrote in message news:<I1******* ***********@new sread1.news.pas .earthlink.net> ...
"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
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.goo gle.com...
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<I1******* ***********@new sread1.news.pas .earthlink.net> ...
"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
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******@mkwah ler.net> wrote in message news:<I1******* ***********@new sread1.news.pas .earthlink.net> ...
"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
> 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.calte ch.edu> wrote in message
news:M4iwb.2210 52$ao4.797519@a ttbi_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

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
2759
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
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
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:
6
2330
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 know it has to do with the expectation that the variable can be changed outside of the control of the program. I know it has to do with telling the compiler to not optimize that piece of code, so that the variable can be changed from outside of...
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" ?
20
1819
by: parasprajapati2001 | last post by:
can any one give an example which use volatile keyword ?
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...
16
1821
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 accesses to the members are byte-wide, or is the compiler free to read or read-modify-write in any data width it chooses? Is slapping a volatile on each member of the struct definition any different? better? worse?
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...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.