Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Hello, and welcome. We're going to talk about another type qualifier in this lecture,
mainly the volatile type qualifier.
So this type qualifier tells the compiler explicitly that specified variable
will change its value, so it's sort of the opposite of constant.
So it's provided so that a program can tell the compiler to
suppress various kinds of optimizations.
It prevents the compiler from optimizing away
seemingly redundant assignments to a variable.
So it prevents the compiler from repeated examination of a variable
without its value seemingly changing.
So it essentially prevents the compiler
from caching variables.
So the reason for having this type qualifier is mainly because of the problems
that are encountered in real time or embedded systems programming.
Programs that have a lot of threading, programs where the resources are scarce
will use the volatile type qualifier.
So some use cases of when you should use this.
A variable should be declared volatile whenever its value could change unexpectedly.
The optimizer must be careful to reload the variable every time it is used instead of holding a copy.
You don't want the compiler to use caching.
So there's only three types of variables that should use volatile:
memory mapped profile registers,
global variables non-stack variables modified by interrupting
service routine and global variables accessed by multiple tax
within a multi-threaded application.
And usually, that's when you'll use it you'll use it when you're doing a lot of threading.
So the syntax, it's basically the same as it is for other type qualifiers.
You basically have to say volatile before the data type.
And this will say location one for example
is a volatile location.
Volatile int star ploc that points to a volatile location pointer right.
So loc1 is a volatile value ploc points to a volatile value.
So let's look at an example.
So here we have val 1 equals x. And then we have some code, it's not using x.
And then we have val2 equals x.
So this is just basically making assignments, and then in between those assignments,
you're doing all this other code and you're not accessing variable x.
A smart optimizing compiler might notice
that you only use x twice without changing its value.
So normally what the compiler do is it'll temporarily store the x value in a register
because it's not changing a lot, it will kind of cache it.
So when x is needed for val 2,
it can save time by just reading the value from the register instead of
from the original memory location. We know from the register keyword that registers
and storing things in there is much faster.
So sometimes you don't want this optimization, you don't want to use this value from the register.
So specifically it's not going to be desired if x is changed between
two statements by some other agency.
So you would use the volatile keyword to ensure that the compiler does not optimize this value away
and instead has a stored value for each variable.
You do not want to do this optimization because somewhere in here
maybe a thread somewhere is accessing this data
and you don't want to cache it because then it won't
it won't actually if you modify it it's not going to be reflected in val2.
So if the volatile keyword is not using declaration,
the compiler can assume that a value has not changed between these cases
and it can then attempt to optimize the code and we don't want that, specifically for threading.
Let's look at another example with an i/o port.
Suppose you have an output port. It's pointed to by a variable in your program.
So if you wanted to write two characters to the port: an o followed by an n,
you might say star output point equals o, star outport equals n.,
very simple assignments dereferencing and direction operator assigning characters.
A smart compiler might notice two successive assignments in the same location
because outport is not being modified in between the compiler would remove the first assignment
from the program.
So to prevent this from happening, you can declare outPort to be volatile
a volatile pointer right. You just say volatile char star outport
and you don't have this optimization happen.
You can also use volatile with constant.
It can be both constant and volatile.
A hardware clock setting normally should not be changed by the program.
You can make it constant,
however, it is changed by an agency other than the program
and an agency just means another process or another thread.
So you might want to make it volatile, so you don't do that caching and you don't do that optimization
because it will screw things up.
So you can use both qualifiers and declaration and the order does not matter.
Volatile constant int location or constant volatile int
star p location. And that's the gist between volatile.
You'll see it used more again with threading. So when we talk about threads in a later section,
you're going to get good taste of this keyword.
Most times you don't have to worry about it, but again it's really just an optimization,
telling the compiler do not optimize my value away
to be put into a register so that it's fast,
make sure that i get the most up-to-date value. Thank you.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.