473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

volatile variable in writing driver

1 New Member
what is the exact use of volatile variables in writing device drivers
Dec 11 '06 #1
2 3945
rengaraj
168 New Member
Normal variables { without the 'volatile' type specifier } can be stored in the cache to make the CPU access faster. There might be some situations where you don't want to cache a particular variable and you want to read the variable always from the specified location instead from cache. You declare those kind of variables as volatile.
Jan 31 '07 #2
Motoma
3,237 Recognized Expert Specialist
what is the exact use of volatile variables in writing device drivers
Wecome to The Scripts.
The volatile keyword specifies that that variable may be changed from the outside. The biggest reason for this is to prevent an optimizing compiler from incorrectly removing checks.

Here is a nice excerpt from Wikipedia:

Expand|Select|Wrap|Line Numbers
  1. void foo(void)
  2. {
  3.     int *addr;
  4.     addr = 100;
  5.  
  6.     *addr = 0;
  7.  
  8.     while (*addr != 255)
  9.         ;
  10. }
  11.  
In this example, the code sets the value stored at location 100 in the computer system to 0. It then starts to poll the address until it changes to 255.

An optimizing compiler will assume that no other code will change the value stored in location 100 and so it will remain equal to 0. The compiler will then replace the while loop with something similar to this:

Expand|Select|Wrap|Line Numbers
  1. void foo(void)
  2. {
  3.     int *addr;
  4.     addr = 100;
  5.  
  6.     *addr = 0;
  7.  
  8.     while (TRUE)
  9.         ;                      /* do nothing, just loop */
  10. }
  11.  
and the program will loop forever.
Jan 31 '07 #3

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

Similar topics

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...
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;
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...
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...
0
by: bazzer | last post by:
hey, i am using visual basic.net 2003 and have an ASP.NET webform application thats accessing a microsoft access 2003 database. i kept getting the following error when i tried to run it: ERROR ...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
9
by: d.f.s. | last post by:
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...
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...
3
by: C++Liliput | last post by:
It seems that the keyword "volatile" is used to make sure that threads reading (or writing to) the same data should see a consistent picture of the variable i.e. updates made to the common data...
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...
1
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...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.