Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const volatile???

Reply
Thread Tools

const volatile???

 
 
d.f.s.
Guest
Posts: n/a
 
      05-17-2006
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2006
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


 
Reply With Quote
 
 
 
 
d.f.s.
Guest
Posts: n/a
 
      05-17-2006
[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


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      05-17-2006
d.f.s. <(E-Mail Removed)> 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2006
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


 
Reply With Quote
 
Selwyn Alcantara
Guest
Posts: n/a
 
      05-17-2006
> 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.

 
Reply With Quote
 
d.f.s.
Guest
Posts: n/a
 
      05-17-2006
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


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      05-17-2006
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_REG =
reinterpret_cast<const volatile unsigned long *>(0xFFE00000L);

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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      05-17-2006
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."



 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      05-17-2006
red floyd wrote:

> Example:
>
> const volatile unsigned long * const MEMORY_MAPPED_REG =
> reinterpret_cast<const¬*volatile¬*unsigned¬*long¬**>(0xFFE00000L);
>
> MEMORY_MAPPED_REG 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!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments