473,795 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

volatile expression

Given this code:

extern volatile unsigned char v;

int main(void)
{
v;

return 0;
}

My understanding is that that "v;" is an expression statement which
should be "evaluated" as a void expression for side effects, but that
"evaluation " requires an operator. Does changing the statement to

v + 0;

or

(v);

change this?

I have some compilers which do and some compilers which do not access
"v" with the above code.

--
John W. Temples, III
Nov 14 '05 #1
14 2082
John Temples <us****@xargs-spam.com> scribbled the following:
Given this code: extern volatile unsigned char v; int main(void)
{
v; return 0;
} My understanding is that that "v;" is an expression statement which
should be "evaluated" as a void expression for side effects, but that
"evaluation " requires an operator. Does changing the statement to v + 0; or (v); change this? I have some compilers which do and some compilers which do not access
"v" with the above code.


"v;" is very much an expression, just like "v + 0;" and "(v);" are. If a
compiler does not evaluate the value of the volatile variable v in the
expression "v;", then it is breaking the standard. The C standard
expressly dictates that any mention of a volatile variable in an
expression must result in evaluation of that variable's value.
Non-volatile variables can be optimised away in such expressions but
volatile variables cannot.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
Nov 14 '05 #2
In article <news:6a******* *************@i swest.net>
John Temples <us****@xargs-spam.com> wrote:
[trimmed]
extern volatile unsigned char v;
v;
My understanding is that that "v;" is an expression statement which
should be "evaluated" as a void expression for side effects ...
I have some compilers which do and some compilers which do not access
"v" with the above code.


Here is a quote from a C99 draft (wording essentially unchanged from
the original 1989 ANSI C standard):

[#6] An object that has volatile-qualified type may be
modified in ways unknown to the implementation or have other
unknown side effects. Therefore any expression referring to
such an object shall be evaluated strictly according to the
rules of the abstract machine, as described in 5.1.2.3.
Furthermore, at every sequence point the value last stored
in the object shall agree with that prescribed by the
abstract machine, except as modified by the unknown factors
mentioned previously.99 What constitutes an access to an
object that has volatile-qualified type is implementation-
defined.

The last sentence is the key here: the implementation' s documentation
must state what make something an "access" to a volatile-qualified-type
object like "v".

So, open up the documentation that came with your compiler, see
whether it says "v;" constitutes an "access", and you will find
out whether the compiler should have generated one.

(In practice, given the option, I personally would reject -- or at
least seriously downgrade -- any compiler that does not attempt to
"load v" here, regardless of the contents of the implementation
document. Of course, one must often trade off obnoxious compiler
behavior against required compiler features. But if the document
says "v;" is an access, and the compiler generates no code, you have
obvious grounds for a bug report, at least.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
In article <ch**********@o ravannahka.hels inki.fi>,
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
John Temples <us****@xargs-spam.com> scribbled the following:
Given this code:
extern volatile unsigned char v;

int main(void)
{
v;

return 0;
}

[snippage]
"v;" is very much an expression, just like "v + 0;" and "(v);" are. If a
compiler does not evaluate the value of the volatile variable v in the
expression "v;", then it is breaking the standard.
Isn't that a little bit strong?
The C standard
expressly dictates that any mention of a volatile variable in an
expression must result in evaluation of that variable's value.


I thought it was any *access to* a volatile object, where exactly what
constitutes an access is implementation-defined.
So if the implementor, for some reason, chooses to not define this
as an access to v, then it's acceptable to not generate code for it.
(Whether such a compiler can be trusted to get volatile accesses in more
complex expressions "right" is of course a different matter.)
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Practical Solution #1: Kill the programmer in question. This is
also the most satisfying solution, because it ensures that the
problem will not recur. --Ben Pfaff in comp.lang.c
Nov 14 '05 #4
John Temples <us****@xargs-spam.com> writes:
Given this code:

extern volatile unsigned char v;

int main(void)
{
v;

return 0;
}

My understanding is that that "v;" is an expression statement which
should be "evaluated" as a void expression for side effects,
Yes.
but that
"evaluation " requires an operator.

[...]

Why do you think that evaluation requires an operator?

There is a flaw in the standard's definiion of "expression ".
C99 6.5p1 says:

An _expression_ is a sequence of operators and operands that
specifies computation of a value, or that designates an object or
a function, or that generates side effects, or that performs a
combination thereof.

which, taken literally, implies that v is not an expression, since it
contains no operators or operands. But it's clearly the intent that v
is an expression (see the grammar), and that it can be evaluated.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
On Tue, 7 Sep 2004 20:46:06 +0000 (UTC), dj******@csclub .uwaterloo.ca
(Dave Vandervies) wrote in comp.lang.c:
In article <ch**********@o ravannahka.hels inki.fi>,
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
John Temples <us****@xargs-spam.com> scribbled the following:
Given this code:

extern volatile unsigned char v;

int main(void)
{
v;

return 0;
}

[snippage]
"v;" is very much an expression, just like "v + 0;" and "(v);" are. If a
compiler does not evaluate the value of the volatile variable v in the
expression "v;", then it is breaking the standard.


Isn't that a little bit strong?
The C standard
expressly dictates that any mention of a volatile variable in an
expression must result in evaluation of that variable's value.


I thought it was any *access to* a volatile object, where exactly what
constitutes an access is implementation-defined.
So if the implementor, for some reason, chooses to not define this
as an access to v, then it's acceptable to not generate code for it.
(Whether such a compiler can be trusted to get volatile accesses in more
complex expressions "right" is of course a different matter.)
dave


I was going to jump on you, but it is just as well point you to Chris
Torek's detailed answer to the OP.

As for the bit about what constitutes an access to a volatile object
is implementation defined, I agree that the standard has muddied the
water for 15 years and the wording should be expanded to explain the
(claimed) intent.

Consider:

volatile unsigned char vuc [8];
/* ... */
int x = vuc [3];

On your average, everyday, Pentium, 64 bits (8 octets) are going to be
read from memory. Depending on the alignment of 'vuc', all eight of
those volatile unsigned chars may actually be read, even though all
but one of them will be ignored.

Supposedly the wording about what constitutes access is intended to
cover situations like this, meaning that a read of vuc [3] might well
cause a read of other elements. But it should clearly do a better job
of saying so.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
Keith Thompson wrote:
John Temples <us****@xargs-spam.com> writes:
but that
"evaluation " requires an operator.


[...]

Why do you think that evaluation requires an operator?


The C90 standard (to which the compilers I am testing claim to comply)
defines the term "evaluation " in 6.1.5 as "An operator specifies an
operation to be performed (an _evaluation_) [...]" C99 doesn't appear
to have this explicit definition of "evaluation ."

--
John W. Temples, III
Nov 14 '05 #7
John Temples <us****@xargs-spam.com> writes:
Keith Thompson wrote:
John Temples <us****@xargs-spam.com> writes:
but that
"evaluatio n" requires an operator.

[...]
Why do you think that evaluation requires an operator?


The C90 standard (to which the compilers I am testing claim to comply)
defines the term "evaluation " in 6.1.5 as "An operator specifies an
operation to be performed (an _evaluation_) [...]" C99 doesn't appear
to have this explicit definition of "evaluation ."


I've found that the standard's definitions, at least the ones defining
a term in italic type, are often incomplete. (See C99's definition of
"lvalue" for an example -- but that particular case has already been
argued to death in comp.std.c.) The more explicit definitions in
section 3 tend to be better. (In my opinion, disclaimer disclaimer,
blah blah.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
In <4v************ *************** *****@4ax.com> Jack Klein <ja*******@spam cop.net> writes:
As for the bit about what constitutes an access to a volatile object
is implementation defined, I agree that the standard has muddied the
water for 15 years and the wording should be expanded to explain the
(claimed) intent.


I strongly suspect that the standard is worded in such a way as to make
"volatile" next to useless (except in the context of signals and longjmp)
for purely political reasons that are far from volatile and so is the
wording in question.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9
In <6a************ ********@iswest .net> John Temples <us****@xargs-spam.com> writes:
Given this code:

extern volatile unsigned char v;

int main(void)
{
v;

return 0;
}

My understanding is that that "v;" is an expression statement which
should be "evaluated" as a void expression for side effects, but that
"evaluation " requires an operator. Does changing the statement to

v + 0;

or

(v);

change this?
No, the expressions are equivalent.
I have some compilers which do and some compilers which do not access
"v" with the above code.


So, avoid it. A much safer approach is:

extern volatile unsigned char v;
void nop(unsigned char);

int main()
{
nop(v);
return 0;
}

For best results, define nop() in a different file. In theory, a super
smart implementation could still optimise away the nop() call, but real
implementations are not that smart...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #10

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

Similar topics

4
5821
by: dwaach | last post by:
Hi, I have something like. struct X {}; X ox; X* pox=&ox; X*& volatile r =pox;
9
2761
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
2659
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
7914
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
19365
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:
17
2353
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 that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)." Does that mean that in the following code, *p does not have to be evaluated since its side...
9
5824
by: d.f.s. | last post by:
In the post below, 'copy constructor?', the answers refer to an object declared as const volatile. Now I'm confused. Are those terms not mutually exclusive? const='Hey compiler! This is not going to change so generate your code based on that assumption.' volatile='Hey compiler! This is going to change so generate your code based on that assumption.'
18
21606
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 memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data pointed to by the source pointer is volatile and not the pointer itself?
94
30357
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" ?
0
9522
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
10443
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...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
1
4113
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
3
2921
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.