Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Corsican
Croatian
Czech
Danish
Dutch
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
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
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.