473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const volatile???

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.'

Or am I out to lunch on my understanding of these two words?

Drew
May 17 '06 #1
9 5829
d.f.s. wrote:
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?
Definitely not.
const='Hey compiler! This is not going to change so generate
your code based on that assumption.'
No, that's not all. OOH, it could be "hey, it's not going to change",
OTOH it could just as well be "*you* are not [allowed] to change it".
volatile='Hey compiler! This is going to change so generate
your code based on that assumption.'

Or am I out to lunch on my understanding of these two words?


Not completely, but you're making wrong assumptions and conclusions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #2
[snip]
const='Hey compiler! This is not going to change so generate
your code based on that assumption.'


No, that's not all. OOH, it could be "hey, it's not going to change",
OTOH it could just as well be "*you* are not [allowed] to change it".


Right. It is a promise the coder makes , which the compiler will
enforce upon anyone who writes code to handle the object. It
protects the object & -used to be- helps the compiler to optimize
its' output. I think that I've got that one.
volatile='Hey compiler! This is going to change so generate
your code based on that assumption.'

My understanding of 'volatile':
A key word which indicates that an object is likely to be
or WILL be modified. Redundant for objects not
declared const. Was used to help older compilers to
do optimization. Sort of like 'register', it is seldom used
or needed with modern compilers.

I assume that my understanding of 'volatile' is wrong.

Drew
May 17 '06 #3
d.f.s. <no**@none.ni t> wrote:
My understanding of 'volatile':
A key word which indicates that an object is likely to be
or WILL be modified. Redundant for objects not
declared const. Was used to help older compilers to
do optimization. Sort of like 'register', it is seldom used
or needed with modern compilers.

I assume that my understanding of 'volatile' is wrong.


Here's an article by Andrei Alexandrescu on the 'volatile' keyword:

volatile - Multithreaded Programmer's Best Friend
http://www.ddj.com/showArticle.jhtml...leID=184403766

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 17 '06 #4
d.f.s. wrote:
[..]
My understanding of 'volatile':
A key word which indicates that an object is likely to be
or WILL be modified. Redundant for objects not
declared const.
No, it's not.
Was used to help older compilers to
do optimization. Sort of like 'register', it is seldom used
or needed with modern compilers.

I assume that my understanding of 'volatile' is wrong.


Yes, but not entirely. 'volatile' indicates an object that can change
by means other than the program in which it appears. It could be some
address that a device (through a driver or DMA means) can update based
on a change of its state (serial port receives another input, etc.)

Declaring a variable 'volatile' makes the compiler generate code that
reads the value from the variable's storage any time it's needed in
an expression, precluding the compiler from caching the value or some
other optimization WRT the variable's value.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #5
> My understanding of 'volatile':
A key word which indicates that an object is likely to be
or WILL be modified. Redundant for objects not
declared const. Was used to help older compilers to
do optimization. Sort of like 'register', it is seldom used
or needed with modern compilers. I assume that my understanding of 'volatile' is wrong.


You understood correctly (as in, it's wrong). 'volatile' isn't simple a
cute way of saying "not const". It informs the compiler that it
shouldn't expect to know whether the variable changes or not based on
what it compiles. This is used when you make a variable point to an
area of memory for example. In practice, this means that the program
has to recheck the variable every time it's used rather than apply some
sly optimization because it sees you yourself didn't change it in the
code.

May 17 '06 #6
Ok. So a good example would be:
const volatile clock;

const in that YOU are not allowed to change it.
volatile in that it will change so look at it whenever
CheckTime() is called.

If right, thank you all

Drew
May 17 '06 #7
d.f.s. wrote:
Ok. So a good example would be:
const volatile clock;

const in that YOU are not allowed to change it.
volatile in that it will change so look at it whenever
CheckTime() is called.


Exactly.

const is "This code is not allowed to modify this variable.
volatile is "This variable may change due to forces unrelated to this code".

Example:

const volatile unsigned long * const MEMORY_MAPPED_R EG =
reinterpret_cas t<const volatile unsigned long *>(0xFFE00000L) ;

MEMORY_MAPPED_R EG is a read only h/w register at FFE00000. It
can't be written to, but may change due to external issues.
May 17 '06 #8
Victor Bazarov wrote:
d.f.s. wrote:
[..]
My understanding of 'volatile':
A key word which indicates that an object is likely to be
or WILL be modified. Redundant for objects not
declared const.
No, it's not.
Was used to help older compilers to
do optimization. Sort of like 'register', it is seldom used
or needed with modern compilers.

I assume that my understanding of 'volatile' is wrong.


Yes, but not entirely. 'volatile' indicates an object that can change
by means other than the program in which it appears.


Or that it may be read by something else than that program.
It could be some address that a device (through a driver or DMA means) can
update based on a change of its state (serial port receives another input,
etc.)

Declaring a variable 'volatile' makes the compiler generate code that
reads the value from the variable's storage any time it's needed in
an expression,
And also writing it.
precluding the compiler from caching the value or some other optimization
WRT the variable's value.


volatile is quite commonly used in low-level code that communicates directly
with memory-mapped hardware registers or when transfering data into/out of
an interrupt routine. In this case, it's important that any read and write
operation is guaranteed to be actually performed. It boils down to: "The
as-if rule is not enough."

May 17 '06 #9
red floyd wrote:
Example:

const volatile unsigned long * const MEMORY_MAPPED_R EG =
reinterpret_cas t<constÂ*volati leÂ*unsignedÂ*l ongÂ**>(0xFFE00 000L);

MEMORY_MAPPED_R EG is a read only h/w register at FFE00000.Â*Â*It
can't be written to, but may change due to external issues.


The canonical example here is the system clock...

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 17 '06 #10

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

Similar topics

17
3280
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
2
1387
by: coolrama | last post by:
what does this declaration mean const * volatile * Is it valid. Is it used somewhere? Thanks Rama
16
41780
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the declaration of a const pointer? Is the following program legal C? int main(int argc, char *argv) {
1
3114
by: Mantorok Redgormor | last post by:
strictly speaking we can't say that const means "read only" but instead that it means "do not modify me please" since an object can be both modifiable and not modifiable or volatile and const, right? and if an implementation chose to put things that are const in read only memory if that const thing was also volatile then in that case, it would not be in
9
5377
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1; Const struct Type2 *Field2;
2
1570
by: FT | last post by:
Hi all: In the standuard 9.3.2, it says: >The type of /this/ in a member function of a class X is X*. If the member >function is declared const, the type of /this/ is const X*, if the member >function is declared volatile, the type of /this/ is volatile X*, and if the >member function is declared const volatile, the type of this is const volatile X*. IMO, the /this/ pointer can not be used as a l-value, so its type should be X* const...
7
1496
by: nospam | last post by:
Hi all, I have this struct typedef: typedef struct { int * test; }testst; and have this declaration:
4
2982
by: Olaf | last post by:
Hi, is there a way to declare an array with const variables like: static const ushort RX_BUF_SIZE = 0x100; typedef struct { volatile ushort buf; volatile unsigned head; volatile unsigned tail;
29
1877
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment: main() { const int p =5; int* pp = &p; ++(*pp);
0
9587
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
10588
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
10340
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...
1
10324
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
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
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
4302
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
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
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.