All language subtitles for Pointers-in-C-C-Full-Course_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
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
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

The course you're about to watch is about pointers\xa0\n

instructors from my code school. My Code School is\xa0\n

and has inspired many developers and creators.\xa0\n

article about the channels amazing story.\xa0\n

is a very fundamental and important concept in\xa0\n

find it difficult to understand pointers. So in\xa0\n

And all you need to know to understand this\xa0\n

how to declare a variable and how to do\xa0\n

Okay, so let's get started. To understand\xa0\n

various data types or various variables are stored\xa0\n

figure in the right here is computer's memory. And\xa0\n

program execution, we mostly talk about\xa0\n

we often say that my machine has got two GB of\xa0\n

Now let's say these segments, or partitions that\xa0\n

of memory. Now in a typical memory architecture\xa0\n

each byte of the memory has an address. So let's\xa0\n

assume to be the lowest by deep down here has an\xa0\n

as we go towards the top. So we go on like 012.\xa0\n

201. So the next byte will have an address of 202.\xa0\n

when we declare a variable in our program, let's\xa0\n

type integer, then when this program executes,\xa0\n

corresponding to this particular variable.\xa0\n

the data type, and also upon the compiler. So\xa0\n

is allocated four bytes of memory. Character\xa0\n

float is allocated four bytes of memory and\xa0\n

so as soon as the computer sees a declaration,\xa0\n

it knows that this is an integer variable, so\xa0\n

say in our example, it allocates memory starting\xa0\n

an internal structure, a lookup table, where it\xa0\n

a, it is of type integer, and it is located at\xa0\n

the variable. Now, if we declare another variable,\xa0\n

which is of type character, now once again,\xa0\n

it knows that it is a character variable, so it\xa0\n

free space, let's say in this case, it allocates\xa0\n

it keeps an entry for it in an internal structure\xa0\n

and its addresses 209. Now when we perform some\xa0\n

if we initialize a to five, when our machine or\xa0\n

the lookup table for this variable A. So it finds\xa0\n

at address 204. So, it goes at address 204 and in\xa0\n

five now in reality, the value is written in\xa0\n

are writing here in decimal form. Now once again,\xa0\n

after these statements, we have another statement\xa0\n

that, he has to be incremented, it again looks\xa0\n

modifies this value at this particular address.\xa0\n

the value six now. Now, all of this is really\xa0\n

In our program, or can we operate upon\xa0\n

Well, yes, you can do so in a C or c++ program\xa0\n

variables that store the address of another\xa0\n

a little better, I'll redraw a couple of these\xa0\n

at address two sort of food that stores an integer\xa0\n

the type of which is pointer to integer. And\xa0\n

Now this variable p can store the address of\xa0\n

operators upon p, we can reach a. Now p also\xa0\n

at location address 64. And it also takes four\xa0\n

another integer. So let's say if we\xa0\n

named B and having value 10. And if we change the\xa0\n

me. Let us now see the syntax of pointer variables\xa0\n

the data type and a variable name. So int a means\xa0\n

to write a pointer variable that should point to\xa0\n

sign before the variable. So P is now a pointer\xa0\n

variable that can store the address of an integer.\xa0\n

use a statement like p is equal to ampersand a.\xa0\n

it gives us the address of that particular\xa0\n

to that particular variable. Okay, so let's try\xa0\n

declared the variable A, then it was allocated an\xa0\n

integer p, let's say it was allocated\xa0\n

like p is equal to ampersand A, then P now has\xa0\n

Let's initialize a to some value let's say is\xa0\n

Now, what will happen if I print p, what will\xa0\n

Okay, so if we try to print p, then the output\xa0\n

we simply try to print ampersand of a ampersand\xa0\n

also be 204. What if we want to print ampersand\xa0\n

stored in the memory. So ampersand since it prints\xa0\n

So, printing ampersand of P should give us\xa0\n

one more important property of pointer, if we\xa0\n

then it gives us the value at\xa0\n

So, what will happen if we try to print star\xa0\n

five This concept is called as D referencing\xa0\n

an address in P and we kind of get the value\xa0\n

In fact, we can modify the value at this\xa0\n

like Star P is now eight star p means value at p\xa0\n

So, what will happen if you print star p\xa0\n

by value of p i mean value at address P.\xa0\n

at address that is stored in P. Okay, so just\xa0\n

using by putting an asterisk sign\xa0\n

And if we put an ampersand sign in front of\xa0\n

So if we try to print a pointer variable without a\xa0\n

are operating with the address, while if we put\xa0\n

and operate upon it, then we are\xa0\n

So this was some of the basics of pointers. In the\xa0\n

where we will see some of the common errors that\xa0\n

learn pointer arithmetic and pointers in the\xa0\n

So, in our previous lesson, Introduction to\xa0\n

this lesson, we will see how to work with pointers\xa0\n

a quick recap, pointers are variables that store\xa0\n

normal variable with a syntax like\xa0\n

followed by the variable name, but we declare\xa0\n

an Asterix sign followed by the variable\xa0\n

to store the address have an integer to store the\xa0\n

a character pointer so the same syntax\xa0\n

and let's say the name of the variable is P zero.\xa0\n

pointer to character. And similarly, we can have\xa0\n

to a user defined structure or a user defined\xa0\n

we write a statement like p is equal to ampersand\xa0\n

gives us the address of A. And what happens in the\xa0\n

the address of variable A. So in this figure\xa0\n

so P points to a and using p we can also get the\xa0\n

use a star operator in front of p, then the star\xa0\n

to print stoppie it gives us eight. Let us now\xa0\n

so time to see things moving in a real C program.\xa0\n

will write some print statements, and you need to\xa0\n

do is I'll declare an integer variable a, and\xa0\n

pointed to integer. And now I'll write a\xa0\n

what will be the output of this program? Okay,\xa0\n

an error. And if it is too small for you to read,\xa0\n

is not initialized. And we are using it without\xa0\n

crashing. So now I will write a statement p\xa0\n

nothing but address of A. Now let's run this\xa0\n

and how do we know that this is the address or\xa0\n

So I'll write another print statement. And now I\xa0\n

put in front of that dress then it gives\xa0\n

Okay, so what will be the output of the second\xa0\n

first line gives us the address that points to\xa0\n

address because every time the program runs afresh\xa0\n

is some garbage value is some\xa0\n

This is happening because I have not initialized\xa0\n

some garbage value there that I do not know\xa0\n

a is equal to 10. And now the\xa0\n

and now I will write another print statement. And\xa0\n

be the output of this third print statement?\xa0\n

ampersand operator gives us the address And that's\xa0\n

In fact, that's what we have done\xa0\n

the address of A, we have\xa0\nused operator and percent.\xa0\xa0

Now what I will do is, I will modify the value in\xa0\n

statement like Star p is equal to 12. And this is\xa0\n

pointed by B is now 12. This concept is called\xa0\n

and let's also put one print statement for\xa0\n

run this. So the first print gives me 10, which\xa0\n

and the second print gives us a is equal\xa0\n

using the pointer. Okay, so one more thing that\xa0\n

that will have value 20. And now what I'll say\xa0\n

And the question now is that will the\xa0\n

no, unless you explicitly write a\xa0\n

this reverse operation will not happen,\xa0\n

putting in the address of a the value that B\xa0\n

will get clear only if we write some more printf\xa0\n

I'm writing two print statements, one to\xa0\n

And after we write the statement, I'll write two\xa0\n

and value again. So, let us see what output is\xa0\n

and value is 10. And after the statement star p\xa0\n

pointer still points to a. So a kind of now\xa0\n

Now, one more thing, sometimes we declare and\xa0\n

So instead of writing this A equal 10, here,\xa0\n

So we can do so for the pointer\xa0\n

for these two statements, one simple statement\xa0\n

will not need the statement. This one statement\xa0\n

the syntax of declaring pointers some people write\xa0\n

in front of the variable name. This also works\xa0\n

star which which means pointer to integer\xa0\n

and this will also work. Okay, so now we\xa0\n

We will talk about the\xa0\n

But before that I'll write this rewrite this\xa0\n

in two statements. I like it better this way,\xa0\n

statements here. In the first statement,\xa0\n

I will print p plus one. Now is it really possible\xa0\n

a pointer variable? Well, yes, we can do so. So\xa0\n

something like 2002. So any guesses what\xa0\n

Well, no p plus one will be 2006. This is because\xa0\n

unit takes us to the address of the next integer.\xa0\n

So to go to the next integer address, we need to\xa0\n

four bytes, I'll write another print statement in\xa0\n

And I'll write something like size of integer is\xa0\n

us the size of a data type. So this statement this\xa0\n

and let's write in the first and\xa0\n

address P is blah and address p plus one is a\xa0\n

okay. So the output is address P is 4586052 which\xa0\n

is four bytes. So for this Particular compiler,\xa0\n

p plus one is four bytes more than address p, you\xa0\n

a double pointer or a pointer of some other data\xa0\n

two. And what happens if we increment this by two?\xa0\n

this will be like 2010 plus two will be 2010.\xa0\n

picked up randomly, just for the sake of example.\xa0\n

the value at this particular address, so I'm\xa0\n

that value at address P is and I'll print star\xa0\n

And we will print another line that will say that\xa0\n

be star p plus one, we put the star operator in\xa0\n

output, okay, so, the output is that address P is\xa0\n

is alright, because we had stored the address\xa0\n

of integer is four bytes, and p plus one is four\xa0\n

plus one is some integer value that I do not\xa0\n

because because we do not really have an integer\xa0\n

And this is one dangerous thing about C using\xa0\n

you can reach to any address. And sometimes\xa0\n

behavior to your program. I recommend trying\xa0\n

other data types like character or float, and the\xa0\n

some playing around with pointers. And in\xa0\n

pointers. And we will talk about pointers in\xa0\n

So far, in our previous lessons, we have\xa0\n

So we pretty much understand the basics. In\xa0\n

using pointers. And we will look through some\xa0\n

examples. The first thing that I want to point\xa0\n

What it means is that you need a pointer variable\xa0\n

particular type of variable. So int star or a\xa0\n

address of an integer character pointer will\xa0\n

And similarly, if we have a user defined\xa0\n

of that particular type only. But why do we\xa0\n

pointer variables just store the address of the\xa0\n

That will be some generic type to store the\xa0\n

is that we do not use the pointer variables only\xa0\n

to dereference these addresses so that we can\xa0\n

Now, as we know, data types have different sizes,\xa0\n

it's stored in four bytes. A character\xa0\n

is again stored in four bytes. And these variables\xa0\n

in how we store information in whatever bytes are\xa0\n

say we have an integer a and its value is 1025.\xa0\n

each bracket here is one byte. So let's say this\xa0\n

byte is byte zero, and then we go on like bite\xa0\n

each byte in the memory is addressable. Let's say\xa0\n

bytes need to be contiguous let's say the address\xa0\n

202 and 203. When an integer is represented in the\xa0\n

stores the information that whether this integer\xa0\n

signed bit, signed bit and rest 31\xa0\n

So if you see we have a one at right most\xa0\n

and at this particular bit with place value to to\xa0\n

binary here is one zero to five in decimal.\xa0\n

P and store the address of A in p\xa0\n

what will happen if I print the value of p, the\xa0\n

the address of bytes zero. So we're kind of saying\xa0\n

starting at address 200. If we dereference\xa0\n

we want to know the value at this particular\xa0\n

P is a pointer to integer, so we need to\xa0\n

And then the machine knows that how to extract\xa0\n

So it really it retrieves the value one\xa0\n

if p was a character pointer, then while\xa0\n

at only one byte because a character variable\xa0\n

then although float is also stored in four bytes,\xa0\n

in these four bytes is different from the way\xa0\n

So the value printed would have been\xa0\n

this in a real program and see what happens. In\xa0\n

equal to 1025. And now I'll declare a pointer\xa0\n

A in p by using the ampersand operator. Now I'll\xa0\n

is, and we have a function size of NC, which gives\xa0\n

And now I'll write a print statement like this\xa0\n

asterik p with dereference p to bring the value.\xa0\n

No points for guessing this is pretty\xa0\n

is four bytes, the address that we are showing\xa0\n

Okay, now I'll do some trick here, I'll declare\xa0\n

variable is P zero. Now I'll try to put the same\xa0\n

a statement like this. But this will give us a\xa0\n

character and p is a pointer to integer. So what\xa0\n

pointer and then assign the value. And now I'll\xa0\n

character is these many bytes and use the method\xa0\n

P zero and the value at addresses asterik p zero.\xa0\n

and let us see what's the output now, the first\xa0\n

address because we are running the program fresh\xa0\n

was from the previous run this will be a different\xa0\n

are getting the value 1025. Now the next line\xa0\n

addresses 5373032 which is the same address as\xa0\n

time is one. Now why is this value one once\xa0\n

then this will be the representation. When\xa0\n

the address of P in p zero, then the address\xa0\n

is stored in p zero. But when we dereference p\xa0\n

the machine says that hey, this is a pointer to\xa0\n

So I look at only one byte to see the value.\xa0\n

is one and that's why this output here is one.\xa0\n

One to print the address p plus one and the\xa0\n

we can add or subtract in an integer constant\xa0\n

the only pointer arithmetic that is allowed is\xa0\n

to the pointer. p plus one will take\xa0\n

So it will skip four bytes and take us four bytes\xa0\n

one and the value at V zero plus one. Okay now\xa0\n

the address of a this time is 4456036 that's\xa0\n

the value is 1025 p plus one is 4456040 if you\xa0\n

because size of integer is four bytes and\xa0\n

p takes us four bytes forward and this value is\xa0\n

anything in this particular address. So, there is\xa0\n

now, the address in PS row is also 4456036\xa0\n

significant byte of a the value is one now, P zero\xa0\n

one byte more and this is because character is\xa0\n

if you see p zero plus one will take us to this\xa0\n

byte and this particular byte in binary is four\xa0\n

when we dereference a pointer variable by using\xa0\n

we perform pointer arithmetic with a particular\xa0\n

from one to another also has some use cases we\xa0\n

one pointer type which is generic pointer type\xa0\n

and this pointer type is called void pointer\xa0\n

by using the keyword void and using an Asterix\xa0\n

we can write something like p zero is equal to p\xa0\n

this, the statement p zero is equal to P is valid\xa0\n

and because this particular pointer type\xa0\n

we cannot dereference this particular pointer\xa0\n

P naught or astrix p not this will give you\xa0\n

we can only print the address. And as we can\xa0\n

A and if we perform arithmetic, if we try to do\xa0\n

like p zero plus one, this is also not possible\xa0\n

we will come back to the\xa0\nuse cases of wide pointers\xa0\xa0

in forthcoming lessons. Let's for now know\xa0\n

So this was diving deep into pointer types,\xa0\n

a lot of fun with pointers in the coming lessons.\xa0\n

have seen how we can work with pointers in some of\xa0\n

where pointers can be really puzzling. And once\xa0\n

pointers. So in this lesson, we are going to see\xa0\n

that this is a logical view of computer's memory\xa0\n

we know that each byte of memory has an address,\xa0\n

let us assume that the address increases as we go\xa0\n

200 the next byte would be at address 201 and\xa0\n

Now let's say in my program, I have\xa0\n

and maybe I have initialized it as five and now\xa0\n

will be allocated for this variable x. In a\xa0\n

integer is stored in four bytes, so four byte\xa0\n

four bytes at address 225. This block of four\xa0\n

And the values stored in this block of four bytes\xa0\n

do is I want to declare a pointer variable that\xa0\n

address of an integer we will have to declare a\xa0\n

to put an asterisk sign in front of the variable\xa0\n

of memory will be reserved for this variable p in\xa0\n

in four bytes. So let's say we get this block of\xa0\n

to write a statement like this. So I want to fill\xa0\n

to x ampersand operator gives us the address\xa0\n

we are able to store the address of x in p because\xa0\n

pointer to character, or pointer to some other\xa0\n

X would not have been valid. So if I have to write\xa0\n

an integer, and p is a pointer to integer. Or I\xa0\n

And y pointer variables are strongly typed. Why\xa0\n

or store the address of an integer? It is because\xa0\n

pointer variable. We also use the pointer variable\xa0\n

value there. So if I write a statement like this,\xa0\n

now modified. Now can I create a pointer to this\xa0\n

Well, yes, we actually we can do so. So let's\xa0\n

that will store the address of P. Now what\xa0\n

a pointer of a particular type to store the\xa0\n

So to store a pointer to integer, we will\xa0\n

to pointers, so we will put two Asterix sign\xa0\n

variable q can store the address of P. So with\xa0\n

we created q let's say we created q at 205. And\xa0\n

P. So q points to P and the type of Q is int\xa0\n

simple way to read this is that the type\xa0\n

the address of x we will need a pointer of type\xa0\n

this is a pointer to that particular type.\xa0\n

a pointer to int star so we will put one extra\xa0\n

And we can go on like this. Let's say we want\xa0\n

int asterik asticus pointer to pointer and\xa0\n

these three asterik immediately after this\xa0\n

before the variable name like we have done in\xa0\n

star star star. So let's say R gets this address\xa0\n

astrick Asterix so it can store the address of\xa0\n

address of X or P all the statements something\xa0\n

Now, I have translated my previous rough code\xa0\n

this program is working as per this memory\xa0\n

now I will write some print statements and you\xa0\n

to assume that these variables x p, q and r are\xa0\n

in the right. So the first print statement that\xa0\n

and this one should be simple for you asterik\xa0\n

will be six. Now the next statement\xa0\n

So astrick q will be a value at address stored\xa0\n

this is nothing but the value of p so this will be\xa0\n

want to do dereferencing twice. First I want to go\xa0\n

want to look at the value at address 225. So this\xa0\n

paranthesis here We could have said Astrid,\xa0\n

that would have meant the same. But it's a good\xa0\n

to use paranthesis wherever we can do it because\xa0\n

we are not sure about the proceedings,\xa0\n

Now, what about these two print statements asterik\xa0\n

r means value in Q which will be 215. And then\xa0\n

value in p which is 225. And one more\xa0\n

x which will be six. If you see this is really\xa0\n

operator and we are able to go to Q then we are\xa0\n

and then we are using the astrick operator thrice\xa0\n

this code and run this in a real compiler. So what\xa0\n

And then after this statement, I'll print the\xa0\n

asterik p astrochemistry q and astrochemistry\xa0\n

the address will not be same as we had shown in\xa0\n

see, we are able to modify x by doing this\xa0\n

r if we would write something like astrochemistry\xa0\n

Now asterik P is also referencing x and\xa0\n

we are dereferencing here. So any guesses what\xa0\n

Well, if you see x is incremented by two\xa0\n

I recommend that you write some of this code\xa0\n

lessons we will use pointer to pointer in some\xa0\n

In our previous lessons, we define pointer\xa0\n

upon pointer variables how to work with pointer\xa0\n

really talk about the real use cases of pointer\xa0\n

use pointer variables. So, in this lesson, we\xa0\n

variables and the use cases using them using\xa0\n

this as call by reference. So, let's discuss\xa0\n

and he has recently learned about the concept\xa0\n

this concept and he writes a simple C\xa0\n

achieve here is that he has an integer variable\xa0\n

and he wants to increment the value in this\xa0\n

something like a plus plus or a equal to a plus\xa0\n

he writes a function increment that will take an\xa0\n

is writing a statement like a is equal to a plus\xa0\n

method passing a as argument and then he prints\xa0\n

is that the value of a will be incremented to\xa0\n

equal leaven, but when he runs the program, the\xa0\n

Albert does not understand why this is happening,\xa0\n

initialized this variable to 10 and then he is\xa0\n

and the same a is being being incremented by one\xa0\n

printed is not leaven. Why is it 10 now,\xa0\n

or what he probably forgot, is that whenever\xa0\n

then we call that variable a local variable,\xa0\n

we can access that variable only inside the same\xa0\n

So these two a this a in the function\xa0\n

are actually not the same a, the A in\xa0\n

when main calls the method increment and passes\xa0\n

value of a is copied to this another A, which is\xa0\n

So what I'll do is I'll do couple of modifications\xa0\n

I will write two print statements in\xa0\n

increment method something like this address\xa0\n

if we put ampersand operator in front of the\xa0\n

variable. And I'll comment out this print and I'll\xa0\n

And here I print that address of variable a in\xa0\n

what happens. Let me also put an end of line after\xa0\n

of variable a in increment method is printed as\xa0\n

This two looks similar, but they're not the same.\xa0\n

What the values are is not important, what\xa0\n

important is that these addresses are different.\xa0\n

was same, these two addresses would have\xa0\n

we will try to understand how things happen in\xa0\n

a program or an application is started, then the\xa0\n

reserves some amount of memory for the execution\xa0\n

for the application is divided into is typically\xa0\n

here one part of memory is allocated to store\xa0\n

computer needs to keep all these instructions in\xa0\n

program like increment or declare these variables\xa0\n

memory is one part of the allocated memory is for\xa0\n

a variable inside a function in c++ or C then\xa0\n

can be accessed and modified anywhere in the\xa0\n

accessed and modified within a particular\xa0\n

Now, the third part of the applications memory\xa0\n

this is where all the local variables go and we\xa0\n

And the fourth part the fourth part is heap. And\xa0\n

lessons have these four segments of the allocated\xa0\n

segment and the stack segment, these three are\xa0\n

starts executing the application however, can keep\xa0\n

during its execution only. We will cover all of\xa0\n

please do not get scared by these terms.\xa0\n

executes. Let us say this is our computer's\xa0\n

in the memory is addressable. So, let's say the\xa0\n

200 to 800. And these are the various segments of\xa0\n

address 300 to 600 is allocated for stack.\xa0\n

after address 800 and before address 200\xa0\n

the memory is assigned for our program, let's\xa0\n

Now, when a function is invoked, like when the\xa0\n

invoked, all the information about the method\xa0\n

like its parameters, all the local variables,\xa0\n

The current instruction at which it is executing.\xa0\n

we will take out from the stack some part for the\xa0\n

frame, each function will have a stack frame. Now\xa0\n

from this stack frame and the value of a is 10.\xa0\n

what happens when main method calls increment\xa0\n

your execution for some time, I will stop at this\xa0\n

this method increment. And then I'll come back\xa0\n

now another stack frame is allocated for the\xa0\n

method like we have a parameter a So, fresh local\xa0\n

parameters and whatever values have been passed\xa0\n

Now, when we say a is equal to a plus one\xa0\n

this a which is local to the increment\xa0\n

this is incremented we cannot access a variable it\xa0\n

when increment finishes the control returns to\xa0\n

it clears the stack frame that was located for\xa0\n

method was paused at this particular instruction\xa0\n

till the time the function is executing. Now,\xa0\n

the function print f print F is not a user\xa0\n

the state of execution of main method is\xa0\n

we often call this particular structure call stack\xa0\n

the top of the stack is executing and remember\xa0\n

scenario where one function keeps calling another\xa0\n

an infinite recursion, then the memory of this\xa0\n

okay but that is not relevant for this scenario.\xa0\n

happens when one function calls another function.\xa0\n

Main is our calling function and increment is\xa0\n

the calling function, the argument is also known\xa0\n

the argument is known as formal argument. All\xa0\n

mapped to a formal argument. So, when this\xa0\n

actual argument is mapped to another A, which is a\xa0\n

x here, so, we would have written something like\xa0\n

a would have been mapped to x. So, the value of\xa0\n

make such a function call, where we basically have\xa0\n

value in one variable copied to another variable\xa0\n

call by value. So, this is what Albert was doing,\xa0\n

able to get the desired result. But can we get\xa0\n

wanted to use this variable A, which is local to\xa0\n

Can we really do so? Well yes, we can do so,\xa0\n

us now look at this code. And I'm drawing only the\xa0\n

of program execution neatly. Now, what we are\xa0\n

which is integer in this function increment we\xa0\n

and pointer to integer as we know we will it will\xa0\n

are doing is that in the increment function, we\xa0\n

will start executing the main method will be\xa0\n

of the main method. Let's say 300 to 350. This\xa0\n

would be a local variable a. In this main method,\xa0\n

This may not be in proportion, but still\xa0\n

Now when main method calls increment, then a\xa0\n

created. And this is a pointer to integer and the\xa0\n

The value that gets stored In this particular\xa0\n

the value that gets copied or stored in this\xa0\n

So P is pointing to a. Now in this statement\xa0\n

this address, so we are seeing here that asterik\xa0\n

increment the value stored at address p by one,\xa0\n

by one, so, a is now 11. So, now when increment\xa0\n

and the next line gets executed,\xa0\n

then A is now 11. If you run this program,\xa0\n

such a function call in which instead of passing\xa0\n

the variables so that we have a reference to the\xa0\n

some operations is called call by reference.\xa0\n

then we are using call by reference call by\xa0\n

instead of creating a copy of a large and complex\xa0\n

and using a reference will also cost us\xa0\n

then, we are saved from creating a new copy of a\xa0\n

see more of the layout of applications memory,\xa0\n

thanks for watching the concept of pointers and\xa0\n

strong relationship between these two concepts.\xa0\n

relationship. When we declare an array, let's\xa0\n

then we create five integer variables named is\xa0\n

five integers will be stored in the memory as a\xa0\n

this, what we are showing here in the right,\xa0\n

is stored at address, let's say address 200. And\xa0\n

is stored in four bytes. So a one will be four\xa0\n

at 208, a three will be at 212. A four will be\xa0\n

20 bytes, and these 20 bytes will be one\xa0\n

the section of the memory in which a is stored.\xa0\n

something like this, from left to right, we\xa0\n

are just for the sake of understanding,\xa0\n

the memory. And this time, I'll show this memory\xa0\n

I can accommodate a couple of more variables.\xa0\n

an integer variable x and its value is five. And\xa0\n

say I have a pointer to integer P and P, I store\xa0\n

in p would be 300. So, this statement will print\xa0\n

address stored in Try to print the values stored\xa0\n

this is fine. But we also know that\xa0\n

increment or decrement a pointer variable\xa0\n

p equal p plus one and this will take\xa0\n

and because integer is four bytes, so now p would\xa0\n

output should be 304. But if we try to dereference\xa0\n

know the value at this address. So we cannot say\xa0\n

X lives at address 300 but we do not know who is\xa0\n

But for this integer array A, let us say I'm\xa0\n

which is located at address 200. If I declare a\xa0\n

first element by putting an ampersand operator\xa0\n

us in this scenario, the output 200 and printing\xa0\n

say we have these values in the array,\xa0\n

So Asterix p would be two. Now if I want to\xa0\n

204. And if I dereference p plus one, and try\xa0\n

And similarly if we wanted the third element\xa0\n

So using pointer arithmetic makes sense in the\xa0\n

because we know what is in the adjacent location,\xa0\n

if we just use the name of the array A, then a\xa0\n

array. So we can write a statement like p is equal\xa0\n

address in another pointer variable. If we simply\xa0\n

of the first element in the array. And if we want\xa0\n

then this will give us the value. So if we want\xa0\n

one, then this will give us the address to 04 and\xa0\n

the second element in the array value of the\xa0\n

in the array. at index i we can retrieve the\xa0\n

using either ampersand AI, or simply a plus i\xa0\n

the value of AI can be retrieved using either we\xa0\n

an Asterix a plus I will also give us the value.\xa0\n

ampersand AI or a plus i for each other and they\xa0\n

plus AI for each other and they mean the same. The\xa0\n

be called the base address. And a simply using\xa0\n

of the array. Let us now see some code examples\xa0\n

In my program, let's say we have an integer\xa0\n

a then this should give us the address\xa0\n

And we can also get the address of the first\xa0\n

in front of a zero. If I simply print a zero\xa0\n

and we can also print the first element in the\xa0\n

the variable name a. Let us now run this and see\xa0\n

after each of these print statements. Okay, so the\xa0\n

they are giving us the address of the first\xa0\n

are giving us the value. In fact, if we run\xa0\n

then we can print address of the element\xa0\n

and we can print the value of if element as a AI\xa0\n

here, this is a fresh run a new run so the address\xa0\n

address printed in two lines for a zero is same\xa0\n

four bytes I had off the previous address and the\xa0\n

Now one more thing even though just using\xa0\n

base address or the address of the first\xa0\n

a against some pointer variable like this. We\xa0\n

value of a this will give us compilation error. We\xa0\n

a to some pointer variable other pointer variable\xa0\n

So this As far as how arrays are stored in memory\xa0\n

we can access the values using pointers, in the\xa0\n

about character arrays. And we will talk about\xa0\n

for watching. In this lesson, we will be talking\xa0\n

arrays and pointers go together. And this scenario\xa0\n

let us see a code example where we pass array\xa0\n

I will write a simple C program in which I will\xa0\n

all the elements in an integer array, this\xa0\n

this is one of the ways to pass an integer array,\xa0\n

the data type and this would be the name of the\xa0\n

and initialized an array named a. Now, in the main\xa0\n

and pass it the array as argument. And of course,\xa0\n

of elements, I will declare two variables I and\xa0\n

I will be used to run a loop. So, we will\xa0\n

array that is passed as argument. But wait a\xa0\n

is there some way to know the size of the\xa0\n

of the array and by the size of the array, we\xa0\n

is to use the function size off. So if we write\xa0\n

then this will give us the size of the array\xa0\n

typical compiler, and I know for sure that in this\xa0\n

size of a will return 20. If we want to know the\xa0\n

something like divide the size of the size and\xa0\n

So we will say size of a upon size of each integer\xa0\n

number of elements in the array.\xa0\n

below this line the line where we are\xa0\n

Now, what we can do is we could either pass\xa0\n

So that would be cool. And then we can\xa0\n

minus one and we keep on adding each\xa0\n

we return sum. And finally, we will write\xa0\n

to print the sum of all the elements in the array,\xa0\n

the output on console is that the sum of elements\xa0\n

plus one is 15. So, this is all right. Now, what I\xa0\n

I do not want to pass the size as argument here,\xa0\n

this function. So, we will not be passing\xa0\n

there will be just one argument the array. So\xa0\n

calculate the size here as total size of the array\xa0\n

Let us now run this program and see what\xa0\n

because the sum of elements that we have as output\xa0\n

And why is it so, explain this behavior,\xa0\n

what is the function sum of elements where I will\xa0\n

be calling these two functions size off upon a and\xa0\n

more print statements in the main method. It will\xa0\n

bytes and the size of a zero in bytes. Let us now\xa0\n

of A is equal to four bytes. size of a zero is\xa0\n

phase equal to 20 bytes and the size of a zero\xa0\n

why the size of a is four by In the method sum of\xa0\n

method, we need to dive deep into how compiler\xa0\n

again I will draw this familiar memory diagram,\xa0\n

is typically divided into these four parts, we\xa0\n

for the execution of function calls, we use\xa0\n

here is our stack. Now, when the program starts\xa0\n

some amount of memory from the stack is allocated\xa0\n

and this particular section or this particular\xa0\n

all the local variables reside within this\xa0\n

two local variables one is the array and\xa0\n

So, around 20 bytes if integer is four bytes,\xa0\n

from this stack frame for the local variable a the\xa0\n

and there would be some more information\xa0\n

execution reaches this particular line,\xa0\n

then the execution of main is paused and another\xa0\n

sum of elements. We have talked about this\xa0\n

a number of times in our previous lessons, okay,\xa0\n

a local variable to the function sum of elements\xa0\n

when we make a function call is that the value of\xa0\n

or we should rather say the value from\xa0\n

copied to the variable in the call function. So\xa0\n

function called sum of elements. and the value\xa0\n

So we would have another array named a, taking\xa0\n

and it would have the same value same\xa0\n

main. But actually, this doesn't happen, when\xa0\n

it does not copy the whole array, what it actually\xa0\n

same name, instead of creating the whole array,\xa0\n

the array. So in this case, it will be pointed to\xa0\n

of the first element in the array of the calling\xa0\n

the address of the first element in this array,\xa0\n

in sum of elements, all that happens is a pointer\xa0\n

pointer to integer is 200. The compiler implicitly\xa0\n

that is this a here in this particular format is\xa0\n

as pointer to integer. So whether you write int\xa0\n

you write something like in Star space a, they're\xa0\n

of copying the value of the variable, we are just\xa0\n

So, we make a call by reference here, arrays\xa0\n

And this makes a lot of sense, because arrays\xa0\n

much sense to create a new copy of the array each\xa0\n

So for arrays, there is no call by value\xa0\n

that's why this size of a here would give us four\xa0\n

while here in the main method is an array. So this\xa0\n

we put these braces to tell that this is an array\xa0\n

compiler. You can put an asterisk sign here or you\xa0\n

It makes more sense to write it like this.\xa0\n

integer. That's why when we pass array as function\xa0\n

elements in the array. By using a trick like this,\xa0\n

So I'd rather move this size calculation\xa0\n

And when we run this program, now, we get\xa0\n

15. We should always keep in mind that a\xa0\n

is different from a variable which is pointed\xa0\n

some privileges, like we can use the name of the\xa0\n

like in this function sum of elements, we could\xa0\n

we should, we could have said ampersand\xa0\n

But if we just use a instead of using ampersand a\xa0\n

if a is an array, we cannot do something like\xa0\n

variables. With pointer variables,\xa0\n

And if we have a pointer to the starting\xa0\n

it like a variable name for array because II II\xa0\n

we need to keep these differences and similarities\xa0\n

thing because the array is passed by reference,\xa0\n

call function, and it would refer reflect in\xa0\n

calculating the sum total, we want to double\xa0\n

array. So our code will go something like this, I\xa0\n

not return an integer let's say the return type\xa0\n

this stuff. And what I'll do is I'll simply run\xa0\n

Okay, let us now run this and see what happens.\xa0\n

I must make a call before I print the\xa0\n

If you see the elements in the array are modified\xa0\n

because this is called by reference the same copy\xa0\n

So this was array as function arguments. An\xa0\n

is when we work with strings in C strings\xa0\n

This concept really becomes important in the\xa0\n

about it in the coming lessons. So thanks for\xa0\n

how the concepts of arrays and pointers go\xa0\n

to talk about character arrays, how we can\xa0\n

When we talk about character arrays in C,\xa0\n

Strings are group or set of characters,\xa0\n

and sentences. All of these are strings to us.\xa0\n

Character arrays become all the more important\xa0\n

perform a lot of operations on strings, like\xa0\n

two strings or finding out properties of strings\xa0\n

able to work with strings efficiently in C, there\xa0\n

The first thing that we need to understand\xa0\n

To be able to store a string in a character\xa0\n

the character array should be large enough to\xa0\n

character array. A large enough character array is\xa0\n

number of characters in the string\xa0\n

a name of four characters, like john, then\xa0\n

or equal to five. Now the obvious question would\xa0\n

Isn't it that if we layer, character array of size\xa0\n

will go at the zero at index, four will go at the\xa0\n

index respectively. So I'm storing all the data,\xa0\n

Let's say we have a character array of size eight.\xa0\n

and we want to store the string john, in this\xa0\n

seven, and this is an array of size eight.\xa0\n

the one at index H, add to it index and n add\xa0\n

the characters of the string john in this array.\xa0\n

or we have not stored the information that this\xa0\n

the string, so the string ends at index three. So\xa0\n

and that has to print the string john, or find out\xa0\n

this array, then how would the function know that\xa0\n

we have not filled anything in these indices at\xa0\n

garbage value there. So either we pass this\xa0\n

to store this information that this is the\xa0\n

This is the used part of the string. And this is\xa0\n

of the string in the next position, we put a null\xa0\n

zero. And we can put a null character at position\xa0\n

within single quotes, we put a forward slash\xa0\n

the functions for string manipulation in C, expect\xa0\n

a rule string in C has to be terminated by a null\xa0\n

to store this null character. Let us try to see\xa0\n

this concept better. What I have done here is I\xa0\n

I have filled in the characters, but I have not\xa0\n

using the printf function, this percentile, so it\xa0\n

passing this array C. Let us run this program and\xa0\n

bunch of characters that are getting printed after\xa0\n

the assumption for the printf function that my\xa0\n

So that's why this undefined behavior is coming.\xa0\n

five and put a null termination, something like\xa0\n

fine. Now even if we change the size of the array\xa0\n

a null character. So it only prints on the\xa0\n

not just the print function, there are a bunch\xa0\n

that gives us a bunch of function for string\xa0\n

that the string will be null terminated. So this\xa0\n

say we want to use one function str le n string\xa0\n

I'll print something like this, let's run this and\xa0\n

to four even though the array is of size 20. So\xa0\n

null character. In our program, instead of writing\xa0\n

we could have used string literals string literals\xa0\n

marks. So we could have written something like\xa0\n

the string and the null termination for a string\xa0\n

null termination and memory. Remember, when we\xa0\n

this, this has to happen in one line, we cannot\xa0\n

we go on and modify this in the next line this\xa0\n

this size here, and this would have been fine.\xa0\n

five bytes where one byte stores one character.\xa0\n

john. So if we try to print the size in bytes of\xa0\n

is five one character stored in one byte. So space\xa0\n

length is four, because the null character does\xa0\n

like c four is equal to this particular string\xa0\n

compilation error, because the compiler will force\xa0\n

there is one more syntax of initialization we can\xa0\n

within these braces, but in this case,\xa0\n

we will have to do it explicitly. And the size\xa0\n

to the number of characters here, so it should be\xa0\n

some of the ways to initialize a character array.\xa0\n

arrays and pointers are different types that are\xa0\n

mean? Well, let's say we declare a character\xa0\n

with this string literal. And let's say this is\xa0\n

in one contiguous block of memory. So let's say\xa0\n

is of size one bytes, so the next character will\xa0\n

and so on. Now c one is the variable name for this\xa0\n

variable, which is a pointer to character, let's\xa0\n

c two somewhere in the memory, a pointer variable\xa0\n

So this variable will also have some address,\xa0\n

I'm just picking up these numbers for example\xa0\n

statement like this C two is equal to c one.\xa0\n

a variable name for character array. And c two\xa0\n

this particular expression is valid, just using\xa0\n

of the first element in the array. So what this\xa0\n

it will fill in the address 200 and C two. So C\xa0\n

Now, we can use this variable c two, which is a\xa0\n

write into the array. So if I write something like\xa0\n

one, then the output will be L. and we can even\xa0\n

the character at zero at index two A. So the\xa0\n

when we write C two I for any position I it is\xa0\n

the base address, C two plus, I will take you\xa0\n

case, let's say C two plus two will be 202. And\xa0\n

dereferencing and finding out the value, so these\xa0\n

it is an array name, we can write on these two as\xa0\n

similarity in how we actually use them use arrays\xa0\n

the differences. We cannot modify an array with a\xa0\n

but c one equals c two is not valid, it does\xa0\n

error, we cannot even say C one is equal to c\xa0\n

This video we'll see what we can do it for C two,\xa0\n

plus so C two now points to the next element.\xa0\n

is C two will become 201. So instead of pointing\xa0\n

e to traverse an array, we run a loop and we\xa0\n

it in the loop if we have a pointer variable,\xa0\n

and we can traverse the list we can traverse the\xa0\n

So we must understand where we have an array and\xa0\n

which one. Now, the next thing that we need to\xa0\n

functions by reference or when we pass arrays\xa0\n

of the array in a pointer variable and we do not\xa0\n

not let you do do that. And we have talked\xa0\n

Arrays as function arguments. Let us look through\xa0\n

In my code, I have declared a character array of\xa0\n

in this character array. The null termination\xa0\n

Now I want to print this string, but I do\xa0\n

I want to write my own function print, I want to\xa0\n

should print the string part in a character\xa0\n

talked about this earlier can be something like\xa0\n

but the compiler actually interprets this as\xa0\n

of the character array or the address of any array\xa0\n

arrays are large in size, so it is inefficient to\xa0\n

Okay, so let's write the logic now. Now, this\xa0\n

particular array is of size 20, it only knows\xa0\n

have a variable I initialize it to zero and we can\xa0\n

null character, we can print the character ci,\xa0\xa0

and then we will increment i. And when we find\xa0\n

and print an end of line. Once again, this print\xa0\n

of size 20, it only knows the base address.\xa0\n

when we were not null terminating an array, we\xa0\n

array was finished. Because until we get a null\xa0\n

keep on going into unwanted memory locations.\xa0\n

In my code, here, I'm using percent and see\xa0\n

and see if we can also write as strict c plus II\xa0\n

same. Sometimes you'll see this kind of syntax. As\xa0\n

plus imci are valid, they are the same, there is\xa0\n

not choose to have this particular variable i,\xa0\n

equal to null, print, Asterix C and then increment\xa0\n

code will work. I leave it as an exercise for you\xa0\n

more functions for string manipulation. This is it\xa0\n

lesson on character arrays and pointers, we saw\xa0\n

we also saw how we can work with character arrays\xa0\n

Whenever we are working with pointers, it's\xa0\n

happening in the memory. So, the first thing that\xa0\n

program that we had written in the previous\xa0\n

and see what goes where in the memory. And we will\xa0\n

and constant pointers. So, this is where\xa0\n

we have written a function Print to print the\xa0\n

we have a character array of size 20, but we\xa0\n

we have used a string literal, so null termination\xa0\n

So, let us step through this code and see how it\xa0\n

diagram the memory that is allocated for execution\xa0\n

four parts these four sections on one part of the\xa0\n

we call that the code segment or the text\xa0\n

global variables and stack is where all the\xa0\n

all the local variables go whenever we are writing\xa0\n

should always visualize what variable goes where\xa0\n

that variable or data okay. So, let us run through\xa0\n

When this program will start executing first\xa0\n

whenever a function is called some amount\xa0\n

the execution. Attack function, it's called the\xa0\n

this stack frame from address 100 250 is\xa0\n

one contiguous block of memory. So let us say in\xa0\n

all the local variables of function go into the\xa0\n

character array, or 20 bytes from the stack frame\xa0\n

array. Let's say they're allocated from address\xa0\n

so we need 20 bytes for character array of size\xa0\n

some more information and stack frame. So that's\xa0\n

of the program goes to the statement print\xa0\n

function from a function, the execution of that\xa0\n

line and the machine goes on to execute the\xa0\n

stack frame on top of the calling function. So\xa0\n

main function, whatever function is at the top\xa0\n

will wait for this function to finish let's say\xa0\n

Main is paused and print is executing right now.\xa0\n

but this will be a pointer variable.\xa0\n

memory in a typical architecture. So this will be\xa0\n

a 154. In this stack frame, we have four bytes\xa0\n

in main is actually not the same C in print,\xa0\n

They have different scopes, or we could have\xa0\n

whatever. But all that happens when we make this\xa0\n

is that the address 100 which is the base address\xa0\n

the print function keeps it stores it in a\xa0\n

us when we are using the same local variable\xa0\n

argument name in the called function. But we\xa0\n

Okay to run through this code further. I'll clear\xa0\n

outside the stack here we have an array of\xa0\n

six positions in the array from address 100 205.\xa0\n

The sixth character is a null character and the\xa0\n

hello. Now we have this another guy which is\xa0\n

address 154 that stores address 100 so it points\xa0\n

come back to our program execution. Let's say we\xa0\n

is executing by this green arrow or Let's\xa0\n

is an array, local domain and this is also c\xa0\n

Okay, so now here what we're saying is, while\xa0\n

when we put this asterik operator in front of a\xa0\n

that particular address. So at this stage, when\xa0\n

so this condition is not true, we will go to\xa0\n

which is h let's write down the output here. So we\xa0\n

understand pointer arithmetic from our previous\xa0\n

increments the address by size of the data\xa0\n

pointer to character data type and character data\xa0\n

C is equal to C plus one. So C now becomes 101. So\xa0\n

And once again we come to verifying this condition\xa0\n

null. Now once again we will go inside the loop\xa0\n

builder address in this pointer variable\xa0\n

particular addresses null character so the\xa0\n

this statement to print and end offline and\xa0\n

So this particular stack frame for print will\xa0\n

And now main will resume and finished. So with\xa0\n

in the memory. Okay, so let us now modify this\xa0\n

Or what I'm going to do in my code is, instead\xa0\n

I'll create a character pointer named C and\xa0\n

like this. And if you run this program on the\xa0\n

if you use the string literal initializations\xa0\n

and then the string get stored in the space\xa0\n

it will go into the stack in this character array\xa0\n

elsewhere in a statement like this, or maybe when\xa0\n

to a function, then in these cases, the string\xa0\n

most probably it will be stored in the\xa0\n

it cannot be modified. So if you write a\xa0\n

change the first character of this constant\xa0\n

give you an error will cause a crash. Okay,\xa0\n

if we have a character array, and we are passing\xa0\n

that function receives it in a character\xa0\n

the data in this particular array. So if I say\xa0\n

character to a and then we are printing\xa0\n

it is possible to do so. Now sometimes we may want\xa0\n

anything. to force this kind of behavior, we can\xa0\n

Now if we run this code, this code will give us\xa0\n

there in the array that is passed. But we cannot\xa0\n

will work fine. pointers are really tricky,\xa0\n

write buggy code when they're using pointers,\xa0\n

writing some code yourself. That's the best way to\xa0\n

watching. In this lesson, we are going to talk\xa0\n

As we have seen in our previous lessons, the\xa0\n

have already talked about pointers in context of\xa0\n

dimensional arrays using pointers. Now let's\xa0\n

array or a three dimensional array, or a multi\xa0\n

To understand this concept, once again, we first\xa0\n

are organized in computer's memory. Let's first go\xa0\n

in memory. When we declare a one dimensional array\xa0\n

an integer array A of five elements, then\xa0\n

five different integer variables that we can name\xa0\n

contiguous block of memory. What I'm showing here\xa0\n

is stored in this section of memory, and the\xa0\n

byte in a computer's memory has an address. And if\xa0\n

which is what it takes, in a typical compiler,\xa0\n

will be a zero, as I'm showing here, block of four\xa0\n

block of code byte starting address 208 will be\xa0\n

We had seen this earlier also in our previous\xa0\n

then this is all right. If I just use the variable\xa0\n

basically returns a pointer to the first\xa0\n

array of integers. So each element will be an\xa0\n

Once I have written a statement like int Asterix\xa0\n

dereferencing to access all the elements in\xa0\n

address stored in P and I'm not using full\xa0\n

if I would simply try to dereference\xa0\n

try to print something like Asterix\xa0\n

because we have an integer pointer adding one\xa0\n

which will be four bytes ahead adding to will take\xa0\n

eight bytes ahead if we would be printing p plus\xa0\n

and we would print Asterix p plus two then it\xa0\n

pointer arithmetic in our previous lessons. Now\xa0\n

the name of the array just like a pointer\xa0\n

doing all of this printing with P if we will do\xa0\n

In fact Asterix a plus i is same as AI these are\xa0\n

AI both will give us the address of the ayat\xa0\n

though we can use the name of the array just like\xa0\n

it's not seen as a pointer variable. So\xa0\n

what we have done here. So this is all right.\xa0\n

equal p this will give you a compilation error.\xa0\n

with one dimensional arrays. Let's now say\xa0\n

I will declare a two dimensional array\xa0\n

Now, what we are doing here is we are creating\xa0\n

dimensional arrays of three elements each this\xa0\n

arrays of three integers each. a one dimensional\xa0\n

integer is four bytes in size. So if I have to\xa0\n

starting address 400 will be B zero. And the next\xa0\n

As we had said name of the array returns a pointer\xa0\n

element is not an integer, each element is a one\xa0\n

write a statement like this int Asterix p equals\xa0\n

because b will return a pointer to a 1d array of\xa0\n

the type of a pointer matters not when you have to\xa0\n

or when you perform pointer arithmetic. It's\xa0\n

define a pointer to a 1d array of three integers\xa0\n

this is all right. equate B with B this time if\xa0\n

address of B zero, this will be 400 if I\xa0\n

B zero, then B zero this time is variable name\xa0\n

So just using the name B zero will return us a\xa0\n

will be accessed as p 00. So I'm putting\xa0\n

First integer in B zero will be this block\xa0\n

Okay, now I have also created three blocks of\xa0\n

also filled in some values. Now I'm going to\xa0\n

to guess the output. What will be the output\xa0\n

the address of each block of four bytes\xa0\n

is the address of the first byte in the block.\xa0\n

B this time is returning us a pointer to\xa0\n

So, if I would do a pointer arithmetic like adding\xa0\n

of three integers. So, we will be moving to\xa0\n

of three integers in bytes. So, output will be 412\xa0\n

ampersand B one or address of B one and address\xa0\n

B plus one when we are putting an Asterix signs\xa0\n

type of pointer becomes important B is a pointer\xa0\n

B plus one is also a pointer to one dimensional\xa0\n

dereference we will get this whole one dimensional\xa0\n

Asterix B plus one is same as B one. So, we will\xa0\n

name of this one dimensional array B one which\xa0\n

integer in B one. So, astrick b plus one is\xa0\n

Once again this output will be 412 all these\xa0\n

ampersand v one, zero are returning us pointer\xa0\n

I'm going to give you some tough ones to decode\xa0\n

B plus one plus two, take some time and think\xa0\n

will return an integer pointer to first integer\xa0\n

to here is performing pointer arithmetic because\xa0\n

storing this address 412 adding to is basically\xa0\n

which will mean skipping eight bytes and and going\xa0\n

plus one in this expression can be written as b\xa0\n

for each other their alternate syntax. So this\xa0\n

these expressions are returning pointer to\xa0\n

B one two. That's why we can also say ampersand\xa0\n

Let's do one more print. If you can get this one\xa0\n

dimensional arrays using pointers what will be the\xa0\n

Asterix of Asterix B plus one what will be the\xa0\n

whenever you encounter an expression with pointer\xa0\n

by step here B is returning us a pointer to one\xa0\n

array of three integers and dereferencing it will\xa0\n

as we know will give us B zero asteroid B is\xa0\n

because B's row is named for a one dimensional\xa0\n

in the one dimensional array. So b zero returns a\xa0\n

integer at address 400. Now, what will happen if\xa0\n

take you four bytes ahead to the next integer it\xa0\n

we will get a pointer to this integer at address\xa0\n

01. And with this final dereferencing we can get\xa0\n

will be p 01 which is three for a two\xa0\n

for my two dimensional array and i and j are\xa0\n

plus j and once again p AI can be\xa0\n

So these three expressions are same. I would\xa0\n

So far this discussion has been about working\xa0\n

will stop here for this lesson. In another lesson\xa0\n

discuss how we can work with an array of further\xa0\n

We will also discuss passing of arrays\xa0\n

it for this lesson. Thanks for watching. In\xa0\n

with two dimensional arrays using pointers.\xa0\n

work with arrays of further higher dimensions\xa0\n

We will also see how we can pass multi dimensional\xa0\n

one scenario where pointers once again will come\xa0\n

what we have discussed in our previous lesson.\xa0\n

and let's pick up the example of two dimensional\xa0\n

we must think of the multi dimensional array as an\xa0\n

of similar things of similar objects. So a multi\xa0\n

arrays. This array B here is a collection of one\xa0\n

to one dimensional arrays of three elements each,\xa0\n

array B will be organized in memory, I have\xa0\n

400. Each cell storing and teacher here is a block\xa0\n

memory has an address, I'm not drawing all the\xa0\n

blocks of four bytes each. And that's why I'm\xa0\n

you can imagine a block of four bytes something\xa0\n

each partition here is one byte. So the next byte\xa0\n

And the next one has address 403. Overall, this\xa0\n

these three integers two, three, and six that I'm\xa0\n

array that I can call B zero. And this next block\xa0\n

three integers that I can call B one. So we have\xa0\n

we have two collections of three integers each\xa0\n

in one contiguous block of memory. Now let's look\xa0\n

we have three integers and we have four bytes\xa0\n

access as element at zero at index of B zero is\xa0\n

integer that can be accessed as p 011 element\xa0\n

and the next one will be v 02 element at index\xa0\n

bytes for zeroeth element of v one four bytes for\xa0\n

two of B one as we had seen in our previous\xa0\n

then it returns us a pointer to the first element\xa0\n

it is an array of one dimensional arrays of size\xa0\n

one dimensional array of three elements pointer\xa0\n

this statement, I have declared a variable which\xa0\n

integers and the name of the variable is be a\xa0\n

this will not be alright because b will not return\xa0\n

one dimensional array of three integers. Now\xa0\n

also written in our previous lesson. I'm not\xa0\n

so once again you need to tell me what will be the\xa0\n

we have p Asterix B and B zero. Well for all\xa0\n

say B just using the array name B will return us\xa0\n

the type of a pointer variable is relevant\xa0\n

perform pointer arithmetic. But if we will\xa0\n

pointer variable it will be the starting address\xa0\n

So if we have a pointer to this one dimensional\xa0\n

then it's addresses 400. Now when we did an\xa0\n

friends and now we have got the complete\xa0\n

B zero because b zero is a one dimensional array,\xa0\n

B zero, so we will get a pointer to bs 00 in\xa0\n

Once again if we would just print the address,\xa0\n

That's why the output here is 400. Even if I would\xa0\n

and Asterix B are both returning us pointers, the\xa0\n

one dimensional array of three integers while\xa0\n

when we are just printing the address both\xa0\n

array B zero and the first element in B zero have\xa0\n

address will be printed the type of pointer\xa0\n

when you try to perform pointer arithmetic.\xa0\n

B IJ can be written as Asterix of B i plus j if b\xa0\n

then b i will give us an integer pointer will\xa0\n

in bi then adding j is basically performing\xa0\n

integer at index j in one dimensional array bi\xa0\n

the value of that integer. Once again be\xa0\n

is a pointer to one dimensional array of three\xa0\n

to one dimensional array of three integers and\xa0\n

give us the one dimensional array and the name\xa0\n

the first element in the array. So this once again\xa0\n

understand how all the pointer arithmetic and\xa0\n

then it's not very difficult to understand how\xa0\n

Now let's say we have created a three dimensional\xa0\n

cross two or three dimensional array is basically\xa0\n

So if I have to show C in memory,\xa0\n

I have assumed that the starting address of C is\xa0\n

address 800 is my first two dimensional array,\xa0\n

bytes. So all these cells in yellow are\xa0\n

The next block of 16 bytes starting address\xa0\n

starting a 32 is C two, we can further break down\xa0\n

arrays. The first two integers in c zero are\xa0\n

And seven and nine are part of C 01. The first\xa0\n

can go on like this. Okay, once again, we'll play\xa0\n

and you need to guess the output. This time just\xa0\n

a two dimensional array of integers of size two\xa0\n

I have declared a pointer to a two dimensional\xa0\n

name of the point that is B if I would just print\xa0\n

printf statement once again my output will be 800.\xa0\n

print Asterix C then this will be seen as c\xa0\n

So we will get a pointer to the first element\xa0\n

us a pointer to one dimensional array of integers\xa0\n

800. Remember C is of type pointer to 2d array\xa0\n

giving us point at a one dimensional array\xa0\n

two dimensional array of integers c itself is an\xa0\n

okay for c c ij k where i j and k are some\xa0\n

K and now once again we can write C ij\xa0\n

expression will look something like this and we\xa0\n

if you are able to understand how\xa0\n

if you are able to understand all the pointer\xa0\n

then you are good working with multi\xa0\n

I want a quick answer for this one what\xa0\n

Well c 01 means we are going to this one\xa0\n

seven and nine. And when we are using the our\xa0\n

integer in this one dimensional array pointer\xa0\n

pointer arithmetic to an integer pointer So, we\xa0\n

integer nine. In fact, this expression is same as\xa0\n

print statement aspects of C one plus one c one\xa0\n

the first one dimensional array in C one,\xa0\n

adding one is performing pointer arithmetic\xa0\n

it's going to this one dimensional array\xa0\n

is basically getting the one dimensional array\xa0\n

of the one dimensional array, you get a pointer\xa0\n

So we will get a pointer to this\xa0\n

This block of four bytes storing address\xa0\n

next will be a 20 and next will be a 24 we can\xa0\n

I'm writing this simple C program I have\xa0\n

the data filled in is same as we were showing\xa0\n

I'm writing I'm trying to print c astrak, c c zero\xa0\n

if I will just use person D address would be\xa0\n

the output for all these four expressions is same.\xa0\n

printf statement, this expression is nothing but\xa0\n

because this is a different run of the program and\xa0\n

whatever the address is, it will be seen for these\xa0\n

the different expressions that we were decoding\xa0\n

next thing that I want to talk about is passing\xa0\n

I'm going to declare a function and\xa0\n

I want this function to accept a three dimensional\xa0\n

a one dimensional array as argument I could have\xa0\n

in one of our previous lessons, this syntax is\xa0\n

this by the compiler. A fresh copy of array is\xa0\n

to it in the form of a pointer is created. So\xa0\n

of one dimensional array like this, and let's\xa0\n

I can make a function called passing a like\xa0\n

we declare a two dimensional array of two cross\xa0\n

to take a two dimensional array as argument. Now\xa0\n

to in teacher but B will return us a pointer to\xa0\n

three integers for this particular definition\xa0\n

p as argument definition should be something\xa0\n

either we can write this or we can write\xa0\n

can be left empty the other dimension has to\xa0\n

something interesting here, if I would declare\xa0\n

let's declare a two dimensional array x have to\xa0\n

because x will return pointer to one\xa0\n

while this function is supposed to receive\xa0\n

if x is defined something like this of dimension\xa0\n

Now, if we want to pass this three dimensional\xa0\n

types he will return. So, C will basically return\xa0\n

two. So, we can either use a syntax like this\xa0\n

So, this is how things will be for any multi\xa0\n

all other dimensions will be enforced. One\xa0\n

dimensional array, they try to use pointer to\xa0\n

dimensional array, they try to use something like\xa0\n

much what we wanted to talk about pointers and\xa0\n

Memory is one important and crucial resource\xa0\n

the architecture of memory, the way operating\xa0\n

accessible to us as programmers. In this lesson,\xa0\n

and we will see how to work with dynamic memory\xa0\n

to a program or application in a typical\xa0\n

one segment of the memory is assigned to store\xa0\n

Another section stores all the static or global\xa0\n

inside a function and that have the whole lifetime\xa0\n

during the whole lifecycle of the application as\xa0\n

the memory is used to store all the information\xa0\n

and we have also talked about stack in our lesson\xa0\n

inside a function and the live only\xa0\n

the amount of memory set aside for these\xa0\n

variable segment and the stack does not grow\xa0\n

come back to why we use this fourth segment\xa0\n

these three segments of the memory are used. When\xa0\n

we have a function square that gives me the\xa0\n

square of sum that is given two arguments x and\xa0\n

in the main method, I'm just calling this function\xa0\n

Let us now see what happens in the memory when\xa0\n

in green here is memory reserved as stack and\xa0\n

statical global variables section when the program\xa0\n

when the main method is invoked some amount of\xa0\n

of main and this total is a global variable. So it\xa0\n

allocated on stack for execution of main can also\xa0\n

the local variables arguments and the information\xa0\n

all this information is stored within this stack\xa0\n

is calculated when the program is compiling.\xa0\n

that's right shortcut s four s four squared off\xa0\n

call to square of some all these local variables\xa0\n

Now, sum of square calls square, let's\xa0\n

So, another stack frame for Squire and\xa0\n

at any time during the execution of the program,\xa0\n

and rest are kind of paused waiting for the\xa0\n

will resume execution I have drawn this play and\xa0\n

Okay, so, this total is a global variable\xa0\n

because it is not declared and declared inside\xa0\n

we go to this particular statement where we call\xa0\n

So right now, this is our call stack. This program\xa0\n

I have written this program this way\xa0\n

calling each other let's say right now, we are\xa0\n

executing this statement. So, at this stage\xa0\n

Now, when this method finishes, we will\xa0\n

As soon as Squire function will return, it will\xa0\n

square of some function will resume. Once again\xa0\n

come back to this particular line to line total is\xa0\n

Now main we'll call printf. So once again\xa0\n

printf will finish and the control will come\xa0\n

and when main finishes program will also finish.\xa0\n

cleared, there was no need in this program. To\xa0\n

assign a variable as global only if it is needed\xa0\n

is needed for the whole lifetime of the program.\xa0\n

keep a variable for the whole lifetime of program\xa0\n

program just to understand the cost concepts.\xa0\n

when our program starts our operating system\xa0\n

always allocates one MB of space as stack,\xa0\n

and the actual allocation of the local variables\xa0\n

our call stack grows beyond the reserved memory\xa0\n

a calls b b call C and we go on calling and we\xa0\n

then this is called Stack Overflow and\xa0\n

One common case of Stack Overflow is when\xa0\n

goes infinitely into recursion. So, as we can see,\xa0\n

set aside for stack does not grow during runtime\xa0\n

So if it is one MB then if the allocation of\xa0\n

our program will crash for the allocation and de\xa0\n

set rule when a function is called it is pushed\xa0\n

finishes it is popped or removed from the stack\xa0\n

variable if it is on the stack. Another limitation\xa0\n

like an array as local variable then we need to\xa0\n

If we have a scenario like we want to decide how\xa0\n

during runtime, then it is a problem that stack\xa0\n

chunks of memory or keeping variable in the\xa0\n

like stack applications he is not fixed. Its size\xa0\n

And there is no set rule for allocation and\xa0\n

control how much memory to use from the heap\xa0\n

during the applications lifetime and he can\xa0\n

on the system itself. That is a dangerous\xa0\n

about using heap For this reason, we also\xa0\n

store of memory, we can get as much as we want\xa0\n

operating system Language Runtime or the compiler\xa0\n

computer architecture. But an abstracted way\xa0\n

this is one large free pool of memory available\xa0\n

heap is also called dynamic memory and using the\xa0\n

And let us now see how to use the heap in our C\xa0\n

and I will draw one more rectangular block\xa0\n

that I must point out before moving forward. He\xa0\n

know about this data structure heap yet, you will\xa0\n

this nomenclature here has nothing to do with\xa0\n

being used only for the large free pool of memory.\xa0\n

context. This term often confuses a lot\xa0\n

structure. Stack is also one data structure but\xa0\n

implementation of stack data structure. But heap\xa0\n

To use dynamic memory in C, we need to know about\xa0\n

use dynamic memory in c++ we need to know about\xa0\n

can also be used in c++ because c++ has backward\xa0\n

c++ programmers mostly use these two operators,\xa0\n

and try to understand how things happen. When\xa0\n

some code example in C, let us write a C program.\xa0\n

one MB first tag this was just an assumption. In\xa0\n

the operating system and the compiler, it is a\xa0\n

If we declare a variable like this, then this\xa0\n

memory for this particular variable a will\xa0\n

method. Let us say we want to store an integer on\xa0\n

on the heap, we need to call the malloc function\xa0\n

how much memory to allocate on the heap in bytes,\xa0\n

size of integer then we are saying that hey give\xa0\n

bytes is the typical size of an integer. So one\xa0\n

on the heap and malloc will return a pointer to\xa0\n

returns a void pointer. Let us say the starting\xa0\n

So malloc will return us the address 200. Now\xa0\n

variable to main. So this will be this will be\xa0\n

We have done a typecasting here because malloc\xa0\n

p is an integer pointer. Now p stores the address\xa0\n

got some block of memory on the heap, which we\xa0\n

do not know what's there in this particular block\xa0\n

we need to dereference this location using the\xa0\n

the only way to use memory on heap is through\xa0\n

look for some free space in the heap book set or\xa0\n

the only way you can access this particular block\xa0\n

to function. Now, let us write something like\xa0\n

I will go ahead and make one more call to malloc.\xa0\n

block of four bytes is allocated on the heap. And\xa0\n

the address that is returned by the second call to\xa0\n

So, what happens is that P is now pointing to\xa0\n

this address, we allocated one more block and\xa0\n

the previous block will still set in the heap.\xa0\n

be cleared off automatically. At any point in\xa0\n

block of memory which is dynamically allocated\xa0\n

because it is unnecessary consumption of memory\xa0\n

have done here is that once we were done using\xa0\n

have made a call to the function free any memory\xa0\n

by calling free and to free we pass the pointer\xa0\n

So, now with this code, this first block of\xa0\n

be pointing to another memory address. It is the\xa0\n

on the heap if he has allocated it\xa0\n

So if you see in terms of the scope of the\xa0\n

is not automatically D allocated when the function\xa0\n

it does not need to live for the whole lifetime\xa0\n

we can control when to free anything on the\xa0\n

If we wanted to store an array on the heap, like\xa0\n

then all we do is make a call to the malloc asking\xa0\n

the array in bytes. So, if we want an integer\xa0\n

to malloc asking 20 into size of int which will\xa0\n

is that one big contiguous block of memory for\xa0\n

we will get the starting address of this block.\xa0\n

the speed will point here to the base address of\xa0\n

20 integers as P zero p one, p two and so on.\xa0\n

address P and P one is same as saying value\xa0\n

One more thing, if malloc is not able to find any\xa0\n

memory on the heap, it returns null. So for\xa0\n

need to write our code appropriately malloc and\xa0\n

If we want to write the same code, same logic\xa0\n

Instead of using these two functions malloc and\xa0\n

and write our code something like this. So instead\xa0\n

here. And instead of using free we are using\xa0\n

use something like this where we put the size in\xa0\n

we use this particular operator delete\xa0\n

With c++ we do not have to do all these typecast\xa0\n

typecast it back to integer pointer, new undelete\xa0\n

they're used with a type and return pointers\xa0\n

get a pointer to integer only, we will be talking\xa0\n

functions in more detail in the coming lessons.\xa0\n

learned the concept of dynamic memory allocation,\xa0\n

applications memory. In this lesson, we will be\xa0\n

in C for dynamic memory allocation. We will look\xa0\n

We will be talking about three functions that\xa0\n

functions are malloc, calloc and realloc. And\xa0\n

D allocates a block of memory on the heap and this\xa0\n

This is the most frequently used library function\xa0\n

or the definition of this function is\xa0\n

argument asks you for the size of the memory block\xa0\n

are not aware of it is a data type that stores\xa0\n

think of this particular data type as unsigned\xa0\n

value it cannot either be zero or a positive\xa0\n

we use this particular type. And this\xa0\n

we have talked about word pointer in our previous\xa0\n

us the address of the first byte in this\xa0\n

So using malloc, you kind of say that, hey,\xa0\n

bytes. Let us say what we have here in the right\xa0\n

Each of these partitions are one byte. As we\xa0\n

let us say this bottommost byte has the address\xa0\n

increasing towards the top, I'll mark these\xa0\n

Let's say in our code, we make a\xa0\n

that, hey, give me a block of memory.\xa0\n

the address returned by malloc in a void pointer\xa0\n

rectangular block that we're showing here in the\xa0\n

malloc These are four bytes the starting address\xa0\n

want to print the address and p, then what\xa0\n

This is cool. Using malloc, we are able to\xa0\n

amount of memory. But why do we resolve memory,\xa0\n

to store some data there. So we do not want to\xa0\n

although it is correct to write a statement like\xa0\n

we would first calculate how much amount of memory\xa0\n

store an integer, just one in teacher. So I need\xa0\n

So we typically use the function size off, which\xa0\n

to figure out the size of the data type. And\xa0\n

returned by size of by the number of units\xa0\n

if we need memory for just one integer, and\xa0\n

we would write something like this, give me 10\xa0\n

type. Sometimes you may know that I know that size\xa0\n

like 10 into four here, but it's not a good\xa0\n

upon the compiler. And we will also be using\xa0\n

So we must use malloc. Like this. Total number of\xa0\n

size of one unit, one unit of the data. Let's\xa0\n

I have picked up three so that I have enough\xa0\n

I'm showing here. Let's say this block of 12 bytes\xa0\n

typically, so the starting address would\xa0\n

if you see malloc returns a void pointer and\xa0\n

you cannot write a statement something\xa0\n

This is incorrect, you cannot dereference\xa0\n

used as a generic pointer type which is normally\xa0\n

data type. And then it is used because malloc\xa0\n

some number of bytes in memory in heap. It does\xa0\n

memory to store character or to store integer or\xa0\n

a void pointer to the starting address. To be\xa0\n

typecast this void pointer into a pointer of some\xa0\n

Hanes Star p, instead of picking up a void\xa0\n

because we want to operate with integers, so to\xa0\n

we do the typecasting here of the void pointer\xa0\n

If we want to assign some value to the second\xa0\n

will do something like we will dereference the\xa0\n

four here. And if we want to access the third\xa0\n

Let's set the value of sex here. By asking for a\xa0\n

creating an array of integers with three elements,\xa0\n

And we could also write as strict p plus one as\xa0\n

the mean the same. All the manipulation on\xa0\n

pointers, you have a pointer to the starting\xa0\n

element in the array, you increment the pointer.\xa0\n

functions that allocate block of memory.\xa0\n

or definition of calloc is something like this\xa0\n

only a slightly different calloc also returns\xa0\n

instead of one argument, it takes two arguments.\xa0\n

of a particular data type. And the second\xa0\n

So with malloc, if we have to declare an array\xa0\n

we would say three into size of int. With\xa0\n

the first argument is how many units of the data\xa0\n

the second argument is the size of data type in\xa0\n

and calloc. When malloc allocate some amount of\xa0\n

any value. So if you do not fill in any value into\xa0\n

some garbage there. But if you allocate memory\xa0\n

value zero, so it also initializes the memory that\xa0\n

want to talk about is realloc. If you have a block\xa0\n

and if you want to change the size of this\xa0\n

The definition or the signature of this particular\xa0\n

first argument is pointed to the starting address\xa0\n

the size of the new block, there can be a couple\xa0\n

that we want may be larger than than the size\xa0\n

may create an entirely new block and copy the\xa0\n

previous block into the new block, if contiguous\xa0\n

with the existing block and the existing\xa0\n

some code examples and see what we can do with\xa0\n

function three in our code itself. I'll write some\xa0\n

we can use dynamic memory allocation. The first\xa0\n

And we want to first asks the user the size of\xa0\n

exactly of this particular size entered by\xa0\n

n and I write a print statement like enter size\xa0\n

console. Now I want to declare an array of size n\xa0\n

this particular value in the braces cannot be a\xa0\n

we need to know the size of the array we cannot\xa0\n

In such a scenario, we can allocate the memory\xa0\n

in Star A is equal to and we will make a\xa0\n

equal to the size of an integers. And this will\xa0\n

this particular the return of malloc to integer\xa0\n

we can fill in some data into the dynamically into\xa0\n

Let's say we want to put data something like first\xa0\n

and so on. So we will write something like a i is\xa0\n

the array. Let's say we want to pick up size of\xa0\n

we have 10 elements from one to 10. If\xa0\n

then this is what we get, we\xa0\n

If we wanted to use calloc, instead of\xa0\n

code would be that we would use calloc\xa0\n

and would separate out as first argument and\xa0\n

And this program will run around seamlessly. There\xa0\n

If we do not use this initialization, then with\xa0\n

being printed as zero, they're all initialized\xa0\n

then these elements are not initialized, there is\xa0\n

array. So this is one difference between malloc\xa0\n

zero into each byte while malloc doesn't do this\xa0\n

loop again. And now we will talk about free any\xa0\n

allocated till the lifetime of the program till\xa0\n

explicitly D allocated and to deallocate memory\xa0\n

we have the function three and to the function\xa0\n

memory block as argument. Now what will happen\xa0\n

memory is erased, it may or may not be erased, it\xa0\n

But that memory will be available for a location\xa0\n

see what happens in this case when we are printing\xa0\n

array sizes five and as you can see the elements\xa0\n

being printed. If this tree was not there, we\xa0\n

Now the obvious question would be even though\xa0\n

access that the value at that particular memory\xa0\n

the element at index i Well, that is one dangerous\xa0\n

you can look at the value at that address. But\xa0\n

if that address is allocated to you. What if that\xa0\n

Well, you do not know what you are reading\xa0\n

here it will have, it actually depends upon\xa0\n

let's say after freeing we try to access the third\xa0\n

Let's see what happens if we run this program.\xa0\n

even after free, we are able to modify the value\xa0\n

machine such a program may cause our program to\xa0\n

that is allocated. Otherwise, it's like shooting\xa0\n

We will not talk about Maria Alok. If we want\xa0\n

we have a memory block to store n elements in an\xa0\n

to double the size of the array. Or maybe we\xa0\n

For such scenario we use real lock and call\xa0\n

Let's say we take another pointer variable b\xa0\n

and size of the new block. So the new block is\xa0\n

to of course do the typecasting here okay now\xa0\n

memory block of size to n and copy the values in\xa0\n

into this new memory block. how realloc works is\xa0\n

the size of the previous block, then if it is\xa0\n

constitutive memory with the same block. Then\xa0\n

A new block is allocated and the previous block\xa0\n

and the previous block is d allocated this\xa0\n

print statement. I'll print the previous block\xa0\n

in B and I'll also print all the two elements in\xa0\n

B in one single line. Let's say size of there is\xa0\n

9920128. And if you see the new address is also\xa0\n

block only. And in B, the first five elements are\xa0\n

If we wanted to reduce the array size to let's\xa0\n

itself will be reduced. So let's say I want to\xa0\n

Now as you can see, the first two elements\xa0\n

in fact, they are there already, the rest three\xa0\n

we take only the integral part. So we kind of\xa0\n

In fact, if we give the size to be zero, here, all\xa0\n

a will be de allocated. So this statement\xa0\n

In most cases, we will put the return address\xa0\n

so we can write instead of writing B here we can\xa0\n

to realloc as null if the first argument\xa0\n

And let's say we want to create something like a\xa0\n

that is the first argument is null, then\xa0\n

This only creates a new block does not\xa0\n

So realloc can be used with the right arguments\xa0\n

for malloc. This was all about malloc,\xa0\n

code on dynamic memory allocation in the coming\xa0\n

we will talk about pointers as return types. For\xa0\n

It's just that a pointer stores the address\xa0\n

for a function to return pointer. But we must\xa0\n

return a pointer from a function. So let us try\xa0\n

some code. Now I want to write a very simple C\xa0\n

ad that will take two integers as argument\xa0\n

So let's say we declare another variable c and c\xa0\n

Now in the main method, I'll initialize two\xa0\n

let's say x is equal to two and y is equal to\xa0\n

which will be the return of this function\xa0\n

this function. And finally, I'll print something\xa0\n

the output here. I'll modify this code slightly.\xa0\n

once again, that x, y and Zed are local variables\xa0\n

function. And what really happens when we call\xa0\n

x of Maine is copied to variable a of add,\xa0\n

And what if we named these variables in\xa0\n

if we run this code output will be the same. This\xa0\n

Maine is copied to a of ad and the value in B of\xa0\n

this a in AD, they are not the same. You can\xa0\n

I'm printing the addresses of these two A's in\xa0\n

in Maine is something like 2883032 and in\xa0\n

That means these variables are not the same they\xa0\n

variables are local or specific to a particular\xa0\n

function mean can be called calling function and\xa0\n

In this particular call, when we are saying that c\xa0\n

where a and b in Maine are getting copied to\xa0\n

this can be called a call by value. Now, what\xa0\n

I want to pass the addresses of these two\xa0\n

to pass address of A and address of b to\xa0\n

function should be such that it should receive\xa0\n

it takes two pointer to integers A and B. And\xa0\n

by using this Asterix operator which is used to\xa0\n

called call by reference, a and b are integers\xa0\n

ad, A and B are not integer variables, A and\xa0\n

So the type is different. They're not in\xa0\n

they are also variables which are local to the\xa0\n

And now I'm using these two variables which are\xa0\n

a and b in the main method, and to do so we use\xa0\n

also work. I'll write a few more print statements\xa0\n

a and I have tried to print Astra K. And\xa0\n

So now ampersand a should give us the address of\xa0\n

of A in Maine because that is what this variable\xa0\n

A in Maine let us see. And the output. As you can\xa0\n

And address of A in AD is something else, but\xa0\n

a of Maine is equal to 353762. So the first\xa0\n

address we are printing the value, which is\xa0\n

Now I'll clean up some of these print statements\xa0\n

this value C and once again in the main method we\xa0\n

which is C of main why not do something like pass\xa0\n

we will do now is we will say that we want to\xa0\n

And here we will return ampersand see now\xa0\n

a variable it gives us the address. Now of course\xa0\n

address. So we will have to define a pointer\xa0\n

are printing, we will have to print the value at\xa0\n

So what we just did was, what we just did is we\xa0\n

to integer there are two syntax, we can say int\xa0\n

and then put this Asterix and in front of the\xa0\n

Now this function is returning pointer to integer.\xa0\n

Well, let me also strike off this print statement\xa0\n

Now have you got some logical error with this\xa0\n

some time. Now what I want to do is I want\xa0\n

that will print hello world. So I'll name this\xa0\n

I'll write a simple print statement. And now\xa0\n

call this function print hello world. And let's\xa0\n

Some is not correct. Now, I just saw that it\xa0\n

call this print hello world. What happened? So\xa0\n

I'll come back to this family a diagram of various\xa0\n

that is allocated to a program is typically\xa0\n

variables, and the information about function\xa0\n

run through this code. Let us simulate this code\xa0\n

For each function calls some part of the memory\xa0\n

call this the stack frame of that method or that\xa0\n

The main method is invoked. So in the stack frame,\xa0\n

and all the local variables of main function\xa0\n

the starting address of this stack frame is 100.\xa0\n

we will have three local variables created here,\xa0\n

integer pointer, let's say is at address 100, b\xa0\n

just making these assumptions. Now when the main\xa0\n

calling ad function, its execution will pause and\xa0\n

ad at any time whatever function is at the top\xa0\n

add function to complete and return. So here I\xa0\n

And now add comes here in the stack, let's say\xa0\n

has three local variables a B and C A and B are\xa0\n

and the value of B will be 112. Let's say their\xa0\n

Once again these are just random assumptions. Now\xa0\n

is pointing to this location and B is pointing to\xa0\n

in a and astrick B's value at address stored in B\xa0\n

be added. Now this add function will return the\xa0\n

finish its execution. So this PTR will be 144 and\xa0\n

will be de allocated. Now this memory above\xa0\n

And even though this variable pointer PTR\xa0\n

of this particular block it kind of points to this\xa0\n

because this memory has been de allocated. Now\xa0\n

a memory from stack will be allocated for\xa0\n

of main method. This is main. So let's say\xa0\n

from address 132 address 150. But there is no\xa0\n

But still function call execution involves storage\xa0\n

to 150 is for print hello world, I'll write\xa0\n

has been overwritten. So this block at 144 no\xa0\n

at this print statement to print the value at this\xa0\n

the obvious question would be why did the diode\xa0\n

making a call to print hello world, I would say\xa0\n

call any other function after making a call to add\xa0\n

that particular memory location. But when I made\xa0\n

If you see we have passed the addresses of these\xa0\n

but that is all right because called function\xa0\n

the stack. So anytime this called function is\xa0\n

So if ad is executing main is guaranteed to be\xa0\n

will be accessible to add. But if we try to return\xa0\n

the calling function, like if we want to return\xa0\n

function finishes and the control returns back\xa0\n

been de allocated. So it is okay to pass something\xa0\n

rather say that it's okay to pass a local variable\xa0\n

in this stack but it is not okay to return the\xa0\n

in the stack in the call stack. I hope this\xa0\n

be what are the use cases in which we may want to\xa0\n

address of some memory block in the heap section\xa0\n

then we can safely return that address Have\xa0\n

to be explicitly de allocated we control it's\xa0\n

is in the global section, a global variable\xa0\n

I can use malloc or new operator in c++ to get\xa0\n

something like this, I will declare this C as a\xa0\n

on the heap using a call to malloc. Now malloc\xa0\n

but it returns a pointer to an address which\xa0\n

using this pointer variable now, we can write this\xa0\n

memory block and then we can return this address C\xa0\n

but we are safe now because we are returning\xa0\n

not on the stack and this will work now. Now in\xa0\n

this block at address 500 in the heap see now\xa0\n

have written this data here this value six and\xa0\n

the add function which is address 500 is still\xa0\n

will not be de allocated anything on the heap\xa0\n

So while returning pointers from functions, we\xa0\n

sure that the address is not reused to store\xa0\n

that address. In most cases we will be returning\xa0\n

or memory that is in the global section the global\xa0\n

where we will be using pointers as function\xa0\n

linked list data structure. So this was pointers\xa0\n

In this lesson, we are going to talk about\xa0\n

as the name suggests, are used to store address\xa0\n

as variables that would store address of other\xa0\n

that can be used to store the address of some data\xa0\n

to point to or refer to some data. And it doesn't\xa0\n

as constant also. And we use pointers not just to\xa0\n

value at whatever address the pointer is pointing\xa0\n

that can store address or functions or in other\xa0\n

pointer to function to dereference and execute\xa0\n

Some basic questions would pop up what really\xa0\n

if we can have pointers to functions, what are\xa0\n

There are really interesting use cases of function\xa0\n

Let's first try to understand the core logic here.\xa0\n

the memory that is allocated to an application\xa0\n

these four segments. We have talked about\xa0\n

Okay, now a program is basically a set or\xa0\n

to the computer to perform a task, we can write\xa0\n

or c++, but at lowest level in its architecture,\xa0\n

any instruction that has to be executed by the\xa0\n

there will be some rules for encoding. So what we\xa0\n

in a high level language like C or c++, and we\xa0\n

and corresponding to the source code compiler\xa0\n

code, which is instructions encoded in binary.\xa0\n

compiler basically takes one or more source files.\xa0\n

in a file named program dot c. Now a compiler for\xa0\n

an executable file that will have the machine code\xa0\n

e x e, an executable file will be stored in\xa0\n

Whenever we say memory, just memory in context\xa0\n

or RAM that we also call the main memory or\xa0\n

we're talking about here will be a chunk of main\xa0\n

memory only when it starts execution. When the\xa0\n

claimed back. What really happens is that when we\xa0\n

some amount of memory is allocated to it. And that\xa0\n

The cold or text segment of applications memory\xa0\n

copied from the executable file. Instructions\xa0\n

Therefore, they are first copied to main memory,\xa0\n

execution, we need memory not just to store\xa0\n

a lot of data that we would work upon in a\xa0\n

storing and managing data. What I'm going to\xa0\n

text segment, let's assume that each\xa0\n

four bytes. I'm trying to show the section\xa0\n

each partition here is a block of four bytes\xa0\n

So, we have instruction zero at address 200.\xa0\n

And the next is at 208. And so on, instructions\xa0\n

Only Exception will be when instruction itself\xa0\n

instruction at this particular address, which will\xa0\n

if instructions 01 Nat address 200 is currently\xa0\n

be executed is instruction 02 at address 204\xa0\n

go to instruction 05 at address 216 which will\xa0\n

is nothing but a set of instructions\xa0\n

Let's say this block containing instructions five\xa0\n

if you and see one. Basically, a function is\xa0\n

in memory a function will be one contiguous\xa0\n

a function what we also call entry point of\xa0\n

instruction in the function. In this example here,\xa0\n

machine language will basically be an instruction\xa0\n

first instruction in a function. Now we will not\xa0\n

enough to understand function pointers. When we\xa0\n

we basically mean that function pointers store\xa0\n

of memory containing all the instructions in a\xa0\n

use function pointers in a C or c++ program. I am\xa0\n

thing that I'm going to do is I'm going to write\xa0\n

as argument and return the sum of these two\xa0\n

pointer that should point to this function\xa0\n

first type in the return type of the\xa0\n

at will return int so I typed in int, then\xa0\n

Asterix, and then name of the variable so I'm\xa0\n

within paranthesis. Type in all the argument types\xa0\n

argument types in function declaration should be\xa0\n

pointer will point. Because both arguments\xa0\n

in the declaration of function pointer also,\xa0\n

the address of a function we can use a statement\xa0\n

the address of something. This statement p equal\xa0\n

So P now points to AD. And using p, we can execute\xa0\n

going to declare an integer variable named C. And\xa0\n

I've done here is first I have used the Asterix\xa0\n

And then I have passed arguments just like I\xa0\n

two integers two and three. And if everything is\xa0\n

should be integer value five, when I\xa0\n

So, this is really cool, we just used a\xa0\n

and then execute the function. One thing about\xa0\n

we are putting the identifier or name of the\xa0\n

use the paranthesis then it will mean that we are\xa0\n

to integer in this case, if I would write\xa0\n

something like this, then this is declaring a\xa0\n

we can have an Asterix just before the function\xa0\n

these two syntax are same, but if I would put\xa0\n

function pointer. Okay, a few more things. In this\xa0\n

operator. Even if we do not use this ampersand\xa0\n

function name will also return us address of the\xa0\n

to data friends, instead of using this paranthesis\xa0\n

we can simply use the function pointer name\xa0\n

pointer name or identifier just like function\xa0\n

expected. So, we have two possible syntax for\xa0\n

whichever you like the second one is more famous,\xa0\n

type of the function pointer must be appropriate.\xa0\n

it something like this, then this function pointer\xa0\n

two integers as argument and should return void\xa0\n

to add this will give me compile ation error.\xa0\n

something like this, if I will have only one\xa0\n

then P cannot point to add. Okay, now let's use\xa0\n

and try to execute them through function\xa0\n

it will simply print hello on screen. And we will\xa0\n

And then we can initialize the pointer and fill\xa0\n

execute the function through the pointer. Let's\xa0\n

Now let's say we want to say hello to someone\xa0\n

to take a string as argument the declaration\xa0\n

and while calling we will have to pass a\xa0\n

what I get. And this is also looking fine. So this\xa0\n

pointers in C or c++ function pointers are used\xa0\n

interesting use cases. We will talk about the use\xa0\n

Thanks for watching. In our previous lesson,\xa0\n

wrote a simple program to understand how we can\xa0\n

we did not discuss the real use cases of function\xa0\n

can be useful. So in this lesson, we are going\xa0\n

All the use cases of function pointers are\xa0\n

be passed as arguments to functions. And then a\xa0\n

argument can call back the function that this\xa0\n

I will have to write some code. First of all,\xa0\n

a dysfunction. No argument returns void and\xa0\n

another function named p, this function takes\xa0\n

to a function that should take no argument and\xa0\n

I will simply use this function pointer PTR to\xa0\n

other words, I can say that I will call back the\xa0\n

In the main function, I will declare a function\xa0\n

point to a function that should take no argument\xa0\n

function pointer p with address of A. And now\xa0\n

function pointer P. Let's run this program and see\xa0\n

So, basically function a is getting executed,\xa0\n

In the main function instead of writing\xa0\n

a statement like this and this should be good\xa0\n

discussed this earlier also returns a pointer.\xa0\n

statements, when reference to a function is passed\xa0\n

is called a callback function. So, a is a callback\xa0\n

the reference through the function pointer.\xa0\n

through function pointer is a callback, we are\xa0\n

What's the point in calling a indirectly through\xa0\n

So let's now look at a scenario where using a\xa0\n

easier. I have a simple scenario, I have a list of\xa0\n

in increasing order of the value of integers.\xa0\n

I'm writing a function named sort that will take\xa0\n

the function will take an integer array,\xa0\n

I can write Asterix a these two are alternate\xa0\n

I have renamed this function as bubble sort. In\xa0\n

And in each pass as we go from left to right,\xa0\n

we compare adjacent elements. And if the element\xa0\n

higher index, while comparing adjacent elements,\xa0\n

is a pass on array. And this outer loop is\xa0\n

after first pass the largest element in the\xa0\n

and in the next pass, the second largest\xa0\n

this will go on and it finally in n passes, the\xa0\n

refresh bubblesort. There is a link to my\xa0\n

description of this video. In the main function\xa0\n

array A, and then I'm printing all the elements\xa0\n

increasing order. The output is as expected, we\xa0\n

want to sort my list in decreasing order of the\xa0\n

my code here? Think about it. The only change will\xa0\n

while comparing the adjacent elements. Now, I will\xa0\n

element at lower index is smaller than swap, or\xa0\n

comparing adjacent elements and swapping is\xa0\n

towards higher indices. With this condition.\xa0\n

number towards higher index. Let's see the output\xa0\n

decreasing order. Now my list is sorted in\xa0\n

Now let's say I have a requirement like this.\xa0\n

of integers in increasing order. And sometimes\xa0\n

value of integers. So what all can I do? Think\xa0\n

To start functions, the first sort function\xa0\n

function to sort in decreasing order. But if we\xa0\n

the two functions will be same, except\xa0\n

or greater than sign. Writing duplicate code has\xa0\n

better. Another thing that we can do is we can\xa0\n

say whether we want the list sorted in increasing\xa0\n

let's say when flag is one, we will start in\xa0\n

start in decreasing order. Using this approach,\xa0\n

What I'm going to do is I'm going to use\xa0\n

always done on basis of some ranking mechanism.\xa0\n

compare elements and say that they should come\xa0\n

logic in sorting will always be the same. Only the\xa0\n

What I'm going to do is I'm going to decide which\xa0\n

lesser through a function, basically, I will\xa0\n

my function will take a function pointer as\xa0\n

pointer as argument, the callback function, or\xa0\n

must take two integers as argument, it should\xa0\n

return back an integer. It should return\xa0\n

zero if the elements are equal, and minus\xa0\n

has higher rank. And let's say the element\xa0\n

in sorted array towards higher indices. These are\xa0\n

Now I'm going to use my callback function for\xa0\n

as arguments to this callback function, and\xa0\n

we will swap in this condition because AJ will\xa0\n

function to return one. So we will try to push\xa0\n

this particular implementation of bubblesort,\xa0\n

I have written a function like this. Now in main,\xa0\n

return me a function pointer and pass it in this\xa0\n

I have not written a statement to return zero if\xa0\n

logic, so far equality also I will return minus\xa0\n

output. This is what I'm getting the numbers\xa0\n

change the comparison logic a bit, I will return\xa0\n

I have changed the ranking mechanism, the\xa0\n

and will go towards the end of the array. This\xa0\n

The array is now sorted in decreasing order of\xa0\n

design because now our sort function can sort a\xa0\n

we can have one callback function for each\xa0\n

example. Let's say we have a list of integers\xa0\n

I have modified a here a now has both positive\xa0\n

array in increasing order of the absolute value\xa0\n

we will just take a mod and then compare\xa0\n

this. To be able to sort I will have to write a\xa0\n

the same compare function, but actually we\xa0\n

for each sorting scenario. So I will write a new\xa0\n

named absolute compare. I have included a\xa0\n

a BS function from this library that will give me\xa0\n

a is greater than absolute value of b, then it's\xa0\n

return minus one. Let's now use this function to\xa0\n

Okay, instead of passing this compare function,\xa0\n

to bubblesort. This basically is passing\xa0\n

Let's now run this program and see what happens.\xa0\n

in increasing order of their absolute values.\xa0\n

array of integers, but we have a library function\xa0\n

This library function is in STD\xa0\n

q SOT q SOT for quicksort and to this function\xa0\n

it can be an array of integers or it can be an\xa0\n

structured first argument will be the array second\xa0\n

third argument will be the size of the data type.\xa0\n

size of int, size of data type in bytes size\xa0\n

bytes. last argument should be a function pointer\xa0\n

of the comparison function should be like this, it\xa0\n

and return an integer. Why word pointers? What\xa0\n

them to a pointer of any data type. And this\xa0\n

it should be passed a pointer to such a\xa0\n

back. Let's write the comparison function. You\xa0\n

passed as a void pointer to get the element.\xa0\n

typecast the void pointer to int pointer. And\xa0\n

dereference and get the value, we will do the same\xa0\n

must return any positive integer, if A is ranked\xa0\n

and zero if both are ranked same, we can simply\xa0\n

A is higher in value, A minus B will be positive.\xa0\n

function here that would rank and integer with\xa0\n

function can be used to sort the array in\xa0\n

like I said, returning positive value means a\xa0\n

B is ranked higher with this comparison\xa0\n

Q sort and after the call to cusat print the\xa0\n

as you can see the list is sorted in\xa0\n

If I will change this comparison function to\xa0\n

integer with lesser value will be ranked higher,\xa0\n

value of integers. And if I will use abstract\xa0\n

this is what I will get. Remember, in this\xa0\n

compared are being passed through reference, their\xa0\n

const keyword here means you cannot modify the\xa0\n

to use void pointer because of generic design of Q\xa0\n

not just an integer array, it's just that you\xa0\n

we just discussed one of the use cases of function\xa0\n

a lot of interesting design scenarios. One more\xa0\n

life easier is something called event handling. If\xa0\n

this video for some resources on event handling.\xa0\n

In our previous lessons, we have learned\xa0\n

we have understood what is stack and what is\xa0\n

we are going to talk about one situation which is\xa0\n

memory on the heap and this situation is memory\xa0\n

we have discussed in our previous lessons. The\xa0\n

of a program or what we can also Call\xa0\n

into these four segments or these four sections.\xa0\n

that need to be executed. This section is\xa0\n

Another section is used to store the global\xa0\n

inside functions and have lifetime of the whole\xa0\n

used to execute the function calls and store all\xa0\n

The size of these three segments that code segment\xa0\n

are fixed and decided when the program is\xa0\n

fourth section, which is called heap, or dynamic\xa0\n

as per need. As we know, we get memory from the\xa0\n

And when we are done using that memory on the\xa0\n

deallocate or free that particular memory. In\xa0\n

the operator new to get some memory and the\xa0\n

leak is a situation when we get some memory\xa0\n

done using it. So our application is actually\xa0\n

But why do we call this situation memory leak? And\xa0\n

memory only due to improper use of heap only and\xa0\n

we will try to understand this through\xa0\n

I'll write one simple program and show the\xa0\n

To explain these concepts. In my C program here\xa0\n

And the game is that we have three positions\xa0\n

jack is at the first position key queen is at\xa0\n

position. And then computer shuffles these cards\xa0\n

is randomized. And now the player has to guess the\xa0\n

of money. Let's say it's virtual cash. And if he\xa0\n

the position of Queen correctly, he takes away\xa0\n

if he loses, he simply loses the bet amount. Let's\xa0\n

and he can play as many times as he wants until\xa0\n

this game. The first thing that I want to do\xa0\n

cash that at any point will store the virtual cash\xa0\n

virtual cash. And then in the main method, I will\xa0\n

code something like this. While cash is greater\xa0\n

we will ask him to bet something and we will take\xa0\n

negative scenarios like bet equals zero or bad\xa0\n

we will break out of this loop we will end our\xa0\n

make a call to play function. And of course,\xa0\n

we will be passing bet to the play function. So\xa0\n

function I will declare a character\xa0\n

we will have the character j at the first position\xa0\n

J is for jack and similarly Q is for Queen\xa0\n

perform a randomized shuffling. So we will write\xa0\n

a logic to make a random shuffle of cards. One\xa0\n

number generator function in C to use the random\xa0\n

RAND function and pass it an argument something\xa0\n

in a strand by making a call to a strand. Now\xa0\n

choose any two random positions among 01 and two\xa0\n

array. Let's say these positions are x and y. Now\xa0\n

a random number but we want a number between Zero\xa0\n

modulo by three so that we either get zero or one\xa0\n

with statements like this. And now, what we can do\xa0\n

So we will use a temporary variable First, we\xa0\n

and then we will do something like this. I'm\xa0\n

statements in the same line.\xa0\n

So we are swapping or shuffling characters or two\xa0\n

two positions x and y are randomly by making call\xa0\n

So this will guarantee us that x and y is between\xa0\n

by making a call to RAND function, we will\xa0\n

Rand once and pass this time null, the return\xa0\n

there is something there is one concept of seeding\xa0\n

seed to random number generator, we will not go\xa0\n

Now what I'll do in the proof play function is\xa0\n

I'll ask the player to guess the position of Queen\xa0\n

if the player is correct, then the character at\xa0\n

and the position will be one minus what the player\xa0\n

two or three that will map to 01 and two in the\xa0\n

So his overall cash will be incremented by three\xa0\n

cache will be decremented by the bet amount. So in\xa0\n

like this, we will say that you have one and the\xa0\n

this. Remember, cash is a global variable. And we\xa0\n

Finally, our play function is looking something\xa0\n

cash at the top. In the main method, I've\xa0\n

Let's now play this game and see what happens.\xa0\n

and created an executable named game dot txt. And\xa0\n

asking, what's your bet. Let's say we want to bet\xa0\n

$95. Let's bet again, this time again, I lose. And\xa0\n

Finally, a win after long time, I can go\xa0\n

you something else, I have opened the task\xa0\n

this highlighted row is for game dot e xe. The\xa0\n

executable game dot e xe you will have to see\xa0\n

the memory consumption here, the memory that\xa0\n

Now I'll go ahead and make some changes\xa0\n

this particular character array that I'm creating\xa0\n

now it's created as a local variable, so it will\xa0\n

array on the heap. So what I'll do is, I'll write\xa0\n

pointer named C and then I'll use malloc. To\xa0\n

In c++, we could have said something like this,\xa0\n

set a value at z zero at index as j, one at index\xa0\n

array on the heap and c is a pointer to the base\xa0\n

but now it is a pointer to character and we can\xa0\n

will just work fine. Let us run this code and see\xa0\n

I'm running the executable. Watch for the memory\xa0\n

as you You can see right now the memory\xa0\n

after some time the memory consumption is 480 8k,\xa0\n

game further, after some time it will shoot up\xa0\n

up for sure. So why is it happening, or why was it\xa0\n

not on the stack, not on the heap and it was on\xa0\n

sections of applications memory here. And let us\xa0\n

are playing our game. As we know from our previous\xa0\n

execution goes into the stack section of\xa0\n

some amount of memory from the stack is allocated\xa0\n

executing. First, the main method is invoked or\xa0\n

allocated for the execution of Maine. This is what\xa0\n

stack frame of Maine and all the local variables\xa0\n

code we had a local variable named bet, so it will\xa0\n

So initially, let's say main function is executing\xa0\n

calls to play function. And what really happens\xa0\n

is that that particular function is paused\xa0\n

the called function. So main will pause and play\xa0\n

main in the stack. Now, we had a couple of local\xa0\n

i x y players guess and for the case\xa0\n

on the stack itself, so it was not created using\xa0\n

sit in this stack frame. Now when the execution\xa0\n

back to main and the memory that was allocated for\xa0\n

Anytime a function call finishes, the memory\xa0\n

So there is one stack frame corresponding to\xa0\n

that memory is claimed back. Now main will make\xa0\n

rounds. So play will again come into the stack\xa0\n

As you can see, all the local variables get\xa0\n

For anything on the stack, we do not have to worry\xa0\n

when the function call finishes. Now let's talk\xa0\n

created on the heap using a call to malloc\xa0\n

to play function. Now, what will happen this time\xa0\n

we will still have a variable named c a local\xa0\n

be of type character array of size three, this\xa0\n

we will make a call to malloc function to create\xa0\n

which is a pointer to character will only point\xa0\n

on the heap has to be accessed through a pointer\xa0\n

the heap and we have kept only a pointer variable\xa0\n

will finish the memory allocated for the execution\xa0\n

the local variables will go away. But this memory\xa0\n

it will not get d allocated anything on the heap\xa0\n

to free function or by using delete operator and\xa0\n

function as we play multiple rounds of our game.\xa0\n

one such memory block on the heap that will lie\xa0\n

will finish. If we will play our game 100 rounds\xa0\n

unused memory blocks of three characters and\xa0\n

application can claim can get more memory in\xa0\n

is not running out of memory. And if we are not\xa0\n

depleting and wasting memory which is an important\xa0\n

only keep on growing with time. memory leaks\xa0\n

Anything unused and unreferenced on the heap\xa0\n

as programmers that garbage is not created on the\xa0\n

in the heap. in languages like Java and C sharp\xa0\n

so a programmer does not have to worry about\xa0\n

which is a cool feature to have. It avoids memory\xa0\n

of three characters on the heap. What if we were\xa0\n

freeing freeing the memory after we were done\xa0\n

consumption would have shot up like anything.\xa0\n

I have created a character array of size 10,000 of\xa0\n

I'll just use first three positions in the array.\xa0\n

end of this particular function, when we are done\xa0\n

to the free function, passing it the address of\xa0\n

will just work like before. But let's run this\xa0\n

Once again, I'm showing you the task manager\xa0\n

Now watch out the memory consumption of game dot\xa0\n

the memory consumption will shoot up. As you can\xa0\n

even after playing for a long time. And\xa0\n

free to D allocate the memory when we were done\xa0\n

we had created an array of size 10,000. And if\xa0\n

shot up like anything the memory consumption\xa0\n

we are freeing at the end of the function, it's\xa0\n

to summarize it, memory leak is improper use\xa0\n

that causes the memory consumption of our program\xa0\n

memory leak always happens because of unused and\xa0\n

on the stack is de allocated automatically\xa0\n

most we can have an overflow in stack. So this\xa0\n

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