473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

volatile and constant

13
Hi all

what is meant by volatile and const?

wher they can be used?
Jan 10 '07 #1
3 4143
horace1
1,510 Expert 1GB
Hi all

what is meant by volatile and const?

wher they can be used?
The keyword const in a definition or declaration indicates to the compiler that once the object is initialised it is to be treated as a constant and may not be altered.

The keyword volatile iindicates that the contents of the object are subject to unpredictable alterations and references to it must not be optimised. For example, it may be a variable which will be changed by an interrupt service routine, or an I/O register which will be changed by the action of the corresponding I/O device. If a volatile I/O register should not be changed by the program it can also be type modified with const, e.g.:
Expand|Select|Wrap|Line Numbers
  1. const volatile unsigned char io_register;                 // a byte sized I/O register  
  2.  
This declares a byte sized object io_register which may not be altered by the program but is subject to changes from outside the program.
Jan 10 '07 #2
Banfa
9,065 Expert Mod 8TB
the const keyword creates a variable that can't be altered, i.e. has a fixed or constant value. In the case of a pointer it can create a pointer that can't be altered or a pointer to something that can't be altered.

Expand|Select|Wrap|Line Numbers
  1. const int PI1000 = 3141;   /* Constant integer */
  2. const int *pcInt = &PI1000; /* pointer to an integer that is constant, the pointer can be changed but the value of the integer it points to can't */
  3. const int const *pcInt = &PI1000; /* Constant pointer to an integer that is constant, the pointer can't be changed and neither can the value of the integer it points */
  4. int x;
  5. int *pcInt = &x; /* Constant pointer to an integer, the pointer can't be changed but the value of the integer it points to can be changed */
  6.  
The volatile keyword marks a variable about which the compiler can make no assumptions. This means that the compiler can not optomise uses of the variable. It would be used in the situation where you have a pointer to a memory location that could be changed outside the control of the main processor (say the receive register location of a UART).

I have mainly seen it used with pointers, so pointer to a volotile unsigned integer

volatile unsigned int *UART_RX_REGISTER;
Jan 10 '07 #3
Geevi
13
The keyword const in a definition or declaration indicates to the compiler that once the object is initialised it is to be treated as a constant and may not be altered.

The keyword volatile iindicates that the contents of the object are subject to unpredictable alterations and references to it must not be optimised. For example, it may be a variable which will be changed by an interrupt service routine, or an I/O register which will be changed by the action of the corresponding I/O device. If a volatile I/O register should not be changed by the program it can also be type modified with const, e.g.:
Expand|Select|Wrap|Line Numbers
  1. const volatile unsigned char io_register;                 // a byte sized I/O register  
  2.  
This declares a byte sized object io_register which may not be altered by the program but is subject to changes from outside the program.

Thnks a lot...
Jan 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alexander Pope | last post by:
db2 udb v8 AIX in our oltp application we have a few dynamic tables that may contain 0 to 5000 rows. the tables have about 5-8 columns, totalling 50 bytes. depending on the time of the month,...
2
by: LBJ | last post by:
I have the following C code: //pHeadLoc contains an address that can be modified externally, //therefore it is a volatile pointer to a pointer volatile int** const pHeadLoc = (int**)0x456000; ...
8
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
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...
14
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...
94
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...
20
by: parasprajapati2001 | last post by:
can any one give an example which use volatile keyword ?
3
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...
6
by: Rahul | last post by:
Hi Everyone, The following code compiles without any error, int main() { volatile const int a = 10; return(0); };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.