We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,721 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Const volatile

Hi All,
i want to know what is the use of const volatile variable, where exactly it is required to use.

6
Contributors
9
Replies
1 Year
Discussion Span
2 Years Ago
Last Updated
11
Views
Abhishek_jn
Newbie Poster
6 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You mean like const volatile type variblename; , or the difference between the two?

MosaicFuneral
Posting Virtuoso
1,699 posts since Nov 2008
Reputation Points: 888
Solved Threads: 118
Skill Endorsements: 7

Yeah,
i mean const volatile variable type.

Abhishek_jn
Newbie Poster
6 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

const means the program can not modify the value volatile means the value may be arbitrarily modified outside the program.

the two are separate and not mutually exclusive.

use them together, for instance, in the case of reading a hardware status register. const prevents the value from being stomped on, while volatile tells the compiler that this value can be changed at any time external to the program.

this const volatile <type> <variablename> will thus satisfy both requirements and prevent an optimizing compiler from incorrectly optimizing the code, that it would do if only "const" were used.

jephthah
Posting Maven
2,592 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Skill Endorsements: 5

Hi All,
i want to know what is the use of const volatile variable, where exactly it is required to use.

Let's take an example

const volatile usigned int *REG_A = (usigned int *) init_hardware_n_return_address of REG_A();

In the above snippet function "init_hardware_n_return_address of REG_A()" modifies the content of the REG_A( register A say) of a peripheral device , say after initiatialising it ( which cause the content of R/W register REG_A modified by peripheral itself) and returns the address of REG_A to the program .


The assignment in the above snippet implies here that content of REG_A can be modifiable only be external sources like peripheral hardware itself and and your code is not supposed to modify the content of REG_A .
Also whenever such variable is encountered compiler should "load " it value every time instead of doing any code optimasation

Typically memory mapped variables are one consumers for such usage

gkishore
Newbie Poster
1 post since Apr 2009
Reputation Points: 28
Solved Threads: 0
Skill Endorsements: 0

Thanks gkishore
for nice explaination.

Abhishek_jn
Newbie Poster
6 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

i get no love :(

jephthah
Posting Maven
2,592 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Skill Endorsements: 5

i get no love :(

Really sorry jephthah :)
Thanks for your nice explanation.

Abhishek_jn
Newbie Poster
6 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Just adding on what gkishore said...

Declaring a variable as const indicates the compiler that the variable will never be changed(either by the program/external entity like a peripheral device.

Declaring a variable as volatile indicates the compiler that the variable might be changed dynamically (by an external entity or by our program). Hence whenever we are accessing that variable, compiler will not perform optimization and will fetch the value from its address(which will cost us a few more cpu cycles).

Now Declaring a variable as a const volatile, we indicate the compiler that variable cant be modified by the program but can be modified by an external entity.

The usage of const volatile is more predominant in Device Driver Programming.

Saikrishna_bhi
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Kindly check the dates please.

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0876 seconds using 2.73MB