All language subtitles for 003 restrict.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) Download
zh-TW Chinese (Traditional) Download
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
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
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
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. In this lecture, we're going to talk about the restrict type qualifier.

So the restrict type qualifier, another optimization hint for the compiler

just like the other type qualifiers. The compiler can choose to ignore it.

It's used in pointer declarations as a type qualifier for pointers.

It tells the compiler that a particular pointer is the only reference

to the value it points to throughout its scope.

So what it means is the same value is not referenced by any other pointer

or variable within that scope.

The pointer is the sole initial means of accessing a data object.

This tells the compiler that it does not need to add any additional checks.

So without using this keyword, the compiler has to assume the worst case.

It's going to do extra things.

It has to assume that some other identifier might have changed the data in

between the two uses of the pointer.

And so with the restrict keyword used the compiler is free to look

for computational shortcuts, it can do some optimization.

So if a programmer uses restrict and they violate the above condition,

the result is going to be undefined behavior.

It's not supported by c++,

so it's not portable to c++.

The syntax is a little different. You have the restrict keyword after the pointer.

So you have int pointer restrict and then the name of the pointer.

So you do again int pointer restrict in pointer b.

This tells the compiler that for the duration of the scope

in which int pointer a and int pointer b are defined,

they will never access the same value.

So their use for pointing to integers inside an array is mutually exclusive.

They're never going to access that same value.

Let's look at an example in code blocks.

So here we are in code blocks. We're going to create some variables.

We're going to create an array of 10 elements, so we're just going to say int array

10, not the best name, but okay for now.

We're then going to say int star restrict.

So we're creating a restrict point here. We're going to say restar,

and we're going to say int star.

We're going to cast that because malloc returns a void pointer. We're going to say malloc,

and we're going to say 10 times size of int,

something like that.And we're going to have to include malloc.

And then we're going to do int star

par equals ar. We're going to assign a pointer to a pointer.

So the pointer restar here

is the sole initial means of access to the memory allocated by malloc.

The pointer par here

is neither the initial nor the sole means of access to the data in

the ar array, sorry this has to be array so in the array,

so it cannot be qualified as restrict. This cannot be qualified as restrict.

So now let's add some more code.

Here we're going to just do some assignments inside of an array.

So we're going to create a local variable for the array int n.

And then we're going to create a for loop where we're going to do some assignments.

So we have four n equals zero n is less than ten we're iterating through the array.

We're using par, and we're using restar.

And then we're also going to use array

and par and restar again. So restar is restrict.

The compiler can actually replace these two statements on lines 12 and 15

involving restar with a single statement having the same effect

because restar is the sole initial means of access to the block of data points to.

So it could actually --

the compiler can do something like say restar

n plus equals to eight.

So when it goes to compile this code and it generates an assembly language,

it can replace those two statements with just a single statement,

it optimizes it right because that's the sole initial means of access for this restar pointer.

So instead of having lines 12 and 15, get

created assembly code you can just create that one statement.

Now you can't do the same for par.

It would actually be a computational error if you try to condense the

two part statements on lines 11 and 14.

You can't condense those into one so. You would not be able to do

par of n plus equals eight right.

This is going to give the wrong answer.

And the reason it gives the wrong answer is because the loop uses array

to change the value of the data between the two times that par

accesses the same data.

So it can't actually do that.

And that's really the benefit of using the restrict.

So understand that difference because restar

is defined as restrict and it's the sole access

it can actually do some compiler optimizations.

Now you can also restrict with function parameters.

You can use the restrict keyword as a qualifier for a function parameter

that are pointers, so again it's only on pointers.

So what this means is that the compiler can assume that no other identifiers

modify the pointer to data within the body of the function.

So the compiler can try optimizations it might not otherwise use.

The c library has two functions for copying bytes from one location to the other

in c99. The m copy and the m move.

And if you looked at these function prototypes,

you would see them defined like this. And you'll notice the restrict keyword is in there.

But what these functions do is each one copies n bytes from location s2

to location s1.

So m copy requires that there be no overlap between the two locations.

Memmove does not have this requirement.

So declaring s1 and s2 as restrict

means each pointer is the sole means of access for mem copy.

They cannot access the same block of data

and this matches the requirement that there be no overlap.

Now mem move doesn't allow an overlap

so it has to be more careful about copying the data

so that it does not overwrite the data before it is used

and that's why it doesn't use restrict.

So that's mem copy and memmove. Let's look at our own function definition.

And what we want to do here is

we're going to create a function,

we're just going to call it f1.

And we're going to define it as first parameter n, second parameter

is going to be a restricted pointer a1

pointing to a foat and then it's also going to be a constant for the third parameter flow

restrict a2.

And then inside there, we're just going to basically

have a local variable and a loop,

something like this,

i equals 0. And then we're going to assign something a1 and a2.

Now a1 and a2 can be assumed

to refer to disjoint array objects.

They're both of them are restrict qualified.

So what this implies is that each iteration of the loop

is independent of the others.

And so the loop can be progressively optimized because of that restrict.

And so this is why you'd want to use restrict

you know when you're the sole axis of a pointer and you can't optimize.

Okay. So we might see restrict a lot more

we talk about the pointer section with double pointers but understand the basics

here of what the restrict can do concerning pointers. Thank you.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.