LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2004, 02:09 PM   #1
vijeesh_ep
Member
 
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35

Rep: Reputation: 15
Question about "volatile"


Can u explain what the volatile keyword in c means.(It was a job interview question )
 
Old 08-22-2004, 02:18 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
If a variable is classified as volatile, the compiler will not optimize-out any reads or writes to that variable. This is usually done when you have a pointer and that pointer references a memory-mapped device. In other words, your pointer locates a device's control register. Storing a value would likely cause the hardware to perform some action. Likewise, reading from the location could provide a status of the device, or might dequeue some data from the device.

Under normal circumstances, this code:
Code:
 char *some_var_pointer;
 ...
 *some_var_pointer = 0x1B;
 *some_var_pointer = 0xF2;
would be replaced with:
Code:
 char *some_var_pointer;
 ...
 *some_car_pointer = 0xF2;
because the compiler thinks you are just overwriting the previous value; that the 0x1B was an unnecessary write. Using:
Code:
 volatile char *some_var_pointer;
will force the machine to perform both assignments.
 
Old 08-23-2004, 10:00 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
i think basically it refers to a variable which can be changed
by external means. Say it is a bit of hardware that is a temperature
sensor or maybe points at the system clock.
 
Old 08-23-2004, 11:30 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Here's a longer explanation with examples: http://www.embedded.com/story/OEG20010615S0107
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
what is "sticky bit mode" , "SUID" , "SGID" augustus123 Linux - General 10 08-03-2012 04:40 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration