473,408 Members | 2,888 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,408 software developers and data experts.

volatile variable

can any one tell me whats the use of volatile variable,i know by declaring variable as volatile makes compiler not to optimize while compilation,but whats the use of not optimizing.can any one tell me with giving example.
Jun 12 '06 #1
1 7232
Banfa
9,065 Expert Mod 8TB
Easy if you work in embedded software (as I do) when you are accessing hardware external to the processor then you may wish to read or write to the same address more than once like so
Expand|Select|Wrap|Line Numbers
  1. #define CONFIG_REGISTER    ((ULONG *)0xC4000000)
  2. #define INPUT_REGISTER    ((ULONG *)0xC4000004)
  3.  
  4. *CONFIG_REGISTER = 0x80003000;
  5. *CONFIG_REGISTER = 0x40050000;
  6.  
  7. char data[10];
  8. int x;
  9.  
  10. for( x=0; x<10; x++ )
  11. {
  12.     data[x] = *INPUT_REGISTER;
  13. }
  14.  
As declared this wont work because the optomiser will optomise out the multiple consecutive reads or writes to the same location, the volitile specifier stops this by informaing the compiler that it can assume nothing about how that variable works so correctly written this is

Expand|Select|Wrap|Line Numbers
  1. #define CONFIG_REGISTER    ((volatile ULONG *)0xC4000000)
  2. #define INPUT_REGISTER    ((volatile ULONG *)0xC4000004)
  3.  
  4. *CONFIG_REGISTER = 0x80003000;
  5. *CONFIG_REGISTER = 0x40050000;
  6.  
  7. char data[10];
  8. int x;
  9.  
  10. for( x=0; x<10; x++ )
  11. {
  12.     data[x] = *INPUT_REGISTER;
  13. }
  14.  
Jun 13 '06 #2

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

Similar topics

17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
11
by: srinivas reddy | last post by:
Hi, Is there any chance that a program doesn' work properly even after a variable is declared as volatile? I remember somebody mentioning a scenario involving L1, L2 caches. Could anybody throw...
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...
17
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...
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
4
by: vijay.rajamanickam | last post by:
I am a newbie w.r.t. compiler optimizations and I believe these optimizations are generally done in function level. Can I safely assume that if I use getter & setter functions, I can effectively...
6
by: bwaichu | last post by:
Can someone help me out here? This is one of those areas of C that I am fuzzy on. I'm not quite sure when I should use volatile. If you could provide an example, that would help out a lot. I...
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...
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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,...

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.