All language subtitles for 002 volatile.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.