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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
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
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,080 --> 00:00:05,839 The course you're about to watch is about pointers\xa0\n 2 00:00:05,839 --> 00:00:11,839 instructors from my code school. My Code School is\xa0\n 3 00:00:11,839 --> 00:00:17,519 and has inspired many developers and creators.\xa0\n 4 00:00:17,519 --> 00:00:23,839 article about the channels amazing story.\xa0\n 5 00:00:23,839 --> 00:00:30,320 is a very fundamental and important concept in\xa0\n 6 00:00:30,320 --> 00:00:35,679 find it difficult to understand pointers. So in\xa0\n 7 00:00:36,799 --> 00:00:42,000 And all you need to know to understand this\xa0\n 8 00:00:42,000 --> 00:00:47,280 how to declare a variable and how to do\xa0\n 9 00:00:47,920 --> 00:00:54,079 Okay, so let's get started. To understand\xa0\n 10 00:00:54,079 --> 00:01:01,519 various data types or various variables are stored\xa0\n 11 00:01:01,520 --> 00:01:08,000 figure in the right here is computer's memory. And\xa0\n 12 00:01:08,719 --> 00:01:13,120 program execution, we mostly talk about\xa0\n 13 00:01:13,760 --> 00:01:19,600 we often say that my machine has got two GB of\xa0\n 14 00:01:19,599 --> 00:01:25,679 Now let's say these segments, or partitions that\xa0\n 15 00:01:25,680 --> 00:01:32,320 of memory. Now in a typical memory architecture\xa0\n 16 00:01:33,200 --> 00:01:42,799 each byte of the memory has an address. So let's\xa0\n 17 00:01:42,799 --> 00:01:49,120 assume to be the lowest by deep down here has an\xa0\n 18 00:01:49,120 --> 00:01:57,200 as we go towards the top. So we go on like 012.\xa0\n 19 00:01:58,799 --> 00:02:08,560 201. So the next byte will have an address of 202.\xa0\n 20 00:02:08,560 --> 00:02:14,319 when we declare a variable in our program, let's\xa0\n 21 00:02:14,319 --> 00:02:20,959 type integer, then when this program executes,\xa0\n 22 00:02:20,960 --> 00:02:26,640 corresponding to this particular variable.\xa0\n 23 00:02:26,639 --> 00:02:33,519 the data type, and also upon the compiler. So\xa0\n 24 00:02:33,520 --> 00:02:39,840 is allocated four bytes of memory. Character\xa0\n 25 00:02:40,719 --> 00:02:46,240 float is allocated four bytes of memory and\xa0\n 26 00:02:46,240 --> 00:02:51,840 so as soon as the computer sees a declaration,\xa0\n 27 00:02:52,400 --> 00:02:57,599 it knows that this is an integer variable, so\xa0\n 28 00:02:57,599 --> 00:03:07,039 say in our example, it allocates memory starting\xa0\n 29 00:03:07,039 --> 00:03:12,719 an internal structure, a lookup table, where it\xa0\n 30 00:03:12,719 --> 00:03:19,520 a, it is of type integer, and it is located at\xa0\n 31 00:03:19,520 --> 00:03:26,080 the variable. Now, if we declare another variable,\xa0\n 32 00:03:26,080 --> 00:03:30,800 which is of type character, now once again,\xa0\n 33 00:03:31,360 --> 00:03:36,480 it knows that it is a character variable, so it\xa0\n 34 00:03:36,479 --> 00:03:46,000 free space, let's say in this case, it allocates\xa0\n 35 00:03:46,000 --> 00:03:52,639 it keeps an entry for it in an internal structure\xa0\n 36 00:03:53,199 --> 00:03:59,039 and its addresses 209. Now when we perform some\xa0\n 37 00:03:59,039 --> 00:04:05,280 if we initialize a to five, when our machine or\xa0\n 38 00:04:05,280 --> 00:04:11,520 the lookup table for this variable A. So it finds\xa0\n 39 00:04:11,520 --> 00:04:20,480 at address 204. So, it goes at address 204 and in\xa0\n 40 00:04:21,839 --> 00:04:27,759 five now in reality, the value is written in\xa0\n 41 00:04:27,759 --> 00:04:33,199 are writing here in decimal form. Now once again,\xa0\n 42 00:04:33,199 --> 00:04:39,519 after these statements, we have another statement\xa0\n 43 00:04:39,519 --> 00:04:46,159 that, he has to be incremented, it again looks\xa0\n 44 00:04:46,160 --> 00:04:52,720 modifies this value at this particular address.\xa0\n 45 00:04:52,720 --> 00:04:59,520 the value six now. Now, all of this is really\xa0\n 46 00:05:00,240 --> 00:05:04,319 In our program, or can we operate upon\xa0\n 47 00:05:04,879 --> 00:05:12,159 Well, yes, you can do so in a C or c++ program\xa0\n 48 00:05:12,160 --> 00:05:17,280 variables that store the address of another\xa0\n 49 00:05:17,279 --> 00:05:23,519 a little better, I'll redraw a couple of these\xa0\n 50 00:05:23,519 --> 00:05:30,639 at address two sort of food that stores an integer\xa0\n 51 00:05:31,199 --> 00:05:38,079 the type of which is pointer to integer. And\xa0\n 52 00:05:38,079 --> 00:05:46,719 Now this variable p can store the address of\xa0\n 53 00:05:46,720 --> 00:05:53,760 operators upon p, we can reach a. Now p also\xa0\n 54 00:05:54,639 --> 00:06:02,800 at location address 64. And it also takes four\xa0\n 55 00:06:03,519 --> 00:06:08,399 another integer. So let's say if we\xa0\n 56 00:06:09,279 --> 00:06:18,399 named B and having value 10. And if we change the\xa0\n 57 00:06:18,399 --> 00:06:26,079 me. Let us now see the syntax of pointer variables\xa0\n 58 00:06:26,079 --> 00:06:33,759 the data type and a variable name. So int a means\xa0\n 59 00:06:33,759 --> 00:06:40,719 to write a pointer variable that should point to\xa0\n 60 00:06:40,720 --> 00:06:51,120 sign before the variable. So P is now a pointer\xa0\n 61 00:06:51,120 --> 00:06:59,519 variable that can store the address of an integer.\xa0\n 62 00:06:59,519 --> 00:07:06,959 use a statement like p is equal to ampersand a.\xa0\n 63 00:07:06,959 --> 00:07:11,359 it gives us the address of that particular\xa0\n 64 00:07:12,079 --> 00:07:16,479 to that particular variable. Okay, so let's try\xa0\n 65 00:07:16,480 --> 00:07:24,560 declared the variable A, then it was allocated an\xa0\n 66 00:07:25,120 --> 00:07:29,759 integer p, let's say it was allocated\xa0\n 67 00:07:29,759 --> 00:07:38,800 like p is equal to ampersand A, then P now has\xa0\n 68 00:07:40,800 --> 00:07:47,439 Let's initialize a to some value let's say is\xa0\n 69 00:07:48,879 --> 00:07:56,399 Now, what will happen if I print p, what will\xa0\n 70 00:07:57,839 --> 00:08:06,560 Okay, so if we try to print p, then the output\xa0\n 71 00:08:06,560 --> 00:08:12,639 we simply try to print ampersand of a ampersand\xa0\n 72 00:08:12,639 --> 00:08:21,839 also be 204. What if we want to print ampersand\xa0\n 73 00:08:21,839 --> 00:08:27,519 stored in the memory. So ampersand since it prints\xa0\n 74 00:08:27,519 --> 00:08:34,000 So, printing ampersand of P should give us\xa0\n 75 00:08:34,000 --> 00:08:39,840 one more important property of pointer, if we\xa0\n 76 00:08:40,720 --> 00:08:44,240 then it gives us the value at\xa0\n 77 00:08:44,240 --> 00:08:50,560 So, what will happen if we try to print star\xa0\n 78 00:08:51,360 --> 00:08:59,120 five This concept is called as D referencing\xa0\n 79 00:08:59,759 --> 00:09:05,679 an address in P and we kind of get the value\xa0\n 80 00:09:06,480 --> 00:09:12,399 In fact, we can modify the value at this\xa0\n 81 00:09:12,399 --> 00:09:20,879 like Star P is now eight star p means value at p\xa0\n 82 00:09:21,519 --> 00:09:28,399 So, what will happen if you print star p\xa0\n 83 00:09:30,480 --> 00:09:37,360 by value of p i mean value at address P.\xa0\n 84 00:09:38,159 --> 00:09:44,639 at address that is stored in P. Okay, so just\xa0\n 85 00:09:45,440 --> 00:09:49,600 using by putting an asterisk sign\xa0\n 86 00:09:49,600 --> 00:09:54,480 And if we put an ampersand sign in front of\xa0\n 87 00:09:54,480 --> 00:10:00,080 So if we try to print a pointer variable without a\xa0\n 88 00:10:00,080 --> 00:10:04,800 are operating with the address, while if we put\xa0\n 89 00:10:05,519 --> 00:10:09,600 and operate upon it, then we are\xa0\n 90 00:10:10,320 --> 00:10:16,480 So this was some of the basics of pointers. In the\xa0\n 91 00:10:16,480 --> 00:10:22,080 where we will see some of the common errors that\xa0\n 92 00:10:22,080 --> 00:10:28,320 learn pointer arithmetic and pointers in the\xa0\n 93 00:10:30,000 --> 00:10:36,080 So, in our previous lesson, Introduction to\xa0\n 94 00:10:36,080 --> 00:10:42,400 this lesson, we will see how to work with pointers\xa0\n 95 00:10:42,399 --> 00:10:50,319 a quick recap, pointers are variables that store\xa0\n 96 00:10:50,320 --> 00:10:56,879 normal variable with a syntax like\xa0\n 97 00:10:56,879 --> 00:11:04,480 followed by the variable name, but we declare\xa0\n 98 00:11:05,039 --> 00:11:11,279 an Asterix sign followed by the variable\xa0\n 99 00:11:11,279 --> 00:11:17,439 to store the address have an integer to store the\xa0\n 100 00:11:18,559 --> 00:11:22,559 a character pointer so the same syntax\xa0\n 101 00:11:23,440 --> 00:11:29,440 and let's say the name of the variable is P zero.\xa0\n 102 00:11:30,080 --> 00:11:37,920 pointer to character. And similarly, we can have\xa0\n 103 00:11:38,480 --> 00:11:46,320 to a user defined structure or a user defined\xa0\n 104 00:11:46,320 --> 00:11:54,080 we write a statement like p is equal to ampersand\xa0\n 105 00:11:54,080 --> 00:12:01,040 gives us the address of A. And what happens in the\xa0\n 106 00:12:01,039 --> 00:12:08,959 the address of variable A. So in this figure\xa0\n 107 00:12:08,960 --> 00:12:18,480 so P points to a and using p we can also get the\xa0\n 108 00:12:19,120 --> 00:12:27,200 use a star operator in front of p, then the star\xa0\n 109 00:12:27,200 --> 00:12:34,640 to print stoppie it gives us eight. Let us now\xa0\n 110 00:12:34,639 --> 00:12:41,360 so time to see things moving in a real C program.\xa0\n 111 00:12:41,360 --> 00:12:47,919 will write some print statements, and you need to\xa0\n 112 00:12:47,919 --> 00:12:53,360 do is I'll declare an integer variable a, and\xa0\n 113 00:12:53,919 --> 00:13:01,759 pointed to integer. And now I'll write a\xa0\n 114 00:13:01,759 --> 00:13:07,840 what will be the output of this program? Okay,\xa0\n 115 00:13:07,840 --> 00:13:15,040 an error. And if it is too small for you to read,\xa0\n 116 00:13:15,679 --> 00:13:22,079 is not initialized. And we are using it without\xa0\n 117 00:13:22,080 --> 00:13:30,960 crashing. So now I will write a statement p\xa0\n 118 00:13:30,960 --> 00:13:39,519 nothing but address of A. Now let's run this\xa0\n 119 00:13:40,320 --> 00:13:45,040 and how do we know that this is the address or\xa0\n 120 00:13:45,840 --> 00:13:52,960 So I'll write another print statement. And now I\xa0\n 121 00:13:52,960 --> 00:13:56,720 put in front of that dress then it gives\xa0\n 122 00:13:57,360 --> 00:14:04,320 Okay, so what will be the output of the second\xa0\n 123 00:14:04,320 --> 00:14:09,360 first line gives us the address that points to\xa0\n 124 00:14:09,360 --> 00:14:16,399 address because every time the program runs afresh\xa0\n 125 00:14:17,519 --> 00:14:21,279 is some garbage value is some\xa0\n 126 00:14:22,399 --> 00:14:30,240 This is happening because I have not initialized\xa0\n 127 00:14:30,240 --> 00:14:34,960 some garbage value there that I do not know\xa0\n 128 00:14:34,960 --> 00:14:41,120 a is equal to 10. And now the\xa0\n 129 00:14:43,120 --> 00:14:51,679 and now I will write another print statement. And\xa0\n 130 00:14:51,679 --> 00:14:56,879 be the output of this third print statement?\xa0\n 131 00:14:57,759 --> 00:15:03,360 ampersand operator gives us the address And that's\xa0\n 132 00:15:05,279 --> 00:15:08,159 In fact, that's what we have done\xa0\n 133 00:15:09,360 --> 00:15:11,840 the address of A, we have\xa0\nused operator and percent.\xa0\xa0 134 00:15:13,279 --> 00:15:20,319 Now what I will do is, I will modify the value in\xa0\n 135 00:15:20,320 --> 00:15:27,600 statement like Star p is equal to 12. And this is\xa0\n 136 00:15:27,600 --> 00:15:34,639 pointed by B is now 12. This concept is called\xa0\n 137 00:15:36,000 --> 00:15:42,399 and let's also put one print statement for\xa0\n 138 00:15:42,399 --> 00:15:48,879 run this. So the first print gives me 10, which\xa0\n 139 00:15:49,440 --> 00:15:55,120 and the second print gives us a is equal\xa0\n 140 00:15:55,759 --> 00:16:00,960 using the pointer. Okay, so one more thing that\xa0\n 141 00:16:01,600 --> 00:16:08,480 that will have value 20. And now what I'll say\xa0\n 142 00:16:09,519 --> 00:16:16,159 And the question now is that will the\xa0\n 143 00:16:16,159 --> 00:16:22,000 no, unless you explicitly write a\xa0\n 144 00:16:22,000 --> 00:16:25,440 this reverse operation will not happen,\xa0\n 145 00:16:26,159 --> 00:16:32,159 putting in the address of a the value that B\xa0\n 146 00:16:32,159 --> 00:16:38,000 will get clear only if we write some more printf\xa0\n 147 00:16:38,000 --> 00:16:42,159 I'm writing two print statements, one to\xa0\n 148 00:16:43,039 --> 00:16:48,159 And after we write the statement, I'll write two\xa0\n 149 00:16:48,159 --> 00:16:55,759 and value again. So, let us see what output is\xa0\n 150 00:16:57,440 --> 00:17:03,120 and value is 10. And after the statement star p\xa0\n 151 00:17:03,120 --> 00:17:07,920 pointer still points to a. So a kind of now\xa0\n 152 00:17:08,720 --> 00:17:14,319 Now, one more thing, sometimes we declare and\xa0\n 153 00:17:14,319 --> 00:17:20,559 So instead of writing this A equal 10, here,\xa0\n 154 00:17:21,440 --> 00:17:25,360 So we can do so for the pointer\xa0\n 155 00:17:25,359 --> 00:17:33,039 for these two statements, one simple statement\xa0\n 156 00:17:33,039 --> 00:17:37,839 will not need the statement. This one statement\xa0\n 157 00:17:38,960 --> 00:17:46,240 the syntax of declaring pointers some people write\xa0\n 158 00:17:46,240 --> 00:17:51,200 in front of the variable name. This also works\xa0\n 159 00:17:51,200 --> 00:17:55,039 star which which means pointer to integer\xa0\n 160 00:17:55,920 --> 00:18:01,840 and this will also work. Okay, so now we\xa0\n 161 00:18:03,200 --> 00:18:06,240 We will talk about the\xa0\n 162 00:18:09,279 --> 00:18:15,359 But before that I'll write this rewrite this\xa0\n 163 00:18:15,359 --> 00:18:21,519 in two statements. I like it better this way,\xa0\n 164 00:18:21,519 --> 00:18:27,359 statements here. In the first statement,\xa0\n 165 00:18:27,359 --> 00:18:35,119 I will print p plus one. Now is it really possible\xa0\n 166 00:18:35,119 --> 00:18:43,519 a pointer variable? Well, yes, we can do so. So\xa0\n 167 00:18:43,519 --> 00:18:50,639 something like 2002. So any guesses what\xa0\n 168 00:18:52,319 --> 00:19:01,119 Well, no p plus one will be 2006. This is because\xa0\n 169 00:19:01,119 --> 00:19:08,159 unit takes us to the address of the next integer.\xa0\n 170 00:19:08,720 --> 00:19:16,400 So to go to the next integer address, we need to\xa0\n 171 00:19:16,400 --> 00:19:22,000 four bytes, I'll write another print statement in\xa0\n 172 00:19:22,559 --> 00:19:30,480 And I'll write something like size of integer is\xa0\n 173 00:19:30,480 --> 00:19:36,400 us the size of a data type. So this statement this\xa0\n 174 00:19:36,960 --> 00:19:41,279 and let's write in the first and\xa0\n 175 00:19:42,319 --> 00:19:50,240 address P is blah and address p plus one is a\xa0\n 176 00:19:50,240 --> 00:19:58,000 okay. So the output is address P is 4586052 which\xa0\n 177 00:19:58,000 --> 00:20:04,480 is four bytes. So for this Particular compiler,\xa0\n 178 00:20:04,480 --> 00:20:10,960 p plus one is four bytes more than address p, you\xa0\n 179 00:20:10,960 --> 00:20:16,319 a double pointer or a pointer of some other data\xa0\n 180 00:20:16,319 --> 00:20:24,240 two. And what happens if we increment this by two?\xa0\n 181 00:20:24,240 --> 00:20:33,200 this will be like 2010 plus two will be 2010.\xa0\n 182 00:20:33,200 --> 00:20:39,200 picked up randomly, just for the sake of example.\xa0\n 183 00:20:39,200 --> 00:20:43,759 the value at this particular address, so I'm\xa0\n 184 00:20:43,759 --> 00:20:48,960 that value at address P is and I'll print star\xa0\n 185 00:20:49,920 --> 00:20:55,519 And we will print another line that will say that\xa0\n 186 00:20:55,519 --> 00:21:02,240 be star p plus one, we put the star operator in\xa0\n 187 00:21:02,240 --> 00:21:07,759 output, okay, so, the output is that address P is\xa0\n 188 00:21:07,759 --> 00:21:13,680 is alright, because we had stored the address\xa0\n 189 00:21:13,680 --> 00:21:19,360 of integer is four bytes, and p plus one is four\xa0\n 190 00:21:19,359 --> 00:21:24,479 plus one is some integer value that I do not\xa0\n 191 00:21:24,480 --> 00:21:30,079 because because we do not really have an integer\xa0\n 192 00:21:30,079 --> 00:21:35,599 And this is one dangerous thing about C using\xa0\n 193 00:21:35,599 --> 00:21:41,679 you can reach to any address. And sometimes\xa0\n 194 00:21:41,680 --> 00:21:46,560 behavior to your program. I recommend trying\xa0\n 195 00:21:46,559 --> 00:21:52,639 other data types like character or float, and the\xa0\n 196 00:21:53,519 --> 00:21:58,639 some playing around with pointers. And in\xa0\n 197 00:21:58,640 --> 00:22:04,560 pointers. And we will talk about pointers in\xa0\n 198 00:22:06,960 --> 00:22:11,840 So far, in our previous lessons, we have\xa0\n 199 00:22:12,400 --> 00:22:17,200 So we pretty much understand the basics. In\xa0\n 200 00:22:17,200 --> 00:22:23,200 using pointers. And we will look through some\xa0\n 201 00:22:23,200 --> 00:22:28,559 examples. The first thing that I want to point\xa0\n 202 00:22:29,279 --> 00:22:35,359 What it means is that you need a pointer variable\xa0\n 203 00:22:35,359 --> 00:22:40,879 particular type of variable. So int star or a\xa0\n 204 00:22:40,880 --> 00:22:45,600 address of an integer character pointer will\xa0\n 205 00:22:45,599 --> 00:22:51,039 And similarly, if we have a user defined\xa0\n 206 00:22:51,039 --> 00:22:56,720 of that particular type only. But why do we\xa0\n 207 00:22:56,720 --> 00:23:02,240 pointer variables just store the address of the\xa0\n 208 00:23:02,240 --> 00:23:08,559 That will be some generic type to store the\xa0\n 209 00:23:08,559 --> 00:23:14,000 is that we do not use the pointer variables only\xa0\n 210 00:23:14,559 --> 00:23:21,839 to dereference these addresses so that we can\xa0\n 211 00:23:22,559 --> 00:23:28,799 Now, as we know, data types have different sizes,\xa0\n 212 00:23:29,359 --> 00:23:36,399 it's stored in four bytes. A character\xa0\n 213 00:23:37,119 --> 00:23:43,359 is again stored in four bytes. And these variables\xa0\n 214 00:23:43,359 --> 00:23:51,199 in how we store information in whatever bytes are\xa0\n 215 00:23:51,200 --> 00:23:59,279 say we have an integer a and its value is 1025.\xa0\n 216 00:23:59,279 --> 00:24:04,879 each bracket here is one byte. So let's say this\xa0\n 217 00:24:04,880 --> 00:24:13,680 byte is byte zero, and then we go on like bite\xa0\n 218 00:24:13,680 --> 00:24:21,039 each byte in the memory is addressable. Let's say\xa0\n 219 00:24:21,039 --> 00:24:27,039 bytes need to be contiguous let's say the address\xa0\n 220 00:24:27,039 --> 00:24:36,000 202 and 203. When an integer is represented in the\xa0\n 221 00:24:36,960 --> 00:24:41,759 stores the information that whether this integer\xa0\n 222 00:24:42,559 --> 00:24:48,240 signed bit, signed bit and rest 31\xa0\n 223 00:24:49,119 --> 00:24:55,279 So if you see we have a one at right most\xa0\n 224 00:24:56,000 --> 00:25:03,119 and at this particular bit with place value to to\xa0\n 225 00:25:03,119 --> 00:25:10,879 binary here is one zero to five in decimal.\xa0\n 226 00:25:11,599 --> 00:25:16,319 P and store the address of A in p\xa0\n 227 00:25:17,200 --> 00:25:23,920 what will happen if I print the value of p, the\xa0\n 228 00:25:23,920 --> 00:25:30,240 the address of bytes zero. So we're kind of saying\xa0\n 229 00:25:30,240 --> 00:25:35,839 starting at address 200. If we dereference\xa0\n 230 00:25:36,640 --> 00:25:40,720 we want to know the value at this particular\xa0\n 231 00:25:40,720 --> 00:25:48,400 P is a pointer to integer, so we need to\xa0\n 232 00:25:48,400 --> 00:25:53,440 And then the machine knows that how to extract\xa0\n 233 00:25:54,559 --> 00:26:01,279 So it really it retrieves the value one\xa0\n 234 00:26:01,279 --> 00:26:05,759 if p was a character pointer, then while\xa0\n 235 00:26:05,759 --> 00:26:12,640 at only one byte because a character variable\xa0\n 236 00:26:13,359 --> 00:26:19,119 then although float is also stored in four bytes,\xa0\n 237 00:26:19,119 --> 00:26:24,879 in these four bytes is different from the way\xa0\n 238 00:26:24,880 --> 00:26:28,400 So the value printed would have been\xa0\n 239 00:26:28,400 --> 00:26:36,240 this in a real program and see what happens. In\xa0\n 240 00:26:36,799 --> 00:26:44,720 equal to 1025. And now I'll declare a pointer\xa0\n 241 00:26:44,720 --> 00:26:52,559 A in p by using the ampersand operator. Now I'll\xa0\n 242 00:26:52,559 --> 00:26:59,119 is, and we have a function size of NC, which gives\xa0\n 243 00:26:59,920 --> 00:27:06,640 And now I'll write a print statement like this\xa0\n 244 00:27:07,440 --> 00:27:13,840 asterik p with dereference p to bring the value.\xa0\n 245 00:27:14,400 --> 00:27:18,240 No points for guessing this is pretty\xa0\n 246 00:27:18,240 --> 00:27:23,039 is four bytes, the address that we are showing\xa0\n 247 00:27:25,440 --> 00:27:30,720 Okay, now I'll do some trick here, I'll declare\xa0\n 248 00:27:30,720 --> 00:27:39,039 variable is P zero. Now I'll try to put the same\xa0\n 249 00:27:39,039 --> 00:27:44,639 a statement like this. But this will give us a\xa0\n 250 00:27:44,640 --> 00:27:52,480 character and p is a pointer to integer. So what\xa0\n 251 00:27:52,480 --> 00:27:59,519 pointer and then assign the value. And now I'll\xa0\n 252 00:27:59,519 --> 00:28:06,240 character is these many bytes and use the method\xa0\n 253 00:28:06,240 --> 00:28:12,640 P zero and the value at addresses asterik p zero.\xa0\n 254 00:28:13,359 --> 00:28:19,439 and let us see what's the output now, the first\xa0\n 255 00:28:19,440 --> 00:28:24,559 address because we are running the program fresh\xa0\n 256 00:28:24,559 --> 00:28:30,079 was from the previous run this will be a different\xa0\n 257 00:28:30,079 --> 00:28:35,359 are getting the value 1025. Now the next line\xa0\n 258 00:28:35,359 --> 00:28:42,159 addresses 5373032 which is the same address as\xa0\n 259 00:28:42,160 --> 00:28:50,400 time is one. Now why is this value one once\xa0\n 260 00:28:51,279 --> 00:28:57,440 then this will be the representation. When\xa0\n 261 00:28:57,440 --> 00:29:02,799 the address of P in p zero, then the address\xa0\n 262 00:29:02,799 --> 00:29:08,799 is stored in p zero. But when we dereference p\xa0\n 263 00:29:08,799 --> 00:29:13,759 the machine says that hey, this is a pointer to\xa0\n 264 00:29:13,759 --> 00:29:19,839 So I look at only one byte to see the value.\xa0\n 265 00:29:20,559 --> 00:29:28,000 is one and that's why this output here is one.\xa0\n 266 00:29:28,640 --> 00:29:34,560 One to print the address p plus one and the\xa0\n 267 00:29:34,559 --> 00:29:42,000 we can add or subtract in an integer constant\xa0\n 268 00:29:42,000 --> 00:29:47,359 the only pointer arithmetic that is allowed is\xa0\n 269 00:29:47,920 --> 00:29:52,880 to the pointer. p plus one will take\xa0\n 270 00:29:52,880 --> 00:29:58,880 So it will skip four bytes and take us four bytes\xa0\n 271 00:29:58,880 --> 00:30:04,160 one and the value at V zero plus one. Okay now\xa0\n 272 00:30:04,720 --> 00:30:11,759 the address of a this time is 4456036 that's\xa0\n 273 00:30:12,480 --> 00:30:21,360 the value is 1025 p plus one is 4456040 if you\xa0\n 274 00:30:21,359 --> 00:30:25,919 because size of integer is four bytes and\xa0\n 275 00:30:27,119 --> 00:30:32,879 p takes us four bytes forward and this value is\xa0\n 276 00:30:32,880 --> 00:30:38,320 anything in this particular address. So, there is\xa0\n 277 00:30:38,319 --> 00:30:45,439 now, the address in PS row is also 4456036\xa0\n 278 00:30:45,440 --> 00:30:54,880 significant byte of a the value is one now, P zero\xa0\n 279 00:30:54,880 --> 00:31:00,240 one byte more and this is because character is\xa0\n 280 00:31:00,880 --> 00:31:05,360 if you see p zero plus one will take us to this\xa0\n 281 00:31:05,359 --> 00:31:13,839 byte and this particular byte in binary is four\xa0\n 282 00:31:13,839 --> 00:31:19,519 when we dereference a pointer variable by using\xa0\n 283 00:31:19,519 --> 00:31:26,160 we perform pointer arithmetic with a particular\xa0\n 284 00:31:26,160 --> 00:31:32,640 from one to another also has some use cases we\xa0\n 285 00:31:32,640 --> 00:31:38,880 one pointer type which is generic pointer type\xa0\n 286 00:31:38,880 --> 00:31:44,320 and this pointer type is called void pointer\xa0\n 287 00:31:44,880 --> 00:31:50,560 by using the keyword void and using an Asterix\xa0\n 288 00:31:51,599 --> 00:31:59,039 we can write something like p zero is equal to p\xa0\n 289 00:31:59,039 --> 00:32:05,200 this, the statement p zero is equal to P is valid\xa0\n 290 00:32:05,839 --> 00:32:10,480 and because this particular pointer type\xa0\n 291 00:32:10,480 --> 00:32:15,200 we cannot dereference this particular pointer\xa0\n 292 00:32:15,200 --> 00:32:22,559 P naught or astrix p not this will give you\xa0\n 293 00:32:23,359 --> 00:32:28,799 we can only print the address. And as we can\xa0\n 294 00:32:28,799 --> 00:32:35,919 A and if we perform arithmetic, if we try to do\xa0\n 295 00:32:35,920 --> 00:32:40,080 like p zero plus one, this is also not possible\xa0\n 296 00:32:41,039 --> 00:32:44,159 we will come back to the\xa0\nuse cases of wide pointers\xa0\xa0 297 00:32:44,720 --> 00:32:49,680 in forthcoming lessons. Let's for now know\xa0\n 298 00:32:51,119 --> 00:32:58,559 So this was diving deep into pointer types,\xa0\n 299 00:32:58,559 --> 00:33:05,599 a lot of fun with pointers in the coming lessons.\xa0\n 300 00:33:05,599 --> 00:33:12,799 have seen how we can work with pointers in some of\xa0\n 301 00:33:12,799 --> 00:33:19,599 where pointers can be really puzzling. And once\xa0\n 302 00:33:19,599 --> 00:33:27,679 pointers. So in this lesson, we are going to see\xa0\n 303 00:33:27,680 --> 00:33:34,160 that this is a logical view of computer's memory\xa0\n 304 00:33:34,160 --> 00:33:39,600 we know that each byte of memory has an address,\xa0\n 305 00:33:39,599 --> 00:33:45,759 let us assume that the address increases as we go\xa0\n 306 00:33:45,759 --> 00:33:52,879 200 the next byte would be at address 201 and\xa0\n 307 00:33:53,599 --> 00:34:00,000 Now let's say in my program, I have\xa0\n 308 00:34:00,880 --> 00:34:08,240 and maybe I have initialized it as five and now\xa0\n 309 00:34:08,239 --> 00:34:14,799 will be allocated for this variable x. In a\xa0\n 310 00:34:14,800 --> 00:34:21,280 integer is stored in four bytes, so four byte\xa0\n 311 00:34:21,280 --> 00:34:29,200 four bytes at address 225. This block of four\xa0\n 312 00:34:29,920 --> 00:34:37,119 And the values stored in this block of four bytes\xa0\n 313 00:34:37,119 --> 00:34:44,319 do is I want to declare a pointer variable that\xa0\n 314 00:34:44,320 --> 00:34:50,000 address of an integer we will have to declare a\xa0\n 315 00:34:50,000 --> 00:34:55,920 to put an asterisk sign in front of the variable\xa0\n 316 00:34:55,920 --> 00:35:02,000 of memory will be reserved for this variable p in\xa0\n 317 00:35:02,000 --> 00:35:10,159 in four bytes. So let's say we get this block of\xa0\n 318 00:35:10,159 --> 00:35:17,199 to write a statement like this. So I want to fill\xa0\n 319 00:35:17,199 --> 00:35:24,000 to x ampersand operator gives us the address\xa0\n 320 00:35:24,639 --> 00:35:33,039 we are able to store the address of x in p because\xa0\n 321 00:35:33,039 --> 00:35:39,119 pointer to character, or pointer to some other\xa0\n 322 00:35:39,119 --> 00:35:45,039 X would not have been valid. So if I have to write\xa0\n 323 00:35:45,039 --> 00:35:53,599 an integer, and p is a pointer to integer. Or I\xa0\n 324 00:35:54,239 --> 00:36:00,959 And y pointer variables are strongly typed. Why\xa0\n 325 00:36:00,960 --> 00:36:06,880 or store the address of an integer? It is because\xa0\n 326 00:36:06,880 --> 00:36:14,800 pointer variable. We also use the pointer variable\xa0\n 327 00:36:14,800 --> 00:36:21,519 value there. So if I write a statement like this,\xa0\n 328 00:36:21,519 --> 00:36:28,639 now modified. Now can I create a pointer to this\xa0\n 329 00:36:29,199 --> 00:36:34,239 Well, yes, we actually we can do so. So let's\xa0\n 330 00:36:35,280 --> 00:36:40,560 that will store the address of P. Now what\xa0\n 331 00:36:40,559 --> 00:36:45,920 a pointer of a particular type to store the\xa0\n 332 00:36:45,920 --> 00:36:51,760 So to store a pointer to integer, we will\xa0\n 333 00:36:52,880 --> 00:36:58,559 to pointers, so we will put two Asterix sign\xa0\n 334 00:36:58,559 --> 00:37:08,480 variable q can store the address of P. So with\xa0\n 335 00:37:08,480 --> 00:37:16,320 we created q let's say we created q at 205. And\xa0\n 336 00:37:16,320 --> 00:37:26,320 P. So q points to P and the type of Q is int\xa0\n 337 00:37:27,760 --> 00:37:32,800 simple way to read this is that the type\xa0\n 338 00:37:32,800 --> 00:37:40,080 the address of x we will need a pointer of type\xa0\n 339 00:37:40,079 --> 00:37:45,920 this is a pointer to that particular type.\xa0\n 340 00:37:45,920 --> 00:37:52,639 a pointer to int star so we will put one extra\xa0\n 341 00:37:52,639 --> 00:37:58,159 And we can go on like this. Let's say we want\xa0\n 342 00:37:58,159 --> 00:38:03,599 int asterik asticus pointer to pointer and\xa0\n 343 00:38:03,599 --> 00:38:08,400 these three asterik immediately after this\xa0\n 344 00:38:08,400 --> 00:38:14,480 before the variable name like we have done in\xa0\n 345 00:38:14,480 --> 00:38:24,960 star star star. So let's say R gets this address\xa0\n 346 00:38:24,960 --> 00:38:30,159 astrick Asterix so it can store the address of\xa0\n 347 00:38:30,159 --> 00:38:37,039 address of X or P all the statements something\xa0\n 348 00:38:38,400 --> 00:38:45,680 Now, I have translated my previous rough code\xa0\n 349 00:38:45,679 --> 00:38:51,199 this program is working as per this memory\xa0\n 350 00:38:51,199 --> 00:38:57,599 now I will write some print statements and you\xa0\n 351 00:38:57,599 --> 00:39:05,279 to assume that these variables x p, q and r are\xa0\n 352 00:39:05,280 --> 00:39:12,240 in the right. So the first print statement that\xa0\n 353 00:39:12,800 --> 00:39:18,880 and this one should be simple for you asterik\xa0\n 354 00:39:18,880 --> 00:39:24,160 will be six. Now the next statement\xa0\n 355 00:39:24,880 --> 00:39:31,920 So astrick q will be a value at address stored\xa0\n 356 00:39:32,719 --> 00:39:41,679 this is nothing but the value of p so this will be\xa0\n 357 00:39:41,679 --> 00:39:50,000 want to do dereferencing twice. First I want to go\xa0\n 358 00:39:50,000 --> 00:39:58,800 want to look at the value at address 225. So this\xa0\n 359 00:39:58,800 --> 00:40:04,880 paranthesis here We could have said Astrid,\xa0\n 360 00:40:05,519 --> 00:40:10,960 that would have meant the same. But it's a good\xa0\n 361 00:40:10,960 --> 00:40:18,079 to use paranthesis wherever we can do it because\xa0\n 362 00:40:18,800 --> 00:40:23,840 we are not sure about the proceedings,\xa0\n 363 00:40:25,920 --> 00:40:32,240 Now, what about these two print statements asterik\xa0\n 364 00:40:32,239 --> 00:40:38,719 r means value in Q which will be 215. And then\xa0\n 365 00:40:39,760 --> 00:40:45,280 value in p which is 225. And one more\xa0\n 366 00:40:45,280 --> 00:40:52,240 x which will be six. If you see this is really\xa0\n 367 00:40:52,239 --> 00:40:58,719 operator and we are able to go to Q then we are\xa0\n 368 00:40:58,719 --> 00:41:04,319 and then we are using the astrick operator thrice\xa0\n 369 00:41:04,320 --> 00:41:11,039 this code and run this in a real compiler. So what\xa0\n 370 00:41:13,199 --> 00:41:21,119 And then after this statement, I'll print the\xa0\n 371 00:41:21,119 --> 00:41:26,960 asterik p astrochemistry q and astrochemistry\xa0\n 372 00:41:27,760 --> 00:41:34,720 the address will not be same as we had shown in\xa0\n 373 00:41:34,719 --> 00:41:41,359 see, we are able to modify x by doing this\xa0\n 374 00:41:41,360 --> 00:41:50,559 r if we would write something like astrochemistry\xa0\n 375 00:41:52,239 --> 00:41:59,119 Now asterik P is also referencing x and\xa0\n 376 00:41:59,119 --> 00:42:04,719 we are dereferencing here. So any guesses what\xa0\n 377 00:42:05,280 --> 00:42:10,800 Well, if you see x is incremented by two\xa0\n 378 00:42:10,800 --> 00:42:16,720 I recommend that you write some of this code\xa0\n 379 00:42:16,719 --> 00:42:22,719 lessons we will use pointer to pointer in some\xa0\n 380 00:42:24,159 --> 00:42:30,879 In our previous lessons, we define pointer\xa0\n 381 00:42:30,880 --> 00:42:37,280 upon pointer variables how to work with pointer\xa0\n 382 00:42:37,280 --> 00:42:43,280 really talk about the real use cases of pointer\xa0\n 383 00:42:43,280 --> 00:42:48,800 use pointer variables. So, in this lesson, we\xa0\n 384 00:42:48,800 --> 00:42:56,800 variables and the use cases using them using\xa0\n 385 00:42:56,800 --> 00:43:04,960 this as call by reference. So, let's discuss\xa0\n 386 00:43:04,960 --> 00:43:11,440 and he has recently learned about the concept\xa0\n 387 00:43:11,440 --> 00:43:17,840 this concept and he writes a simple C\xa0\n 388 00:43:17,840 --> 00:43:25,120 achieve here is that he has an integer variable\xa0\n 389 00:43:25,119 --> 00:43:29,759 and he wants to increment the value in this\xa0\n 390 00:43:29,760 --> 00:43:36,080 something like a plus plus or a equal to a plus\xa0\n 391 00:43:36,079 --> 00:43:43,119 he writes a function increment that will take an\xa0\n 392 00:43:43,119 --> 00:43:49,039 is writing a statement like a is equal to a plus\xa0\n 393 00:43:49,039 --> 00:43:56,400 method passing a as argument and then he prints\xa0\n 394 00:43:57,119 --> 00:44:04,079 is that the value of a will be incremented to\xa0\n 395 00:44:04,079 --> 00:44:11,519 equal leaven, but when he runs the program, the\xa0\n 396 00:44:11,519 --> 00:44:17,519 Albert does not understand why this is happening,\xa0\n 397 00:44:17,519 --> 00:44:23,119 initialized this variable to 10 and then he is\xa0\n 398 00:44:23,119 --> 00:44:29,679 and the same a is being being incremented by one\xa0\n 399 00:44:30,400 --> 00:44:37,039 printed is not leaven. Why is it 10 now,\xa0\n 400 00:44:37,599 --> 00:44:43,519 or what he probably forgot, is that whenever\xa0\n 401 00:44:44,079 --> 00:44:49,840 then we call that variable a local variable,\xa0\n 402 00:44:50,400 --> 00:44:55,760 we can access that variable only inside the same\xa0\n 403 00:44:56,320 --> 00:45:00,880 So these two a this a in the function\xa0\n 404 00:45:01,440 --> 00:45:06,960 are actually not the same a, the A in\xa0\n 405 00:45:06,960 --> 00:45:15,199 when main calls the method increment and passes\xa0\n 406 00:45:15,199 --> 00:45:22,239 value of a is copied to this another A, which is\xa0\n 407 00:45:22,880 --> 00:45:27,200 So what I'll do is I'll do couple of modifications\xa0\n 408 00:45:28,159 --> 00:45:32,960 I will write two print statements in\xa0\n 409 00:45:32,960 --> 00:45:41,280 increment method something like this address\xa0\n 410 00:45:41,280 --> 00:45:46,480 if we put ampersand operator in front of the\xa0\n 411 00:45:46,480 --> 00:45:54,240 variable. And I'll comment out this print and I'll\xa0\n 412 00:45:55,119 --> 00:46:01,199 And here I print that address of variable a in\xa0\n 413 00:46:01,199 --> 00:46:10,960 what happens. Let me also put an end of line after\xa0\n 414 00:46:10,960 --> 00:46:19,280 of variable a in increment method is printed as\xa0\n 415 00:46:22,159 --> 00:46:27,759 This two looks similar, but they're not the same.\xa0\n 416 00:46:29,360 --> 00:46:34,079 What the values are is not important, what\xa0\n 417 00:46:34,079 --> 00:46:41,440 important is that these addresses are different.\xa0\n 418 00:46:41,440 --> 00:46:47,840 was same, these two addresses would have\xa0\n 419 00:46:47,840 --> 00:46:54,320 we will try to understand how things happen in\xa0\n 420 00:46:54,320 --> 00:47:00,720 a program or an application is started, then the\xa0\n 421 00:47:00,719 --> 00:47:07,359 reserves some amount of memory for the execution\xa0\n 422 00:47:07,360 --> 00:47:14,400 for the application is divided into is typically\xa0\n 423 00:47:14,400 --> 00:47:21,200 here one part of memory is allocated to store\xa0\n 424 00:47:21,199 --> 00:47:25,679 computer needs to keep all these instructions in\xa0\n 425 00:47:25,679 --> 00:47:31,599 program like increment or declare these variables\xa0\n 426 00:47:31,599 --> 00:47:38,880 memory is one part of the allocated memory is for\xa0\n 427 00:47:38,880 --> 00:47:46,559 a variable inside a function in c++ or C then\xa0\n 428 00:47:46,559 --> 00:47:52,799 can be accessed and modified anywhere in the\xa0\n 429 00:47:53,519 --> 00:47:57,840 accessed and modified within a particular\xa0\n 430 00:47:58,719 --> 00:48:06,159 Now, the third part of the applications memory\xa0\n 431 00:48:06,159 --> 00:48:12,639 this is where all the local variables go and we\xa0\n 432 00:48:13,280 --> 00:48:20,960 And the fourth part the fourth part is heap. And\xa0\n 433 00:48:20,960 --> 00:48:28,240 lessons have these four segments of the allocated\xa0\n 434 00:48:28,239 --> 00:48:34,159 segment and the stack segment, these three are\xa0\n 435 00:48:34,159 --> 00:48:40,960 starts executing the application however, can keep\xa0\n 436 00:48:41,679 --> 00:48:48,000 during its execution only. We will cover all of\xa0\n 437 00:48:48,000 --> 00:48:53,679 please do not get scared by these terms.\xa0\n 438 00:48:54,320 --> 00:49:01,600 executes. Let us say this is our computer's\xa0\n 439 00:49:01,599 --> 00:49:08,000 in the memory is addressable. So, let's say the\xa0\n 440 00:49:08,000 --> 00:49:16,079 200 to 800. And these are the various segments of\xa0\n 441 00:49:16,079 --> 00:49:22,960 address 300 to 600 is allocated for stack.\xa0\n 442 00:49:23,599 --> 00:49:29,759 after address 800 and before address 200\xa0\n 443 00:49:29,760 --> 00:49:34,400 the memory is assigned for our program, let's\xa0\n 444 00:49:35,599 --> 00:49:41,759 Now, when a function is invoked, like when the\xa0\n 445 00:49:41,760 --> 00:49:47,760 invoked, all the information about the method\xa0\n 446 00:49:47,760 --> 00:49:53,360 like its parameters, all the local variables,\xa0\n 447 00:49:54,239 --> 00:50:00,559 The current instruction at which it is executing.\xa0\n 448 00:50:00,559 --> 00:50:08,400 we will take out from the stack some part for the\xa0\n 449 00:50:08,400 --> 00:50:16,720 frame, each function will have a stack frame. Now\xa0\n 450 00:50:16,719 --> 00:50:22,480 from this stack frame and the value of a is 10.\xa0\n 451 00:50:23,039 --> 00:50:28,559 what happens when main method calls increment\xa0\n 452 00:50:28,559 --> 00:50:34,880 your execution for some time, I will stop at this\xa0\n 453 00:50:34,880 --> 00:50:39,840 this method increment. And then I'll come back\xa0\n 454 00:50:40,400 --> 00:50:45,440 now another stack frame is allocated for the\xa0\n 455 00:50:45,440 --> 00:50:51,039 method like we have a parameter a So, fresh local\xa0\n 456 00:50:51,039 --> 00:50:56,400 parameters and whatever values have been passed\xa0\n 457 00:50:57,519 --> 00:51:03,679 Now, when we say a is equal to a plus one\xa0\n 458 00:51:03,679 --> 00:51:07,679 this a which is local to the increment\xa0\n 459 00:51:07,679 --> 00:51:16,079 this is incremented we cannot access a variable it\xa0\n 460 00:51:16,079 --> 00:51:21,599 when increment finishes the control returns to\xa0\n 461 00:51:21,599 --> 00:51:27,360 it clears the stack frame that was located for\xa0\n 462 00:51:27,360 --> 00:51:33,039 method was paused at this particular instruction\xa0\n 463 00:51:33,679 --> 00:51:39,839 till the time the function is executing. Now,\xa0\n 464 00:51:39,840 --> 00:51:44,800 the function print f print F is not a user\xa0\n 465 00:51:44,800 --> 00:51:49,440 the state of execution of main method is\xa0\n 466 00:51:49,440 --> 00:51:56,240 we often call this particular structure call stack\xa0\n 467 00:51:56,239 --> 00:52:02,879 the top of the stack is executing and remember\xa0\n 468 00:52:02,880 --> 00:52:08,400 scenario where one function keeps calling another\xa0\n 469 00:52:08,400 --> 00:52:13,280 an infinite recursion, then the memory of this\xa0\n 470 00:52:14,079 --> 00:52:19,199 okay but that is not relevant for this scenario.\xa0\n 471 00:52:19,199 --> 00:52:24,719 happens when one function calls another function.\xa0\n 472 00:52:25,440 --> 00:52:32,159 Main is our calling function and increment is\xa0\n 473 00:52:32,159 --> 00:52:38,639 the calling function, the argument is also known\xa0\n 474 00:52:38,639 --> 00:52:44,480 the argument is known as formal argument. All\xa0\n 475 00:52:44,480 --> 00:52:51,119 mapped to a formal argument. So, when this\xa0\n 476 00:52:51,119 --> 00:52:57,199 actual argument is mapped to another A, which is a\xa0\n 477 00:52:57,199 --> 00:53:02,879 x here, so, we would have written something like\xa0\n 478 00:53:04,159 --> 00:53:10,960 a would have been mapped to x. So, the value of\xa0\n 479 00:53:10,960 --> 00:53:17,280 make such a function call, where we basically have\xa0\n 480 00:53:17,280 --> 00:53:23,600 value in one variable copied to another variable\xa0\n 481 00:53:23,599 --> 00:53:31,440 call by value. So, this is what Albert was doing,\xa0\n 482 00:53:31,440 --> 00:53:37,280 able to get the desired result. But can we get\xa0\n 483 00:53:37,280 --> 00:53:43,680 wanted to use this variable A, which is local to\xa0\n 484 00:53:44,480 --> 00:53:51,440 Can we really do so? Well yes, we can do so,\xa0\n 485 00:53:51,440 --> 00:53:57,039 us now look at this code. And I'm drawing only the\xa0\n 486 00:53:57,599 --> 00:54:03,440 of program execution neatly. Now, what we are\xa0\n 487 00:54:03,440 --> 00:54:09,200 which is integer in this function increment we\xa0\n 488 00:54:09,199 --> 00:54:14,559 and pointer to integer as we know we will it will\xa0\n 489 00:54:14,559 --> 00:54:20,320 are doing is that in the increment function, we\xa0\n 490 00:54:20,320 --> 00:54:25,280 will start executing the main method will be\xa0\n 491 00:54:25,280 --> 00:54:33,280 of the main method. Let's say 300 to 350. This\xa0\n 492 00:54:33,280 --> 00:54:40,640 would be a local variable a. In this main method,\xa0\n 493 00:54:42,480 --> 00:54:46,880 This may not be in proportion, but still\xa0\n 494 00:54:46,880 --> 00:54:52,720 Now when main method calls increment, then a\xa0\n 495 00:54:52,719 --> 00:54:58,719 created. And this is a pointer to integer and the\xa0\n 496 00:54:58,719 --> 00:55:03,279 The value that gets stored In this particular\xa0\n 497 00:55:03,280 --> 00:55:09,519 the value that gets copied or stored in this\xa0\n 498 00:55:11,199 --> 00:55:17,599 So P is pointing to a. Now in this statement\xa0\n 499 00:55:17,599 --> 00:55:25,199 this address, so we are seeing here that asterik\xa0\n 500 00:55:25,199 --> 00:55:30,960 increment the value stored at address p by one,\xa0\n 501 00:55:30,960 --> 00:55:39,360 by one, so, a is now 11. So, now when increment\xa0\n 502 00:55:39,360 --> 00:55:42,880 and the next line gets executed,\xa0\n 503 00:55:43,519 --> 00:55:49,679 then A is now 11. If you run this program,\xa0\n 504 00:55:50,320 --> 00:55:55,760 such a function call in which instead of passing\xa0\n 505 00:55:55,760 --> 00:56:01,120 the variables so that we have a reference to the\xa0\n 506 00:56:01,119 --> 00:56:08,079 some operations is called call by reference.\xa0\n 507 00:56:08,079 --> 00:56:13,279 then we are using call by reference call by\xa0\n 508 00:56:13,280 --> 00:56:19,600 instead of creating a copy of a large and complex\xa0\n 509 00:56:20,159 --> 00:56:23,759 and using a reference will also cost us\xa0\n 510 00:56:24,719 --> 00:56:30,959 then, we are saved from creating a new copy of a\xa0\n 511 00:56:30,960 --> 00:56:37,440 see more of the layout of applications memory,\xa0\n 512 00:56:37,440 --> 00:56:46,559 thanks for watching the concept of pointers and\xa0\n 513 00:56:46,559 --> 00:56:51,840 strong relationship between these two concepts.\xa0\n 514 00:56:51,840 --> 00:56:59,680 relationship. When we declare an array, let's\xa0\n 515 00:57:00,320 --> 00:57:09,120 then we create five integer variables named is\xa0\n 516 00:57:09,119 --> 00:57:15,199 five integers will be stored in the memory as a\xa0\n 517 00:57:15,840 --> 00:57:20,880 this, what we are showing here in the right,\xa0\n 518 00:57:20,880 --> 00:57:27,119 is stored at address, let's say address 200. And\xa0\n 519 00:57:27,119 --> 00:57:34,719 is stored in four bytes. So a one will be four\xa0\n 520 00:57:34,719 --> 00:57:41,759 at 208, a three will be at 212. A four will be\xa0\n 521 00:57:42,639 --> 00:57:49,039 20 bytes, and these 20 bytes will be one\xa0\n 522 00:57:49,039 --> 00:57:55,360 the section of the memory in which a is stored.\xa0\n 523 00:57:55,360 --> 00:58:01,039 something like this, from left to right, we\xa0\n 524 00:58:01,039 --> 00:58:05,360 are just for the sake of understanding,\xa0\n 525 00:58:05,360 --> 00:58:11,440 the memory. And this time, I'll show this memory\xa0\n 526 00:58:11,440 --> 00:58:17,440 I can accommodate a couple of more variables.\xa0\n 527 00:58:18,800 --> 00:58:28,960 an integer variable x and its value is five. And\xa0\n 528 00:58:28,960 --> 00:58:39,599 say I have a pointer to integer P and P, I store\xa0\n 529 00:58:39,599 --> 00:58:48,639 in p would be 300. So, this statement will print\xa0\n 530 00:58:48,639 --> 00:58:56,719 address stored in Try to print the values stored\xa0\n 531 00:58:58,000 --> 00:59:01,920 this is fine. But we also know that\xa0\n 532 00:59:02,719 --> 00:59:07,679 increment or decrement a pointer variable\xa0\n 533 00:59:07,679 --> 00:59:12,319 p equal p plus one and this will take\xa0\n 534 00:59:12,960 --> 00:59:20,880 and because integer is four bytes, so now p would\xa0\n 535 00:59:20,880 --> 00:59:28,240 output should be 304. But if we try to dereference\xa0\n 536 00:59:28,239 --> 00:59:34,000 know the value at this address. So we cannot say\xa0\n 537 00:59:34,000 --> 00:59:41,199 X lives at address 300 but we do not know who is\xa0\n 538 00:59:42,400 --> 00:59:47,599 But for this integer array A, let us say I'm\xa0\n 539 00:59:48,639 --> 00:59:57,039 which is located at address 200. If I declare a\xa0\n 540 00:59:57,039 --> 01:00:04,320 first element by putting an ampersand operator\xa0\n 541 01:00:04,320 --> 01:00:13,280 us in this scenario, the output 200 and printing\xa0\n 542 01:00:13,280 --> 01:00:17,280 say we have these values in the array,\xa0\n 543 01:00:19,840 --> 01:00:29,200 So Asterix p would be two. Now if I want to\xa0\n 544 01:00:30,400 --> 01:00:37,920 204. And if I dereference p plus one, and try\xa0\n 545 01:00:37,920 --> 01:00:43,680 And similarly if we wanted the third element\xa0\n 546 01:00:44,480 --> 01:00:50,079 So using pointer arithmetic makes sense in the\xa0\n 547 01:00:50,079 --> 01:00:56,239 because we know what is in the adjacent location,\xa0\n 548 01:00:56,239 --> 01:01:02,559 if we just use the name of the array A, then a\xa0\n 549 01:01:02,559 --> 01:01:08,079 array. So we can write a statement like p is equal\xa0\n 550 01:01:08,079 --> 01:01:16,159 address in another pointer variable. If we simply\xa0\n 551 01:01:16,159 --> 01:01:23,839 of the first element in the array. And if we want\xa0\n 552 01:01:24,719 --> 01:01:31,759 then this will give us the value. So if we want\xa0\n 553 01:01:31,760 --> 01:01:38,640 one, then this will give us the address to 04 and\xa0\n 554 01:01:38,639 --> 01:01:44,319 the second element in the array value of the\xa0\n 555 01:01:44,320 --> 01:01:51,840 in the array. at index i we can retrieve the\xa0\n 556 01:01:52,400 --> 01:02:04,160 using either ampersand AI, or simply a plus i\xa0\n 557 01:02:04,159 --> 01:02:14,719 the value of AI can be retrieved using either we\xa0\n 558 01:02:14,719 --> 01:02:20,399 an Asterix a plus I will also give us the value.\xa0\n 559 01:02:20,400 --> 01:02:28,480 ampersand AI or a plus i for each other and they\xa0\n 560 01:02:28,480 --> 01:02:35,199 plus AI for each other and they mean the same. The\xa0\n 561 01:02:35,199 --> 01:02:42,239 be called the base address. And a simply using\xa0\n 562 01:02:43,039 --> 01:02:50,079 of the array. Let us now see some code examples\xa0\n 563 01:02:51,199 --> 01:02:58,960 In my program, let's say we have an integer\xa0\n 564 01:02:59,840 --> 01:03:03,680 a then this should give us the address\xa0\n 565 01:03:04,239 --> 01:03:08,959 And we can also get the address of the first\xa0\n 566 01:03:08,960 --> 01:03:15,519 in front of a zero. If I simply print a zero\xa0\n 567 01:03:15,519 --> 01:03:22,639 and we can also print the first element in the\xa0\n 568 01:03:23,360 --> 01:03:30,480 the variable name a. Let us now run this and see\xa0\n 569 01:03:30,480 --> 01:03:37,199 after each of these print statements. Okay, so the\xa0\n 570 01:03:37,840 --> 01:03:42,000 they are giving us the address of the first\xa0\n 571 01:03:42,000 --> 01:03:47,519 are giving us the value. In fact, if we run\xa0\n 572 01:03:48,480 --> 01:03:54,880 then we can print address of the element\xa0\n 573 01:03:55,679 --> 01:04:03,919 and we can print the value of if element as a AI\xa0\n 574 01:04:03,920 --> 01:04:10,400 here, this is a fresh run a new run so the address\xa0\n 575 01:04:10,400 --> 01:04:17,360 address printed in two lines for a zero is same\xa0\n 576 01:04:17,360 --> 01:04:23,840 four bytes I had off the previous address and the\xa0\n 577 01:04:25,519 --> 01:04:31,840 Now one more thing even though just using\xa0\n 578 01:04:32,400 --> 01:04:38,639 base address or the address of the first\xa0\n 579 01:04:38,639 --> 01:04:44,079 a against some pointer variable like this. We\xa0\n 580 01:04:44,079 --> 01:04:49,679 value of a this will give us compilation error. We\xa0\n 581 01:04:50,480 --> 01:04:57,039 a to some pointer variable other pointer variable\xa0\n 582 01:04:59,039 --> 01:05:06,000 So this As far as how arrays are stored in memory\xa0\n 583 01:05:06,000 --> 01:05:13,039 we can access the values using pointers, in the\xa0\n 584 01:05:13,039 --> 01:05:20,159 about character arrays. And we will talk about\xa0\n 585 01:05:20,159 --> 01:05:26,879 for watching. In this lesson, we will be talking\xa0\n 586 01:05:26,880 --> 01:05:34,640 arrays and pointers go together. And this scenario\xa0\n 587 01:05:35,760 --> 01:05:41,840 let us see a code example where we pass array\xa0\n 588 01:05:42,480 --> 01:05:49,519 I will write a simple C program in which I will\xa0\n 589 01:05:49,519 --> 01:05:55,519 all the elements in an integer array, this\xa0\n 590 01:05:56,159 --> 01:06:01,839 this is one of the ways to pass an integer array,\xa0\n 591 01:06:01,840 --> 01:06:08,400 the data type and this would be the name of the\xa0\n 592 01:06:08,400 --> 01:06:14,320 and initialized an array named a. Now, in the main\xa0\n 593 01:06:14,880 --> 01:06:21,599 and pass it the array as argument. And of course,\xa0\n 594 01:06:21,599 --> 01:06:28,239 of elements, I will declare two variables I and\xa0\n 595 01:06:28,880 --> 01:06:33,200 I will be used to run a loop. So, we will\xa0\n 596 01:06:33,199 --> 01:06:39,279 array that is passed as argument. But wait a\xa0\n 597 01:06:40,320 --> 01:06:46,160 is there some way to know the size of the\xa0\n 598 01:06:46,159 --> 01:06:50,079 of the array and by the size of the array, we\xa0\n 599 01:06:50,639 --> 01:06:57,599 is to use the function size off. So if we write\xa0\n 600 01:06:58,159 --> 01:07:04,000 then this will give us the size of the array\xa0\n 601 01:07:04,000 --> 01:07:09,920 typical compiler, and I know for sure that in this\xa0\n 602 01:07:09,920 --> 01:07:17,039 size of a will return 20. If we want to know the\xa0\n 603 01:07:17,039 --> 01:07:24,239 something like divide the size of the size and\xa0\n 604 01:07:24,800 --> 01:07:31,280 So we will say size of a upon size of each integer\xa0\n 605 01:07:31,280 --> 01:07:36,000 number of elements in the array.\xa0\n 606 01:07:36,000 --> 01:07:40,719 below this line the line where we are\xa0\n 607 01:07:41,280 --> 01:07:46,880 Now, what we can do is we could either pass\xa0\n 608 01:07:48,800 --> 01:07:55,440 So that would be cool. And then we can\xa0\n 609 01:07:55,440 --> 01:08:03,519 minus one and we keep on adding each\xa0\n 610 01:08:03,519 --> 01:08:11,280 we return sum. And finally, we will write\xa0\n 611 01:08:12,400 --> 01:08:18,399 to print the sum of all the elements in the array,\xa0\n 612 01:08:18,399 --> 01:08:25,439 the output on console is that the sum of elements\xa0\n 613 01:08:25,439 --> 01:08:31,519 plus one is 15. So, this is all right. Now, what I\xa0\n 614 01:08:32,079 --> 01:08:38,079 I do not want to pass the size as argument here,\xa0\n 615 01:08:38,079 --> 01:08:44,399 this function. So, we will not be passing\xa0\n 616 01:08:44,399 --> 01:08:51,039 there will be just one argument the array. So\xa0\n 617 01:08:51,039 --> 01:08:58,720 calculate the size here as total size of the array\xa0\n 618 01:08:58,720 --> 01:09:04,320 Let us now run this program and see what\xa0\n 619 01:09:04,319 --> 01:09:10,319 because the sum of elements that we have as output\xa0\n 620 01:09:11,119 --> 01:09:16,640 And why is it so, explain this behavior,\xa0\n 621 01:09:17,199 --> 01:09:25,439 what is the function sum of elements where I will\xa0\n 622 01:09:25,439 --> 01:09:32,000 be calling these two functions size off upon a and\xa0\n 623 01:09:32,000 --> 01:09:36,880 more print statements in the main method. It will\xa0\n 624 01:09:36,880 --> 01:09:43,600 bytes and the size of a zero in bytes. Let us now\xa0\n 625 01:09:43,600 --> 01:09:49,600 of A is equal to four bytes. size of a zero is\xa0\n 626 01:09:49,600 --> 01:09:56,160 phase equal to 20 bytes and the size of a zero\xa0\n 627 01:09:56,880 --> 01:10:04,400 why the size of a is four by In the method sum of\xa0\n 628 01:10:04,399 --> 01:10:12,559 method, we need to dive deep into how compiler\xa0\n 629 01:10:12,560 --> 01:10:19,120 again I will draw this familiar memory diagram,\xa0\n 630 01:10:19,119 --> 01:10:24,640 is typically divided into these four parts, we\xa0\n 631 01:10:24,640 --> 01:10:31,440 for the execution of function calls, we use\xa0\n 632 01:10:31,439 --> 01:10:38,479 here is our stack. Now, when the program starts\xa0\n 633 01:10:38,479 --> 01:10:44,799 some amount of memory from the stack is allocated\xa0\n 634 01:10:45,439 --> 01:10:52,399 and this particular section or this particular\xa0\n 635 01:10:52,399 --> 01:10:58,799 all the local variables reside within this\xa0\n 636 01:10:58,800 --> 01:11:05,199 two local variables one is the array and\xa0\n 637 01:11:05,760 --> 01:11:12,640 So, around 20 bytes if integer is four bytes,\xa0\n 638 01:11:13,199 --> 01:11:21,279 from this stack frame for the local variable a the\xa0\n 639 01:11:22,399 --> 01:11:25,920 and there would be some more information\xa0\n 640 01:11:25,920 --> 01:11:30,480 execution reaches this particular line,\xa0\n 641 01:11:30,479 --> 01:11:38,879 then the execution of main is paused and another\xa0\n 642 01:11:39,520 --> 01:11:45,920 sum of elements. We have talked about this\xa0\n 643 01:11:45,920 --> 01:11:50,960 a number of times in our previous lessons, okay,\xa0\n 644 01:11:50,960 --> 01:11:57,920 a local variable to the function sum of elements\xa0\n 645 01:11:57,920 --> 01:12:03,279 when we make a function call is that the value of\xa0\n 646 01:12:03,279 --> 01:12:07,840 or we should rather say the value from\xa0\n 647 01:12:07,840 --> 01:12:16,800 copied to the variable in the call function. So\xa0\n 648 01:12:16,800 --> 01:12:23,360 function called sum of elements. and the value\xa0\n 649 01:12:25,520 --> 01:12:31,600 So we would have another array named a, taking\xa0\n 650 01:12:31,600 --> 01:12:37,680 and it would have the same value same\xa0\n 651 01:12:38,399 --> 01:12:47,199 main. But actually, this doesn't happen, when\xa0\n 652 01:12:47,840 --> 01:12:56,880 it does not copy the whole array, what it actually\xa0\n 653 01:12:56,880 --> 01:13:02,960 same name, instead of creating the whole array,\xa0\n 654 01:13:02,960 --> 01:13:09,600 the array. So in this case, it will be pointed to\xa0\n 655 01:13:09,600 --> 01:13:16,720 of the first element in the array of the calling\xa0\n 656 01:13:16,720 --> 01:13:22,960 the address of the first element in this array,\xa0\n 657 01:13:22,960 --> 01:13:28,800 in sum of elements, all that happens is a pointer\xa0\n 658 01:13:28,800 --> 01:13:39,119 pointer to integer is 200. The compiler implicitly\xa0\n 659 01:13:39,119 --> 01:13:45,920 that is this a here in this particular format is\xa0\n 660 01:13:45,920 --> 01:13:53,119 as pointer to integer. So whether you write int\xa0\n 661 01:13:53,119 --> 01:14:01,439 you write something like in Star space a, they're\xa0\n 662 01:14:01,439 --> 01:14:06,639 of copying the value of the variable, we are just\xa0\n 663 01:14:07,600 --> 01:14:14,240 So, we make a call by reference here, arrays\xa0\n 664 01:14:14,960 --> 01:14:20,960 And this makes a lot of sense, because arrays\xa0\n 665 01:14:20,960 --> 01:14:26,640 much sense to create a new copy of the array each\xa0\n 666 01:14:27,439 --> 01:14:32,239 So for arrays, there is no call by value\xa0\n 667 01:14:32,239 --> 01:14:40,000 that's why this size of a here would give us four\xa0\n 668 01:14:40,000 --> 01:14:47,439 while here in the main method is an array. So this\xa0\n 669 01:14:47,439 --> 01:14:55,119 we put these braces to tell that this is an array\xa0\n 670 01:14:55,119 --> 01:15:00,000 compiler. You can put an asterisk sign here or you\xa0\n 671 01:15:00,880 --> 01:15:06,000 It makes more sense to write it like this.\xa0\n 672 01:15:06,000 --> 01:15:12,640 integer. That's why when we pass array as function\xa0\n 673 01:15:12,640 --> 01:15:18,720 elements in the array. By using a trick like this,\xa0\n 674 01:15:19,359 --> 01:15:25,839 So I'd rather move this size calculation\xa0\n 675 01:15:26,720 --> 01:15:32,560 And when we run this program, now, we get\xa0\n 676 01:15:33,359 --> 01:15:40,559 15. We should always keep in mind that a\xa0\n 677 01:15:40,560 --> 01:15:45,520 is different from a variable which is pointed\xa0\n 678 01:15:45,520 --> 01:15:51,840 some privileges, like we can use the name of the\xa0\n 679 01:15:52,560 --> 01:15:57,360 like in this function sum of elements, we could\xa0\n 680 01:15:57,359 --> 01:16:01,119 we should, we could have said ampersand\xa0\n 681 01:16:02,399 --> 01:16:10,719 But if we just use a instead of using ampersand a\xa0\n 682 01:16:10,720 --> 01:16:16,560 if a is an array, we cannot do something like\xa0\n 683 01:16:16,560 --> 01:16:20,880 variables. With pointer variables,\xa0\n 684 01:16:21,520 --> 01:16:25,680 And if we have a pointer to the starting\xa0\n 685 01:16:25,680 --> 01:16:34,000 it like a variable name for array because II II\xa0\n 686 01:16:34,000 --> 01:16:39,600 we need to keep these differences and similarities\xa0\n 687 01:16:39,600 --> 01:16:47,120 thing because the array is passed by reference,\xa0\n 688 01:16:47,680 --> 01:16:54,240 call function, and it would refer reflect in\xa0\n 689 01:16:55,279 --> 01:17:00,399 calculating the sum total, we want to double\xa0\n 690 01:17:00,399 --> 01:17:08,559 array. So our code will go something like this, I\xa0\n 691 01:17:08,560 --> 01:17:14,000 not return an integer let's say the return type\xa0\n 692 01:17:14,000 --> 01:17:19,680 this stuff. And what I'll do is I'll simply run\xa0\n 693 01:17:21,119 --> 01:17:27,359 Okay, let us now run this and see what happens.\xa0\n 694 01:17:27,920 --> 01:17:31,920 I must make a call before I print the\xa0\n 695 01:17:32,560 --> 01:17:39,440 If you see the elements in the array are modified\xa0\n 696 01:17:40,000 --> 01:17:48,800 because this is called by reference the same copy\xa0\n 697 01:17:50,720 --> 01:17:57,840 So this was array as function arguments. An\xa0\n 698 01:17:58,960 --> 01:18:05,199 is when we work with strings in C strings\xa0\n 699 01:18:06,079 --> 01:18:11,279 This concept really becomes important in the\xa0\n 700 01:18:11,279 --> 01:18:17,920 about it in the coming lessons. So thanks for\xa0\n 701 01:18:18,479 --> 01:18:23,839 how the concepts of arrays and pointers go\xa0\n 702 01:18:23,840 --> 01:18:28,640 to talk about character arrays, how we can\xa0\n 703 01:18:29,520 --> 01:18:34,640 When we talk about character arrays in C,\xa0\n 704 01:18:35,520 --> 01:18:42,960 Strings are group or set of characters,\xa0\n 705 01:18:42,960 --> 01:18:48,560 and sentences. All of these are strings to us.\xa0\n 706 01:18:49,119 --> 01:18:55,199 Character arrays become all the more important\xa0\n 707 01:18:55,199 --> 01:19:02,479 perform a lot of operations on strings, like\xa0\n 708 01:19:02,479 --> 01:19:08,799 two strings or finding out properties of strings\xa0\n 709 01:19:08,800 --> 01:19:14,159 able to work with strings efficiently in C, there\xa0\n 710 01:19:14,720 --> 01:19:19,280 The first thing that we need to understand\xa0\n 711 01:19:19,840 --> 01:19:25,360 To be able to store a string in a character\xa0\n 712 01:19:25,359 --> 01:19:31,199 the character array should be large enough to\xa0\n 713 01:19:31,199 --> 01:19:38,319 character array. A large enough character array is\xa0\n 714 01:19:38,319 --> 01:19:44,799 number of characters in the string\xa0\n 715 01:19:45,359 --> 01:19:51,839 a name of four characters, like john, then\xa0\n 716 01:19:51,840 --> 01:19:58,560 or equal to five. Now the obvious question would\xa0\n 717 01:19:58,560 --> 01:20:05,520 Isn't it that if we layer, character array of size\xa0\n 718 01:20:05,520 --> 01:20:12,640 will go at the zero at index, four will go at the\xa0\n 719 01:20:12,640 --> 01:20:18,800 index respectively. So I'm storing all the data,\xa0\n 720 01:20:18,800 --> 01:20:25,199 Let's say we have a character array of size eight.\xa0\n 721 01:20:25,199 --> 01:20:30,399 and we want to store the string john, in this\xa0\n 722 01:20:30,399 --> 01:20:35,759 seven, and this is an array of size eight.\xa0\n 723 01:20:35,760 --> 01:20:41,920 the one at index H, add to it index and n add\xa0\n 724 01:20:41,920 --> 01:20:48,079 the characters of the string john in this array.\xa0\n 725 01:20:48,079 --> 01:20:52,800 or we have not stored the information that this\xa0\n 726 01:20:52,800 --> 01:20:59,680 the string, so the string ends at index three. So\xa0\n 727 01:20:59,680 --> 01:21:04,960 and that has to print the string john, or find out\xa0\n 728 01:21:04,960 --> 01:21:09,920 this array, then how would the function know that\xa0\n 729 01:21:09,920 --> 01:21:16,159 we have not filled anything in these indices at\xa0\n 730 01:21:16,159 --> 01:21:20,880 garbage value there. So either we pass this\xa0\n 731 01:21:21,920 --> 01:21:26,159 to store this information that this is the\xa0\n 732 01:21:26,159 --> 01:21:33,359 This is the used part of the string. And this is\xa0\n 733 01:21:33,359 --> 01:21:39,199 of the string in the next position, we put a null\xa0\n 734 01:21:39,199 --> 01:21:45,519 zero. And we can put a null character at position\xa0\n 735 01:21:46,079 --> 01:21:52,880 within single quotes, we put a forward slash\xa0\n 736 01:21:52,880 --> 01:21:59,119 the functions for string manipulation in C, expect\xa0\n 737 01:21:59,119 --> 01:22:06,800 a rule string in C has to be terminated by a null\xa0\n 738 01:22:07,520 --> 01:22:12,720 to store this null character. Let us try to see\xa0\n 739 01:22:12,720 --> 01:22:18,800 this concept better. What I have done here is I\xa0\n 740 01:22:18,800 --> 01:22:25,440 I have filled in the characters, but I have not\xa0\n 741 01:22:25,439 --> 01:22:31,199 using the printf function, this percentile, so it\xa0\n 742 01:22:31,199 --> 01:22:38,639 passing this array C. Let us run this program and\xa0\n 743 01:22:38,640 --> 01:22:45,200 bunch of characters that are getting printed after\xa0\n 744 01:22:45,199 --> 01:22:52,000 the assumption for the printf function that my\xa0\n 745 01:22:52,000 --> 01:22:57,119 So that's why this undefined behavior is coming.\xa0\n 746 01:22:57,119 --> 01:23:04,159 five and put a null termination, something like\xa0\n 747 01:23:04,159 --> 01:23:11,519 fine. Now even if we change the size of the array\xa0\n 748 01:23:11,520 --> 01:23:16,640 a null character. So it only prints on the\xa0\n 749 01:23:16,640 --> 01:23:22,960 not just the print function, there are a bunch\xa0\n 750 01:23:22,960 --> 01:23:27,600 that gives us a bunch of function for string\xa0\n 751 01:23:27,600 --> 01:23:34,160 that the string will be null terminated. So this\xa0\n 752 01:23:34,159 --> 01:23:40,960 say we want to use one function str le n string\xa0\n 753 01:23:41,520 --> 01:23:47,520 I'll print something like this, let's run this and\xa0\n 754 01:23:47,520 --> 01:23:54,720 to four even though the array is of size 20. So\xa0\n 755 01:23:54,720 --> 01:24:02,240 null character. In our program, instead of writing\xa0\n 756 01:24:02,239 --> 01:24:10,079 we could have used string literals string literals\xa0\n 757 01:24:10,079 --> 01:24:16,159 marks. So we could have written something like\xa0\n 758 01:24:16,159 --> 01:24:24,079 the string and the null termination for a string\xa0\n 759 01:24:24,079 --> 01:24:30,000 null termination and memory. Remember, when we\xa0\n 760 01:24:30,000 --> 01:24:35,439 this, this has to happen in one line, we cannot\xa0\n 761 01:24:35,439 --> 01:24:41,199 we go on and modify this in the next line this\xa0\n 762 01:24:41,199 --> 01:24:47,760 this size here, and this would have been fine.\xa0\n 763 01:24:48,319 --> 01:24:54,399 five bytes where one byte stores one character.\xa0\n 764 01:24:55,119 --> 01:25:03,119 john. So if we try to print the size in bytes of\xa0\n 765 01:25:03,119 --> 01:25:08,720 is five one character stored in one byte. So space\xa0\n 766 01:25:08,720 --> 01:25:14,560 length is four, because the null character does\xa0\n 767 01:25:14,560 --> 01:25:20,560 like c four is equal to this particular string\xa0\n 768 01:25:20,560 --> 01:25:28,160 compilation error, because the compiler will force\xa0\n 769 01:25:28,159 --> 01:25:35,439 there is one more syntax of initialization we can\xa0\n 770 01:25:36,479 --> 01:25:42,239 within these braces, but in this case,\xa0\n 771 01:25:42,239 --> 01:25:47,359 we will have to do it explicitly. And the size\xa0\n 772 01:25:47,359 --> 01:25:51,519 to the number of characters here, so it should be\xa0\n 773 01:25:51,520 --> 01:25:56,720 some of the ways to initialize a character array.\xa0\n 774 01:25:57,520 --> 01:26:03,280 arrays and pointers are different types that are\xa0\n 775 01:26:03,279 --> 01:26:08,800 mean? Well, let's say we declare a character\xa0\n 776 01:26:08,800 --> 01:26:14,000 with this string literal. And let's say this is\xa0\n 777 01:26:14,000 --> 01:26:19,680 in one contiguous block of memory. So let's say\xa0\n 778 01:26:19,680 --> 01:26:25,119 is of size one bytes, so the next character will\xa0\n 779 01:26:25,119 --> 01:26:32,640 and so on. Now c one is the variable name for this\xa0\n 780 01:26:32,640 --> 01:26:38,400 variable, which is a pointer to character, let's\xa0\n 781 01:26:38,399 --> 01:26:44,159 c two somewhere in the memory, a pointer variable\xa0\n 782 01:26:44,159 --> 01:26:48,559 So this variable will also have some address,\xa0\n 783 01:26:48,560 --> 01:26:53,039 I'm just picking up these numbers for example\xa0\n 784 01:26:53,039 --> 01:26:59,199 statement like this C two is equal to c one.\xa0\n 785 01:26:59,199 --> 01:27:03,920 a variable name for character array. And c two\xa0\n 786 01:27:03,920 --> 01:27:10,480 this particular expression is valid, just using\xa0\n 787 01:27:10,479 --> 01:27:16,319 of the first element in the array. So what this\xa0\n 788 01:27:16,319 --> 01:27:22,639 it will fill in the address 200 and C two. So C\xa0\n 789 01:27:23,920 --> 01:27:30,480 Now, we can use this variable c two, which is a\xa0\n 790 01:27:30,479 --> 01:27:37,679 write into the array. So if I write something like\xa0\n 791 01:27:38,239 --> 01:27:45,439 one, then the output will be L. and we can even\xa0\n 792 01:27:45,439 --> 01:27:52,639 the character at zero at index two A. So the\xa0\n 793 01:27:53,279 --> 01:28:00,719 when we write C two I for any position I it is\xa0\n 794 01:28:00,720 --> 01:28:06,560 the base address, C two plus, I will take you\xa0\n 795 01:28:06,560 --> 01:28:12,640 case, let's say C two plus two will be 202. And\xa0\n 796 01:28:12,640 --> 01:28:18,720 dereferencing and finding out the value, so these\xa0\n 797 01:28:18,720 --> 01:28:25,680 it is an array name, we can write on these two as\xa0\n 798 01:28:25,680 --> 01:28:34,560 similarity in how we actually use them use arrays\xa0\n 799 01:28:34,560 --> 01:28:42,320 the differences. We cannot modify an array with a\xa0\n 800 01:28:42,319 --> 01:28:48,479 but c one equals c two is not valid, it does\xa0\n 801 01:28:48,479 --> 01:28:53,839 error, we cannot even say C one is equal to c\xa0\n 802 01:28:54,479 --> 01:28:59,439 This video we'll see what we can do it for C two,\xa0\n 803 01:28:59,439 --> 01:29:05,119 plus so C two now points to the next element.\xa0\n 804 01:29:05,119 --> 01:29:12,159 is C two will become 201. So instead of pointing\xa0\n 805 01:29:12,159 --> 01:29:18,239 e to traverse an array, we run a loop and we\xa0\n 806 01:29:18,239 --> 01:29:22,159 it in the loop if we have a pointer variable,\xa0\n 807 01:29:22,720 --> 01:29:28,159 and we can traverse the list we can traverse the\xa0\n 808 01:29:28,159 --> 01:29:32,960 So we must understand where we have an array and\xa0\n 809 01:29:32,960 --> 01:29:41,439 which one. Now, the next thing that we need to\xa0\n 810 01:29:41,439 --> 01:29:47,279 functions by reference or when we pass arrays\xa0\n 811 01:29:47,279 --> 01:29:53,599 of the array in a pointer variable and we do not\xa0\n 812 01:29:53,600 --> 01:29:57,920 not let you do do that. And we have talked\xa0\n 813 01:29:58,560 --> 01:30:03,680 Arrays as function arguments. Let us look through\xa0\n 814 01:30:04,960 --> 01:30:11,920 In my code, I have declared a character array of\xa0\n 815 01:30:11,920 --> 01:30:17,359 in this character array. The null termination\xa0\n 816 01:30:17,359 --> 01:30:22,960 Now I want to print this string, but I do\xa0\n 817 01:30:22,960 --> 01:30:29,680 I want to write my own function print, I want to\xa0\n 818 01:30:29,680 --> 01:30:35,119 should print the string part in a character\xa0\n 819 01:30:35,119 --> 01:30:40,079 talked about this earlier can be something like\xa0\n 820 01:30:40,720 --> 01:30:48,480 but the compiler actually interprets this as\xa0\n 821 01:30:48,479 --> 01:30:55,439 of the character array or the address of any array\xa0\n 822 01:30:55,439 --> 01:31:02,639 arrays are large in size, so it is inefficient to\xa0\n 823 01:31:02,640 --> 01:31:07,760 Okay, so let's write the logic now. Now, this\xa0\n 824 01:31:07,760 --> 01:31:13,600 particular array is of size 20, it only knows\xa0\n 825 01:31:13,600 --> 01:31:20,880 have a variable I initialize it to zero and we can\xa0\n 826 01:31:22,000 --> 01:31:25,520 null character, we can print the character ci,\xa0\xa0 827 01:31:27,119 --> 01:31:33,119 and then we will increment i. And when we find\xa0\n 828 01:31:33,119 --> 01:31:39,599 and print an end of line. Once again, this print\xa0\n 829 01:31:39,600 --> 01:31:44,640 of size 20, it only knows the base address.\xa0\n 830 01:31:45,279 --> 01:31:50,800 when we were not null terminating an array, we\xa0\n 831 01:31:50,800 --> 01:31:56,720 array was finished. Because until we get a null\xa0\n 832 01:31:56,720 --> 01:32:03,280 keep on going into unwanted memory locations.\xa0\n 833 01:32:04,479 --> 01:32:08,799 In my code, here, I'm using percent and see\xa0\n 834 01:32:09,520 --> 01:32:16,480 and see if we can also write as strict c plus II\xa0\n 835 01:32:16,479 --> 01:32:23,439 same. Sometimes you'll see this kind of syntax. As\xa0\n 836 01:32:23,439 --> 01:32:29,279 plus imci are valid, they are the same, there is\xa0\n 837 01:32:29,279 --> 01:32:37,119 not choose to have this particular variable i,\xa0\n 838 01:32:37,119 --> 01:32:45,439 equal to null, print, Asterix C and then increment\xa0\n 839 01:32:45,439 --> 01:32:50,079 code will work. I leave it as an exercise for you\xa0\n 840 01:32:50,079 --> 01:32:56,880 more functions for string manipulation. This is it\xa0\n 841 01:32:56,880 --> 01:33:03,520 lesson on character arrays and pointers, we saw\xa0\n 842 01:33:03,520 --> 01:33:08,480 we also saw how we can work with character arrays\xa0\n 843 01:33:09,359 --> 01:33:14,000 Whenever we are working with pointers, it's\xa0\n 844 01:33:14,000 --> 01:33:19,359 happening in the memory. So, the first thing that\xa0\n 845 01:33:19,359 --> 01:33:24,319 program that we had written in the previous\xa0\n 846 01:33:25,119 --> 01:33:32,319 and see what goes where in the memory. And we will\xa0\n 847 01:33:32,319 --> 01:33:37,279 and constant pointers. So, this is where\xa0\n 848 01:33:37,920 --> 01:33:43,359 we have written a function Print to print the\xa0\n 849 01:33:43,359 --> 01:33:48,880 we have a character array of size 20, but we\xa0\n 850 01:33:49,520 --> 01:33:56,160 we have used a string literal, so null termination\xa0\n 851 01:33:56,159 --> 01:34:02,399 So, let us step through this code and see how it\xa0\n 852 01:34:02,399 --> 01:34:08,879 diagram the memory that is allocated for execution\xa0\n 853 01:34:08,880 --> 01:34:14,880 four parts these four sections on one part of the\xa0\n 854 01:34:14,880 --> 01:34:19,920 we call that the code segment or the text\xa0\n 855 01:34:20,479 --> 01:34:26,559 global variables and stack is where all the\xa0\n 856 01:34:26,560 --> 01:34:32,000 all the local variables go whenever we are writing\xa0\n 857 01:34:32,000 --> 01:34:39,920 should always visualize what variable goes where\xa0\n 858 01:34:39,920 --> 01:34:45,840 that variable or data okay. So, let us run through\xa0\n 859 01:34:46,800 --> 01:34:52,480 When this program will start executing first\xa0\n 860 01:34:53,279 --> 01:34:59,039 whenever a function is called some amount\xa0\n 861 01:34:59,039 --> 01:35:05,119 the execution. Attack function, it's called the\xa0\n 862 01:35:06,000 --> 01:35:12,640 this stack frame from address 100 250 is\xa0\n 863 01:35:12,640 --> 01:35:19,600 one contiguous block of memory. So let us say in\xa0\n 864 01:35:19,600 --> 01:35:26,000 all the local variables of function go into the\xa0\n 865 01:35:26,000 --> 01:35:31,359 character array, or 20 bytes from the stack frame\xa0\n 866 01:35:31,359 --> 01:35:38,719 array. Let's say they're allocated from address\xa0\n 867 01:35:38,720 --> 01:35:44,880 so we need 20 bytes for character array of size\xa0\n 868 01:35:44,880 --> 01:35:50,159 some more information and stack frame. So that's\xa0\n 869 01:35:50,159 --> 01:35:55,199 of the program goes to the statement print\xa0\n 870 01:35:55,199 --> 01:36:00,800 function from a function, the execution of that\xa0\n 871 01:36:00,800 --> 01:36:06,640 line and the machine goes on to execute the\xa0\n 872 01:36:06,640 --> 01:36:12,880 stack frame on top of the calling function. So\xa0\n 873 01:36:12,880 --> 01:36:18,720 main function, whatever function is at the top\xa0\n 874 01:36:18,720 --> 01:36:24,880 will wait for this function to finish let's say\xa0\n 875 01:36:26,800 --> 01:36:32,800 Main is paused and print is executing right now.\xa0\n 876 01:36:32,800 --> 01:36:37,920 but this will be a pointer variable.\xa0\n 877 01:36:37,920 --> 01:36:44,560 memory in a typical architecture. So this will be\xa0\n 878 01:36:44,560 --> 01:36:50,240 a 154. In this stack frame, we have four bytes\xa0\n 879 01:36:50,239 --> 01:36:57,359 in main is actually not the same C in print,\xa0\n 880 01:36:57,359 --> 01:37:03,599 They have different scopes, or we could have\xa0\n 881 01:37:03,600 --> 01:37:10,240 whatever. But all that happens when we make this\xa0\n 882 01:37:10,880 --> 01:37:16,480 is that the address 100 which is the base address\xa0\n 883 01:37:16,479 --> 01:37:22,479 the print function keeps it stores it in a\xa0\n 884 01:37:22,479 --> 01:37:27,439 us when we are using the same local variable\xa0\n 885 01:37:28,079 --> 01:37:32,720 argument name in the called function. But we\xa0\n 886 01:37:32,720 --> 01:37:40,000 Okay to run through this code further. I'll clear\xa0\n 887 01:37:40,000 --> 01:37:47,279 outside the stack here we have an array of\xa0\n 888 01:37:47,279 --> 01:37:53,679 six positions in the array from address 100 205.\xa0\n 889 01:37:54,239 --> 01:38:00,399 The sixth character is a null character and the\xa0\n 890 01:38:00,399 --> 01:38:06,559 hello. Now we have this another guy which is\xa0\n 891 01:38:06,560 --> 01:38:14,400 address 154 that stores address 100 so it points\xa0\n 892 01:38:14,399 --> 01:38:20,639 come back to our program execution. Let's say we\xa0\n 893 01:38:20,640 --> 01:38:26,000 is executing by this green arrow or Let's\xa0\n 894 01:38:26,000 --> 01:38:32,079 is an array, local domain and this is also c\xa0\n 895 01:38:32,640 --> 01:38:38,400 Okay, so now here what we're saying is, while\xa0\n 896 01:38:38,399 --> 01:38:44,559 when we put this asterik operator in front of a\xa0\n 897 01:38:44,560 --> 01:38:52,480 that particular address. So at this stage, when\xa0\n 898 01:38:52,479 --> 01:38:59,039 so this condition is not true, we will go to\xa0\n 899 01:38:59,039 --> 01:39:08,800 which is h let's write down the output here. So we\xa0\n 900 01:39:08,800 --> 01:39:14,400 understand pointer arithmetic from our previous\xa0\n 901 01:39:14,399 --> 01:39:20,639 increments the address by size of the data\xa0\n 902 01:39:20,640 --> 01:39:27,760 pointer to character data type and character data\xa0\n 903 01:39:27,760 --> 01:39:35,760 C is equal to C plus one. So C now becomes 101. So\xa0\n 904 01:39:36,319 --> 01:39:42,880 And once again we come to verifying this condition\xa0\n 905 01:39:42,880 --> 01:39:48,319 null. Now once again we will go inside the loop\xa0\n 906 01:39:50,800 --> 01:39:56,960 builder address in this pointer variable\xa0\n 907 01:39:56,960 --> 01:40:02,319 particular addresses null character so the\xa0\n 908 01:40:02,319 --> 01:40:08,639 this statement to print and end offline and\xa0\n 909 01:40:09,680 --> 01:40:16,159 So this particular stack frame for print will\xa0\n 910 01:40:16,159 --> 01:40:22,000 And now main will resume and finished. So with\xa0\n 911 01:40:22,000 --> 01:40:29,600 in the memory. Okay, so let us now modify this\xa0\n 912 01:40:29,600 --> 01:40:36,320 Or what I'm going to do in my code is, instead\xa0\n 913 01:40:36,319 --> 01:40:45,039 I'll create a character pointer named C and\xa0\n 914 01:40:45,039 --> 01:40:51,680 like this. And if you run this program on the\xa0\n 915 01:40:51,680 --> 01:40:57,760 if you use the string literal initializations\xa0\n 916 01:40:57,760 --> 01:41:02,159 and then the string get stored in the space\xa0\n 917 01:41:02,159 --> 01:41:08,800 it will go into the stack in this character array\xa0\n 918 01:41:08,800 --> 01:41:16,720 elsewhere in a statement like this, or maybe when\xa0\n 919 01:41:16,720 --> 01:41:25,119 to a function, then in these cases, the string\xa0\n 920 01:41:25,680 --> 01:41:30,800 most probably it will be stored in the\xa0\n 921 01:41:30,800 --> 01:41:34,880 it cannot be modified. So if you write a\xa0\n 922 01:41:34,880 --> 01:41:40,640 change the first character of this constant\xa0\n 923 01:41:40,640 --> 01:41:48,400 give you an error will cause a crash. Okay,\xa0\n 924 01:41:48,399 --> 01:41:53,599 if we have a character array, and we are passing\xa0\n 925 01:41:54,399 --> 01:41:59,839 that function receives it in a character\xa0\n 926 01:42:01,359 --> 01:42:06,960 the data in this particular array. So if I say\xa0\n 927 01:42:06,960 --> 01:42:10,800 character to a and then we are printing\xa0\n 928 01:42:11,359 --> 01:42:17,679 it is possible to do so. Now sometimes we may want\xa0\n 929 01:42:17,680 --> 01:42:24,800 anything. to force this kind of behavior, we can\xa0\n 930 01:42:25,439 --> 01:42:30,159 Now if we run this code, this code will give us\xa0\n 931 01:42:30,159 --> 01:42:36,479 there in the array that is passed. But we cannot\xa0\n 932 01:42:36,479 --> 01:42:42,799 will work fine. pointers are really tricky,\xa0\n 933 01:42:43,359 --> 01:42:48,799 write buggy code when they're using pointers,\xa0\n 934 01:42:48,800 --> 01:42:54,320 writing some code yourself. That's the best way to\xa0\n 935 01:42:54,319 --> 01:43:01,279 watching. In this lesson, we are going to talk\xa0\n 936 01:43:01,279 --> 01:43:07,039 As we have seen in our previous lessons, the\xa0\n 937 01:43:07,039 --> 01:43:12,411 have already talked about pointers in context of\xa0\n 938 01:43:12,411 --> 01:43:17,198 dimensional arrays using pointers. Now let's\xa0\n 939 01:43:17,199 --> 01:43:23,199 array or a three dimensional array, or a multi\xa0\n 940 01:43:23,199 --> 01:43:28,800 To understand this concept, once again, we first\xa0\n 941 01:43:28,800 --> 01:43:34,720 are organized in computer's memory. Let's first go\xa0\n 942 01:43:34,720 --> 01:43:40,400 in memory. When we declare a one dimensional array\xa0\n 943 01:43:40,399 --> 01:43:46,799 an integer array A of five elements, then\xa0\n 944 01:43:47,760 --> 01:43:55,199 five different integer variables that we can name\xa0\n 945 01:43:55,199 --> 01:44:02,239 contiguous block of memory. What I'm showing here\xa0\n 946 01:44:02,239 --> 01:44:09,199 is stored in this section of memory, and the\xa0\n 947 01:44:09,199 --> 01:44:16,479 byte in a computer's memory has an address. And if\xa0\n 948 01:44:16,479 --> 01:44:23,519 which is what it takes, in a typical compiler,\xa0\n 949 01:44:23,520 --> 01:44:30,720 will be a zero, as I'm showing here, block of four\xa0\n 950 01:44:30,720 --> 01:44:38,720 block of code byte starting address 208 will be\xa0\n 951 01:44:38,720 --> 01:44:42,960 We had seen this earlier also in our previous\xa0\n 952 01:44:43,680 --> 01:44:51,200 then this is all right. If I just use the variable\xa0\n 953 01:44:51,840 --> 01:44:57,039 basically returns a pointer to the first\xa0\n 954 01:44:57,039 --> 01:45:02,640 array of integers. So each element will be an\xa0\n 955 01:45:03,600 --> 01:45:09,760 Once I have written a statement like int Asterix\xa0\n 956 01:45:09,760 --> 01:45:15,119 dereferencing to access all the elements in\xa0\n 957 01:45:15,119 --> 01:45:20,800 address stored in P and I'm not using full\xa0\n 958 01:45:21,520 --> 01:45:27,040 if I would simply try to dereference\xa0\n 959 01:45:27,039 --> 01:45:32,079 try to print something like Asterix\xa0\n 960 01:45:33,199 --> 01:45:39,840 because we have an integer pointer adding one\xa0\n 961 01:45:39,840 --> 01:45:45,600 which will be four bytes ahead adding to will take\xa0\n 962 01:45:45,600 --> 01:45:53,120 eight bytes ahead if we would be printing p plus\xa0\n 963 01:45:53,119 --> 01:45:58,800 and we would print Asterix p plus two then it\xa0\n 964 01:45:58,800 --> 01:46:04,320 pointer arithmetic in our previous lessons. Now\xa0\n 965 01:46:05,039 --> 01:46:11,519 the name of the array just like a pointer\xa0\n 966 01:46:11,520 --> 01:46:17,600 doing all of this printing with P if we will do\xa0\n 967 01:46:18,800 --> 01:46:28,159 In fact Asterix a plus i is same as AI these are\xa0\n 968 01:46:28,159 --> 01:46:33,840 AI both will give us the address of the ayat\xa0\n 969 01:46:33,840 --> 01:46:40,159 though we can use the name of the array just like\xa0\n 970 01:46:40,159 --> 01:46:46,159 it's not seen as a pointer variable. So\xa0\n 971 01:46:46,159 --> 01:46:51,840 what we have done here. So this is all right.\xa0\n 972 01:46:51,840 --> 01:46:59,039 equal p this will give you a compilation error.\xa0\n 973 01:46:59,039 --> 01:47:03,519 with one dimensional arrays. Let's now say\xa0\n 974 01:47:04,159 --> 01:47:09,599 I will declare a two dimensional array\xa0\n 975 01:47:11,359 --> 01:47:17,371 Now, what we are doing here is we are creating\xa0\n 976 01:47:17,371 --> 01:47:25,840 dimensional arrays of three elements each this\xa0\n 977 01:47:25,840 --> 01:47:33,279 arrays of three integers each. a one dimensional\xa0\n 978 01:47:33,279 --> 01:47:40,559 integer is four bytes in size. So if I have to\xa0\n 979 01:47:40,560 --> 01:47:52,000 starting address 400 will be B zero. And the next\xa0\n 980 01:47:53,119 --> 01:48:00,800 As we had said name of the array returns a pointer\xa0\n 981 01:48:00,800 --> 01:48:08,239 element is not an integer, each element is a one\xa0\n 982 01:48:08,239 --> 01:48:14,800 write a statement like this int Asterix p equals\xa0\n 983 01:48:14,800 --> 01:48:20,880 because b will return a pointer to a 1d array of\xa0\n 984 01:48:21,439 --> 01:48:28,159 the type of a pointer matters not when you have to\xa0\n 985 01:48:28,720 --> 01:48:33,920 or when you perform pointer arithmetic. It's\xa0\n 986 01:48:33,920 --> 01:48:42,480 define a pointer to a 1d array of three integers\xa0\n 987 01:48:42,479 --> 01:48:51,519 this is all right. equate B with B this time if\xa0\n 988 01:48:51,520 --> 01:48:59,840 address of B zero, this will be 400 if I\xa0\n 989 01:49:00,560 --> 01:49:07,200 B zero, then B zero this time is variable name\xa0\n 990 01:49:07,840 --> 01:49:14,319 So just using the name B zero will return us a\xa0\n 991 01:49:14,319 --> 01:49:20,319 will be accessed as p 00. So I'm putting\xa0\n 992 01:49:21,199 --> 01:49:27,679 First integer in B zero will be this block\xa0\n 993 01:49:29,600 --> 01:49:36,560 Okay, now I have also created three blocks of\xa0\n 994 01:49:36,560 --> 01:49:41,440 also filled in some values. Now I'm going to\xa0\n 995 01:49:42,000 --> 01:49:49,359 to guess the output. What will be the output\xa0\n 996 01:49:49,359 --> 01:49:54,719 the address of each block of four bytes\xa0\n 997 01:49:55,359 --> 01:50:00,719 is the address of the first byte in the block.\xa0\n 998 01:50:01,760 --> 01:50:06,960 B this time is returning us a pointer to\xa0\n 999 01:50:07,600 --> 01:50:13,360 So, if I would do a pointer arithmetic like adding\xa0\n 1000 01:50:13,359 --> 01:50:18,559 of three integers. So, we will be moving to\xa0\n 1001 01:50:18,560 --> 01:50:27,039 of three integers in bytes. So, output will be 412\xa0\n 1002 01:50:27,039 --> 01:50:37,199 ampersand B one or address of B one and address\xa0\n 1003 01:50:37,199 --> 01:50:43,039 B plus one when we are putting an Asterix signs\xa0\n 1004 01:50:43,039 --> 01:50:48,880 type of pointer becomes important B is a pointer\xa0\n 1005 01:50:48,880 --> 01:50:53,786 B plus one is also a pointer to one dimensional\xa0\n 1006 01:50:53,786 --> 01:50:58,560 dereference we will get this whole one dimensional\xa0\n 1007 01:50:59,920 --> 01:51:06,319 Asterix B plus one is same as B one. So, we will\xa0\n 1008 01:51:06,880 --> 01:51:13,440 name of this one dimensional array B one which\xa0\n 1009 01:51:13,439 --> 01:51:20,799 integer in B one. So, astrick b plus one is\xa0\n 1010 01:51:21,680 --> 01:51:28,800 Once again this output will be 412 all these\xa0\n 1011 01:51:28,800 --> 01:51:36,159 ampersand v one, zero are returning us pointer\xa0\n 1012 01:51:36,159 --> 01:51:42,319 I'm going to give you some tough ones to decode\xa0\n 1013 01:51:42,319 --> 01:51:50,479 B plus one plus two, take some time and think\xa0\n 1014 01:51:50,479 --> 01:51:58,319 will return an integer pointer to first integer\xa0\n 1015 01:51:58,319 --> 01:52:04,000 to here is performing pointer arithmetic because\xa0\n 1016 01:52:04,560 --> 01:52:12,480 storing this address 412 adding to is basically\xa0\n 1017 01:52:13,119 --> 01:52:21,039 which will mean skipping eight bytes and and going\xa0\n 1018 01:52:21,039 --> 01:52:26,640 plus one in this expression can be written as b\xa0\n 1019 01:52:26,640 --> 01:52:32,240 for each other their alternate syntax. So this\xa0\n 1020 01:52:33,199 --> 01:52:38,559 these expressions are returning pointer to\xa0\n 1021 01:52:38,560 --> 01:52:44,960 B one two. That's why we can also say ampersand\xa0\n 1022 01:52:45,760 --> 01:52:51,840 Let's do one more print. If you can get this one\xa0\n 1023 01:52:51,840 --> 01:52:58,960 dimensional arrays using pointers what will be the\xa0\n 1024 01:52:59,680 --> 01:53:07,039 Asterix of Asterix B plus one what will be the\xa0\n 1025 01:53:07,039 --> 01:53:13,600 whenever you encounter an expression with pointer\xa0\n 1026 01:53:13,600 --> 01:53:21,760 by step here B is returning us a pointer to one\xa0\n 1027 01:53:21,760 --> 01:53:28,720 array of three integers and dereferencing it will\xa0\n 1028 01:53:29,439 --> 01:53:36,239 as we know will give us B zero asteroid B is\xa0\n 1029 01:53:36,800 --> 01:53:43,119 because B's row is named for a one dimensional\xa0\n 1030 01:53:43,840 --> 01:53:49,440 in the one dimensional array. So b zero returns a\xa0\n 1031 01:53:49,439 --> 01:53:56,319 integer at address 400. Now, what will happen if\xa0\n 1032 01:53:56,319 --> 01:54:02,559 take you four bytes ahead to the next integer it\xa0\n 1033 01:54:02,560 --> 01:54:10,720 we will get a pointer to this integer at address\xa0\n 1034 01:54:10,720 --> 01:54:17,920 01. And with this final dereferencing we can get\xa0\n 1035 01:54:19,119 --> 01:54:28,079 will be p 01 which is three for a two\xa0\n 1036 01:54:28,079 --> 01:54:35,840 for my two dimensional array and i and j are\xa0\n 1037 01:54:36,640 --> 01:54:43,119 plus j and once again p AI can be\xa0\n 1038 01:54:44,000 --> 01:54:50,159 So these three expressions are same. I would\xa0\n 1039 01:54:50,159 --> 01:54:54,880 So far this discussion has been about working\xa0\n 1040 01:54:54,880 --> 01:55:00,960 will stop here for this lesson. In another lesson\xa0\n 1041 01:55:00,960 --> 01:55:06,880 discuss how we can work with an array of further\xa0\n 1042 01:55:06,880 --> 01:55:11,920 We will also discuss passing of arrays\xa0\n 1043 01:55:11,920 --> 01:55:17,199 it for this lesson. Thanks for watching. In\xa0\n 1044 01:55:17,199 --> 01:55:22,079 with two dimensional arrays using pointers.\xa0\n 1045 01:55:22,079 --> 01:55:27,680 work with arrays of further higher dimensions\xa0\n 1046 01:55:28,560 --> 01:55:34,640 We will also see how we can pass multi dimensional\xa0\n 1047 01:55:35,439 --> 01:55:40,239 one scenario where pointers once again will come\xa0\n 1048 01:55:40,239 --> 01:55:45,279 what we have discussed in our previous lesson.\xa0\n 1049 01:55:45,279 --> 01:55:49,439 and let's pick up the example of two dimensional\xa0\n 1050 01:55:50,000 --> 01:55:57,279 we must think of the multi dimensional array as an\xa0\n 1051 01:55:57,279 --> 01:56:04,000 of similar things of similar objects. So a multi\xa0\n 1052 01:56:04,000 --> 01:56:12,399 arrays. This array B here is a collection of one\xa0\n 1053 01:56:12,399 --> 01:56:18,319 to one dimensional arrays of three elements each,\xa0\n 1054 01:56:18,319 --> 01:56:24,319 array B will be organized in memory, I have\xa0\n 1055 01:56:24,319 --> 01:56:31,679 400. Each cell storing and teacher here is a block\xa0\n 1056 01:56:31,680 --> 01:56:36,560 memory has an address, I'm not drawing all the\xa0\n 1057 01:56:37,439 --> 01:56:42,719 blocks of four bytes each. And that's why I'm\xa0\n 1058 01:56:42,720 --> 01:56:48,800 you can imagine a block of four bytes something\xa0\n 1059 01:56:48,800 --> 01:56:57,279 each partition here is one byte. So the next byte\xa0\n 1060 01:56:57,279 --> 01:57:05,039 And the next one has address 403. Overall, this\xa0\n 1061 01:57:05,039 --> 01:57:11,439 these three integers two, three, and six that I'm\xa0\n 1062 01:57:11,439 --> 01:57:18,239 array that I can call B zero. And this next block\xa0\n 1063 01:57:18,239 --> 01:57:25,119 three integers that I can call B one. So we have\xa0\n 1064 01:57:25,119 --> 01:57:30,800 we have two collections of three integers each\xa0\n 1065 01:57:30,800 --> 01:57:40,320 in one contiguous block of memory. Now let's look\xa0\n 1066 01:57:40,319 --> 01:57:45,679 we have three integers and we have four bytes\xa0\n 1067 01:57:45,680 --> 01:57:54,159 access as element at zero at index of B zero is\xa0\n 1068 01:57:54,159 --> 01:58:03,680 integer that can be accessed as p 011 element\xa0\n 1069 01:58:04,319 --> 01:58:12,079 and the next one will be v 02 element at index\xa0\n 1070 01:58:12,079 --> 01:58:20,000 bytes for zeroeth element of v one four bytes for\xa0\n 1071 01:58:20,000 --> 01:58:26,079 two of B one as we had seen in our previous\xa0\n 1072 01:58:26,079 --> 01:58:33,920 then it returns us a pointer to the first element\xa0\n 1073 01:58:33,920 --> 01:58:41,920 it is an array of one dimensional arrays of size\xa0\n 1074 01:58:41,920 --> 01:58:48,159 one dimensional array of three elements pointer\xa0\n 1075 01:58:48,159 --> 01:58:53,359 this statement, I have declared a variable which\xa0\n 1076 01:58:53,359 --> 01:58:59,279 integers and the name of the variable is be a\xa0\n 1077 01:58:59,279 --> 01:59:05,199 this will not be alright because b will not return\xa0\n 1078 01:59:05,199 --> 01:59:10,479 one dimensional array of three integers. Now\xa0\n 1079 01:59:10,479 --> 01:59:15,519 also written in our previous lesson. I'm not\xa0\n 1080 01:59:15,520 --> 01:59:21,840 so once again you need to tell me what will be the\xa0\n 1081 01:59:21,840 --> 01:59:30,319 we have p Asterix B and B zero. Well for all\xa0\n 1082 01:59:30,319 --> 01:59:38,479 say B just using the array name B will return us\xa0\n 1083 01:59:38,479 --> 01:59:43,759 the type of a pointer variable is relevant\xa0\n 1084 01:59:43,760 --> 01:59:49,360 perform pointer arithmetic. But if we will\xa0\n 1085 01:59:49,359 --> 01:59:56,159 pointer variable it will be the starting address\xa0\n 1086 01:59:56,159 --> 02:00:00,000 So if we have a pointer to this one dimensional\xa0\n 1087 02:00:00,880 --> 02:00:08,279 then it's addresses 400. Now when we did an\xa0\n 1088 02:00:08,279 --> 02:00:14,719 friends and now we have got the complete\xa0\n 1089 02:00:14,720 --> 02:00:20,960 B zero because b zero is a one dimensional array,\xa0\n 1090 02:00:20,960 --> 02:00:28,480 B zero, so we will get a pointer to bs 00 in\xa0\n 1091 02:00:28,479 --> 02:00:34,559 Once again if we would just print the address,\xa0\n 1092 02:00:35,199 --> 02:00:43,599 That's why the output here is 400. Even if I would\xa0\n 1093 02:00:43,600 --> 02:00:49,360 and Asterix B are both returning us pointers, the\xa0\n 1094 02:00:49,359 --> 02:00:56,399 one dimensional array of three integers while\xa0\n 1095 02:00:56,399 --> 02:01:01,439 when we are just printing the address both\xa0\n 1096 02:01:01,439 --> 02:01:06,960 array B zero and the first element in B zero have\xa0\n 1097 02:01:06,960 --> 02:01:11,520 address will be printed the type of pointer\xa0\n 1098 02:01:11,520 --> 02:01:16,800 when you try to perform pointer arithmetic.\xa0\n 1099 02:01:17,520 --> 02:01:27,760 B IJ can be written as Asterix of B i plus j if b\xa0\n 1100 02:01:27,760 --> 02:01:33,680 then b i will give us an integer pointer will\xa0\n 1101 02:01:33,680 --> 02:01:39,920 in bi then adding j is basically performing\xa0\n 1102 02:01:39,920 --> 02:01:47,600 integer at index j in one dimensional array bi\xa0\n 1103 02:01:47,600 --> 02:01:56,000 the value of that integer. Once again be\xa0\n 1104 02:01:56,000 --> 02:02:01,359 is a pointer to one dimensional array of three\xa0\n 1105 02:02:01,359 --> 02:02:06,719 to one dimensional array of three integers and\xa0\n 1106 02:02:06,720 --> 02:02:12,800 give us the one dimensional array and the name\xa0\n 1107 02:02:12,800 --> 02:02:18,960 the first element in the array. So this once again\xa0\n 1108 02:02:18,960 --> 02:02:24,720 understand how all the pointer arithmetic and\xa0\n 1109 02:02:25,279 --> 02:02:32,159 then it's not very difficult to understand how\xa0\n 1110 02:02:32,159 --> 02:02:39,199 Now let's say we have created a three dimensional\xa0\n 1111 02:02:39,199 --> 02:02:45,840 cross two or three dimensional array is basically\xa0\n 1112 02:02:46,399 --> 02:02:50,719 So if I have to show C in memory,\xa0\n 1113 02:02:51,680 --> 02:02:58,640 I have assumed that the starting address of C is\xa0\n 1114 02:02:58,640 --> 02:03:05,600 address 800 is my first two dimensional array,\xa0\n 1115 02:03:05,600 --> 02:03:11,360 bytes. So all these cells in yellow are\xa0\n 1116 02:03:11,359 --> 02:03:19,920 The next block of 16 bytes starting address\xa0\n 1117 02:03:19,920 --> 02:03:26,800 starting a 32 is C two, we can further break down\xa0\n 1118 02:03:26,800 --> 02:03:33,760 arrays. The first two integers in c zero are\xa0\n 1119 02:03:34,479 --> 02:03:45,279 And seven and nine are part of C 01. The first\xa0\n 1120 02:03:45,279 --> 02:03:52,159 can go on like this. Okay, once again, we'll play\xa0\n 1121 02:03:52,159 --> 02:04:00,479 and you need to guess the output. This time just\xa0\n 1122 02:04:01,039 --> 02:04:08,000 a two dimensional array of integers of size two\xa0\n 1123 02:04:08,000 --> 02:04:14,800 I have declared a pointer to a two dimensional\xa0\n 1124 02:04:14,800 --> 02:04:21,360 name of the point that is B if I would just print\xa0\n 1125 02:04:21,359 --> 02:04:28,799 printf statement once again my output will be 800.\xa0\n 1126 02:04:29,359 --> 02:04:37,119 print Asterix C then this will be seen as c\xa0\n 1127 02:04:37,119 --> 02:04:43,920 So we will get a pointer to the first element\xa0\n 1128 02:04:43,920 --> 02:04:52,960 us a pointer to one dimensional array of integers\xa0\n 1129 02:04:52,960 --> 02:05:02,000 800. Remember C is of type pointer to 2d array\xa0\n 1130 02:05:02,000 --> 02:05:08,720 giving us point at a one dimensional array\xa0\n 1131 02:05:08,720 --> 02:05:15,039 two dimensional array of integers c itself is an\xa0\n 1132 02:05:15,840 --> 02:05:26,720 okay for c c ij k where i j and k are some\xa0\n 1133 02:05:26,720 --> 02:05:35,280 K and now once again we can write C ij\xa0\n 1134 02:05:35,279 --> 02:05:43,279 expression will look something like this and we\xa0\n 1135 02:05:44,640 --> 02:05:48,240 if you are able to understand how\xa0\n 1136 02:05:48,239 --> 02:05:53,840 if you are able to understand all the pointer\xa0\n 1137 02:05:54,399 --> 02:05:58,079 then you are good working with multi\xa0\n 1138 02:05:58,800 --> 02:06:04,800 I want a quick answer for this one what\xa0\n 1139 02:06:05,840 --> 02:06:11,920 Well c 01 means we are going to this one\xa0\n 1140 02:06:11,920 --> 02:06:18,960 seven and nine. And when we are using the our\xa0\n 1141 02:06:18,960 --> 02:06:26,000 integer in this one dimensional array pointer\xa0\n 1142 02:06:26,000 --> 02:06:32,720 pointer arithmetic to an integer pointer So, we\xa0\n 1143 02:06:33,520 --> 02:06:41,600 integer nine. In fact, this expression is same as\xa0\n 1144 02:06:41,600 --> 02:06:49,120 print statement aspects of C one plus one c one\xa0\n 1145 02:06:49,119 --> 02:06:55,519 the first one dimensional array in C one,\xa0\n 1146 02:06:55,520 --> 02:07:00,480 adding one is performing pointer arithmetic\xa0\n 1147 02:07:01,119 --> 02:07:07,760 it's going to this one dimensional array\xa0\n 1148 02:07:07,760 --> 02:07:14,159 is basically getting the one dimensional array\xa0\n 1149 02:07:14,159 --> 02:07:19,599 of the one dimensional array, you get a pointer\xa0\n 1150 02:07:20,239 --> 02:07:25,840 So we will get a pointer to this\xa0\n 1151 02:07:27,119 --> 02:07:33,279 This block of four bytes storing address\xa0\n 1152 02:07:33,279 --> 02:07:39,840 next will be a 20 and next will be a 24 we can\xa0\n 1153 02:07:40,800 --> 02:07:45,680 I'm writing this simple C program I have\xa0\n 1154 02:07:46,239 --> 02:07:52,159 the data filled in is same as we were showing\xa0\n 1155 02:07:52,159 --> 02:08:01,279 I'm writing I'm trying to print c astrak, c c zero\xa0\n 1156 02:08:01,279 --> 02:08:08,079 if I will just use person D address would be\xa0\n 1157 02:08:08,079 --> 02:08:15,119 the output for all these four expressions is same.\xa0\n 1158 02:08:15,119 --> 02:08:23,039 printf statement, this expression is nothing but\xa0\n 1159 02:08:23,039 --> 02:08:30,880 because this is a different run of the program and\xa0\n 1160 02:08:30,880 --> 02:08:36,640 whatever the address is, it will be seen for these\xa0\n 1161 02:08:36,640 --> 02:08:43,440 the different expressions that we were decoding\xa0\n 1162 02:08:43,439 --> 02:08:49,839 next thing that I want to talk about is passing\xa0\n 1163 02:08:50,880 --> 02:08:57,600 I'm going to declare a function and\xa0\n 1164 02:08:58,640 --> 02:09:06,240 I want this function to accept a three dimensional\xa0\n 1165 02:09:06,239 --> 02:09:12,319 a one dimensional array as argument I could have\xa0\n 1166 02:09:12,319 --> 02:09:19,679 in one of our previous lessons, this syntax is\xa0\n 1167 02:09:19,680 --> 02:09:26,720 this by the compiler. A fresh copy of array is\xa0\n 1168 02:09:26,720 --> 02:09:33,280 to it in the form of a pointer is created. So\xa0\n 1169 02:09:33,840 --> 02:09:39,440 of one dimensional array like this, and let's\xa0\n 1170 02:09:40,000 --> 02:09:46,079 I can make a function called passing a like\xa0\n 1171 02:09:46,079 --> 02:09:51,760 we declare a two dimensional array of two cross\xa0\n 1172 02:09:52,399 --> 02:10:01,119 to take a two dimensional array as argument. Now\xa0\n 1173 02:10:01,119 --> 02:10:09,039 to in teacher but B will return us a pointer to\xa0\n 1174 02:10:09,039 --> 02:10:16,239 three integers for this particular definition\xa0\n 1175 02:10:16,239 --> 02:10:22,079 p as argument definition should be something\xa0\n 1176 02:10:22,640 --> 02:10:28,720 either we can write this or we can write\xa0\n 1177 02:10:28,720 --> 02:10:35,119 can be left empty the other dimension has to\xa0\n 1178 02:10:35,119 --> 02:10:39,840 something interesting here, if I would declare\xa0\n 1179 02:10:39,840 --> 02:10:48,800 let's declare a two dimensional array x have to\xa0\n 1180 02:10:48,800 --> 02:10:53,440 because x will return pointer to one\xa0\n 1181 02:10:53,439 --> 02:10:58,079 while this function is supposed to receive\xa0\n 1182 02:10:58,640 --> 02:11:04,880 if x is defined something like this of dimension\xa0\n 1183 02:11:05,520 --> 02:11:11,600 Now, if we want to pass this three dimensional\xa0\n 1184 02:11:11,600 --> 02:11:19,440 types he will return. So, C will basically return\xa0\n 1185 02:11:19,439 --> 02:11:26,799 two. So, we can either use a syntax like this\xa0\n 1186 02:11:28,159 --> 02:11:34,239 So, this is how things will be for any multi\xa0\n 1187 02:11:34,239 --> 02:11:40,731 all other dimensions will be enforced. One\xa0\n 1188 02:11:40,730 --> 02:11:45,743 dimensional array, they try to use pointer to\xa0\n 1189 02:11:45,744 --> 02:11:51,920 dimensional array, they try to use something like\xa0\n 1190 02:11:51,920 --> 02:11:57,359 much what we wanted to talk about pointers and\xa0\n 1191 02:11:59,439 --> 02:12:08,239 Memory is one important and crucial resource\xa0\n 1192 02:12:08,239 --> 02:12:14,319 the architecture of memory, the way operating\xa0\n 1193 02:12:14,319 --> 02:12:20,799 accessible to us as programmers. In this lesson,\xa0\n 1194 02:12:20,800 --> 02:12:28,239 and we will see how to work with dynamic memory\xa0\n 1195 02:12:28,239 --> 02:12:35,039 to a program or application in a typical\xa0\n 1196 02:12:36,079 --> 02:12:43,680 one segment of the memory is assigned to store\xa0\n 1197 02:12:44,399 --> 02:12:51,439 Another section stores all the static or global\xa0\n 1198 02:12:51,439 --> 02:12:56,879 inside a function and that have the whole lifetime\xa0\n 1199 02:12:56,880 --> 02:13:02,319 during the whole lifecycle of the application as\xa0\n 1200 02:13:02,319 --> 02:13:09,199 the memory is used to store all the information\xa0\n 1201 02:13:09,760 --> 02:13:17,600 and we have also talked about stack in our lesson\xa0\n 1202 02:13:17,600 --> 02:13:22,000 inside a function and the live only\xa0\n 1203 02:13:22,560 --> 02:13:27,280 the amount of memory set aside for these\xa0\n 1204 02:13:27,279 --> 02:13:32,399 variable segment and the stack does not grow\xa0\n 1205 02:13:32,399 --> 02:13:38,719 come back to why we use this fourth segment\xa0\n 1206 02:13:38,720 --> 02:13:45,199 these three segments of the memory are used. When\xa0\n 1207 02:13:45,760 --> 02:13:51,119 we have a function square that gives me the\xa0\n 1208 02:13:51,119 --> 02:13:59,760 square of sum that is given two arguments x and\xa0\n 1209 02:13:59,760 --> 02:14:05,920 in the main method, I'm just calling this function\xa0\n 1210 02:14:06,479 --> 02:14:13,039 Let us now see what happens in the memory when\xa0\n 1211 02:14:13,039 --> 02:14:20,720 in green here is memory reserved as stack and\xa0\n 1212 02:14:20,720 --> 02:14:26,880 statical global variables section when the program\xa0\n 1213 02:14:26,880 --> 02:14:32,800 when the main method is invoked some amount of\xa0\n 1214 02:14:32,800 --> 02:14:40,480 of main and this total is a global variable. So it\xa0\n 1215 02:14:40,479 --> 02:14:47,199 allocated on stack for execution of main can also\xa0\n 1216 02:14:47,199 --> 02:14:54,239 the local variables arguments and the information\xa0\n 1217 02:14:54,239 --> 02:15:00,319 all this information is stored within this stack\xa0\n 1218 02:15:00,319 --> 02:15:06,079 is calculated when the program is compiling.\xa0\n 1219 02:15:06,640 --> 02:15:14,880 that's right shortcut s four s four squared off\xa0\n 1220 02:15:14,880 --> 02:15:20,880 call to square of some all these local variables\xa0\n 1221 02:15:21,439 --> 02:15:26,719 Now, sum of square calls square, let's\xa0\n 1222 02:15:26,720 --> 02:15:30,960 So, another stack frame for Squire and\xa0\n 1223 02:15:31,520 --> 02:15:38,480 at any time during the execution of the program,\xa0\n 1224 02:15:38,479 --> 02:15:44,799 and rest are kind of paused waiting for the\xa0\n 1225 02:15:44,800 --> 02:15:52,000 will resume execution I have drawn this play and\xa0\n 1226 02:15:52,000 --> 02:15:57,439 Okay, so, this total is a global variable\xa0\n 1227 02:15:57,439 --> 02:16:02,639 because it is not declared and declared inside\xa0\n 1228 02:16:02,640 --> 02:16:07,440 we go to this particular statement where we call\xa0\n 1229 02:16:07,439 --> 02:16:13,839 So right now, this is our call stack. This program\xa0\n 1230 02:16:13,840 --> 02:16:18,400 I have written this program this way\xa0\n 1231 02:16:18,960 --> 02:16:23,439 calling each other let's say right now, we are\xa0\n 1232 02:16:23,439 --> 02:16:28,879 executing this statement. So, at this stage\xa0\n 1233 02:16:29,600 --> 02:16:33,201 Now, when this method finishes, we will\xa0\n 1234 02:16:34,000 --> 02:16:40,000 As soon as Squire function will return, it will\xa0\n 1235 02:16:40,000 --> 02:16:46,639 square of some function will resume. Once again\xa0\n 1236 02:16:46,639 --> 02:16:52,959 come back to this particular line to line total is\xa0\n 1237 02:16:54,318 --> 02:17:00,718 Now main we'll call printf. So once again\xa0\n 1238 02:17:01,439 --> 02:17:05,838 printf will finish and the control will come\xa0\n 1239 02:17:06,559 --> 02:17:12,959 and when main finishes program will also finish.\xa0\n 1240 02:17:12,959 --> 02:17:18,799 cleared, there was no need in this program. To\xa0\n 1241 02:17:18,799 --> 02:17:25,519 assign a variable as global only if it is needed\xa0\n 1242 02:17:25,520 --> 02:17:31,201 is needed for the whole lifetime of the program.\xa0\n 1243 02:17:31,920 --> 02:17:38,799 keep a variable for the whole lifetime of program\xa0\n 1244 02:17:38,799 --> 02:17:44,879 program just to understand the cost concepts.\xa0\n 1245 02:17:44,879 --> 02:17:50,799 when our program starts our operating system\xa0\n 1246 02:17:51,840 --> 02:17:59,280 always allocates one MB of space as stack,\xa0\n 1247 02:17:59,280 --> 02:18:05,200 and the actual allocation of the local variables\xa0\n 1248 02:18:05,200 --> 02:18:11,920 our call stack grows beyond the reserved memory\xa0\n 1249 02:18:11,920 --> 02:18:18,719 a calls b b call C and we go on calling and we\xa0\n 1250 02:18:18,719 --> 02:18:24,559 then this is called Stack Overflow and\xa0\n 1251 02:18:26,318 --> 02:18:31,759 One common case of Stack Overflow is when\xa0\n 1252 02:18:31,760 --> 02:18:38,960 goes infinitely into recursion. So, as we can see,\xa0\n 1253 02:18:38,959 --> 02:18:45,759 set aside for stack does not grow during runtime\xa0\n 1254 02:18:45,760 --> 02:18:52,880 So if it is one MB then if the allocation of\xa0\n 1255 02:18:52,879 --> 02:18:59,439 our program will crash for the allocation and de\xa0\n 1256 02:18:59,439 --> 02:19:05,359 set rule when a function is called it is pushed\xa0\n 1257 02:19:05,359 --> 02:19:10,639 finishes it is popped or removed from the stack\xa0\n 1258 02:19:10,639 --> 02:19:18,161 variable if it is on the stack. Another limitation\xa0\n 1259 02:19:18,159 --> 02:19:25,119 like an array as local variable then we need to\xa0\n 1260 02:19:25,120 --> 02:19:30,880 If we have a scenario like we want to decide how\xa0\n 1261 02:19:30,879 --> 02:19:38,079 during runtime, then it is a problem that stack\xa0\n 1262 02:19:38,079 --> 02:19:44,398 chunks of memory or keeping variable in the\xa0\n 1263 02:19:46,239 --> 02:19:53,760 like stack applications he is not fixed. Its size\xa0\n 1264 02:19:54,478 --> 02:20:00,798 And there is no set rule for allocation and\xa0\n 1265 02:20:00,799 --> 02:20:07,759 control how much memory to use from the heap\xa0\n 1266 02:20:07,760 --> 02:20:13,760 during the applications lifetime and he can\xa0\n 1267 02:20:14,559 --> 02:20:19,680 on the system itself. That is a dangerous\xa0\n 1268 02:20:19,680 --> 02:20:26,880 about using heap For this reason, we also\xa0\n 1269 02:20:26,879 --> 02:20:34,159 store of memory, we can get as much as we want\xa0\n 1270 02:20:34,159 --> 02:20:40,719 operating system Language Runtime or the compiler\xa0\n 1271 02:20:40,719 --> 02:20:46,959 computer architecture. But an abstracted way\xa0\n 1272 02:20:46,959 --> 02:20:54,479 this is one large free pool of memory available\xa0\n 1273 02:20:56,000 --> 02:21:03,760 heap is also called dynamic memory and using the\xa0\n 1274 02:21:03,760 --> 02:21:12,239 And let us now see how to use the heap in our C\xa0\n 1275 02:21:12,239 --> 02:21:18,398 and I will draw one more rectangular block\xa0\n 1276 02:21:18,398 --> 02:21:24,878 that I must point out before moving forward. He\xa0\n 1277 02:21:24,879 --> 02:21:30,079 know about this data structure heap yet, you will\xa0\n 1278 02:21:30,079 --> 02:21:36,799 this nomenclature here has nothing to do with\xa0\n 1279 02:21:36,799 --> 02:21:43,599 being used only for the large free pool of memory.\xa0\n 1280 02:21:43,600 --> 02:21:48,240 context. This term often confuses a lot\xa0\n 1281 02:21:48,239 --> 02:21:54,159 structure. Stack is also one data structure but\xa0\n 1282 02:21:54,159 --> 02:21:58,719 implementation of stack data structure. But heap\xa0\n 1283 02:21:59,359 --> 02:22:11,359 To use dynamic memory in C, we need to know about\xa0\n 1284 02:22:11,359 --> 02:22:20,719 use dynamic memory in c++ we need to know about\xa0\n 1285 02:22:20,719 --> 02:22:29,679 can also be used in c++ because c++ has backward\xa0\n 1286 02:22:29,680 --> 02:22:37,760 c++ programmers mostly use these two operators,\xa0\n 1287 02:22:37,760 --> 02:22:43,281 and try to understand how things happen. When\xa0\n 1288 02:22:43,280 --> 02:22:50,159 some code example in C, let us write a C program.\xa0\n 1289 02:22:50,159 --> 02:22:55,280 one MB first tag this was just an assumption. In\xa0\n 1290 02:22:55,280 --> 02:23:00,960 the operating system and the compiler, it is a\xa0\n 1291 02:23:00,959 --> 02:23:06,159 If we declare a variable like this, then this\xa0\n 1292 02:23:06,879 --> 02:23:12,879 memory for this particular variable a will\xa0\n 1293 02:23:12,879 --> 02:23:20,559 method. Let us say we want to store an integer on\xa0\n 1294 02:23:20,559 --> 02:23:26,799 on the heap, we need to call the malloc function\xa0\n 1295 02:23:26,799 --> 02:23:33,599 how much memory to allocate on the heap in bytes,\xa0\n 1296 02:23:33,600 --> 02:23:39,520 size of integer then we are saying that hey give\xa0\n 1297 02:23:39,520 --> 02:23:45,520 bytes is the typical size of an integer. So one\xa0\n 1298 02:23:45,520 --> 02:23:51,520 on the heap and malloc will return a pointer to\xa0\n 1299 02:23:51,520 --> 02:23:57,201 returns a void pointer. Let us say the starting\xa0\n 1300 02:23:58,079 --> 02:24:03,680 So malloc will return us the address 200. Now\xa0\n 1301 02:24:03,680 --> 02:24:09,920 variable to main. So this will be this will be\xa0\n 1302 02:24:09,920 --> 02:24:15,600 We have done a typecasting here because malloc\xa0\n 1303 02:24:15,600 --> 02:24:23,760 p is an integer pointer. Now p stores the address\xa0\n 1304 02:24:23,760 --> 02:24:29,840 got some block of memory on the heap, which we\xa0\n 1305 02:24:29,840 --> 02:24:35,360 do not know what's there in this particular block\xa0\n 1306 02:24:36,000 --> 02:24:41,600 we need to dereference this location using the\xa0\n 1307 02:24:41,600 --> 02:24:47,920 the only way to use memory on heap is through\xa0\n 1308 02:24:48,559 --> 02:24:54,000 look for some free space in the heap book set or\xa0\n 1309 02:24:54,000 --> 02:24:59,760 the only way you can access this particular block\xa0\n 1310 02:24:59,760 --> 02:25:07,200 to function. Now, let us write something like\xa0\n 1311 02:25:07,760 --> 02:25:14,159 I will go ahead and make one more call to malloc.\xa0\n 1312 02:25:14,159 --> 02:25:20,959 block of four bytes is allocated on the heap. And\xa0\n 1313 02:25:20,959 --> 02:25:25,759 the address that is returned by the second call to\xa0\n 1314 02:25:26,478 --> 02:25:33,838 So, what happens is that P is now pointing to\xa0\n 1315 02:25:33,840 --> 02:25:40,399 this address, we allocated one more block and\xa0\n 1316 02:25:40,398 --> 02:25:46,079 the previous block will still set in the heap.\xa0\n 1317 02:25:46,079 --> 02:25:51,680 be cleared off automatically. At any point in\xa0\n 1318 02:25:51,680 --> 02:25:57,360 block of memory which is dynamically allocated\xa0\n 1319 02:25:57,359 --> 02:26:02,159 because it is unnecessary consumption of memory\xa0\n 1320 02:26:02,159 --> 02:26:08,638 have done here is that once we were done using\xa0\n 1321 02:26:08,639 --> 02:26:14,799 have made a call to the function free any memory\xa0\n 1322 02:26:15,439 --> 02:26:20,960 by calling free and to free we pass the pointer\xa0\n 1323 02:26:20,959 --> 02:26:26,000 So, now with this code, this first block of\xa0\n 1324 02:26:26,000 --> 02:26:32,398 be pointing to another memory address. It is the\xa0\n 1325 02:26:32,398 --> 02:26:37,039 on the heap if he has allocated it\xa0\n 1326 02:26:37,040 --> 02:26:41,439 So if you see in terms of the scope of the\xa0\n 1327 02:26:41,439 --> 02:26:47,679 is not automatically D allocated when the function\xa0\n 1328 02:26:47,680 --> 02:26:52,639 it does not need to live for the whole lifetime\xa0\n 1329 02:26:52,639 --> 02:26:57,359 we can control when to free anything on the\xa0\n 1330 02:26:58,398 --> 02:27:03,599 If we wanted to store an array on the heap, like\xa0\n 1331 02:27:03,600 --> 02:27:09,840 then all we do is make a call to the malloc asking\xa0\n 1332 02:27:09,840 --> 02:27:17,440 the array in bytes. So, if we want an integer\xa0\n 1333 02:27:17,439 --> 02:27:24,799 to malloc asking 20 into size of int which will\xa0\n 1334 02:27:24,799 --> 02:27:31,439 is that one big contiguous block of memory for\xa0\n 1335 02:27:31,439 --> 02:27:36,799 we will get the starting address of this block.\xa0\n 1336 02:27:37,920 --> 02:27:44,318 the speed will point here to the base address of\xa0\n 1337 02:27:44,318 --> 02:27:53,439 20 integers as P zero p one, p two and so on.\xa0\n 1338 02:27:53,439 --> 02:27:59,759 address P and P one is same as saying value\xa0\n 1339 02:28:01,120 --> 02:28:06,880 One more thing, if malloc is not able to find any\xa0\n 1340 02:28:06,879 --> 02:28:12,559 memory on the heap, it returns null. So for\xa0\n 1341 02:28:12,559 --> 02:28:19,840 need to write our code appropriately malloc and\xa0\n 1342 02:28:19,840 --> 02:28:25,200 If we want to write the same code, same logic\xa0\n 1343 02:28:25,840 --> 02:28:32,318 Instead of using these two functions malloc and\xa0\n 1344 02:28:32,318 --> 02:28:37,599 and write our code something like this. So instead\xa0\n 1345 02:28:37,600 --> 02:28:45,121 here. And instead of using free we are using\xa0\n 1346 02:28:45,120 --> 02:28:50,640 use something like this where we put the size in\xa0\n 1347 02:28:50,639 --> 02:28:55,280 we use this particular operator delete\xa0\n 1348 02:28:55,920 --> 02:29:01,120 With c++ we do not have to do all these typecast\xa0\n 1349 02:29:01,120 --> 02:29:07,040 typecast it back to integer pointer, new undelete\xa0\n 1350 02:29:07,040 --> 02:29:12,720 they're used with a type and return pointers\xa0\n 1351 02:29:12,719 --> 02:29:18,879 get a pointer to integer only, we will be talking\xa0\n 1352 02:29:18,879 --> 02:29:25,679 functions in more detail in the coming lessons.\xa0\n 1353 02:29:25,680 --> 02:29:32,639 learned the concept of dynamic memory allocation,\xa0\n 1354 02:29:32,639 --> 02:29:38,399 applications memory. In this lesson, we will be\xa0\n 1355 02:29:38,398 --> 02:29:45,119 in C for dynamic memory allocation. We will look\xa0\n 1356 02:29:46,079 --> 02:29:52,318 We will be talking about three functions that\xa0\n 1357 02:29:52,318 --> 02:30:00,959 functions are malloc, calloc and realloc. And\xa0\n 1358 02:30:00,959 --> 02:30:09,759 D allocates a block of memory on the heap and this\xa0\n 1359 02:30:09,760 --> 02:30:16,239 This is the most frequently used library function\xa0\n 1360 02:30:16,239 --> 02:30:21,359 or the definition of this function is\xa0\n 1361 02:30:21,359 --> 02:30:29,679 argument asks you for the size of the memory block\xa0\n 1362 02:30:29,680 --> 02:30:35,680 are not aware of it is a data type that stores\xa0\n 1363 02:30:36,398 --> 02:30:43,279 think of this particular data type as unsigned\xa0\n 1364 02:30:43,280 --> 02:30:48,719 value it cannot either be zero or a positive\xa0\n 1365 02:30:48,719 --> 02:30:54,639 we use this particular type. And this\xa0\n 1366 02:30:54,639 --> 02:31:00,399 we have talked about word pointer in our previous\xa0\n 1367 02:31:00,398 --> 02:31:06,798 us the address of the first byte in this\xa0\n 1368 02:31:07,920 --> 02:31:12,318 So using malloc, you kind of say that, hey,\xa0\n 1369 02:31:12,318 --> 02:31:17,599 bytes. Let us say what we have here in the right\xa0\n 1370 02:31:18,318 --> 02:31:23,920 Each of these partitions are one byte. As we\xa0\n 1371 02:31:23,920 --> 02:31:31,359 let us say this bottommost byte has the address\xa0\n 1372 02:31:31,359 --> 02:31:36,239 increasing towards the top, I'll mark these\xa0\n 1373 02:31:36,879 --> 02:31:40,318 Let's say in our code, we make a\xa0\n 1374 02:31:40,318 --> 02:31:46,959 that, hey, give me a block of memory.\xa0\n 1375 02:31:46,959 --> 02:31:52,479 the address returned by malloc in a void pointer\xa0\n 1376 02:31:53,200 --> 02:31:58,559 rectangular block that we're showing here in the\xa0\n 1377 02:31:58,559 --> 02:32:04,639 malloc These are four bytes the starting address\xa0\n 1378 02:32:05,200 --> 02:32:10,960 want to print the address and p, then what\xa0\n 1379 02:32:12,318 --> 02:32:16,318 This is cool. Using malloc, we are able to\xa0\n 1380 02:32:16,318 --> 02:32:21,759 amount of memory. But why do we resolve memory,\xa0\n 1381 02:32:21,760 --> 02:32:27,840 to store some data there. So we do not want to\xa0\n 1382 02:32:27,840 --> 02:32:32,799 although it is correct to write a statement like\xa0\n 1383 02:32:32,799 --> 02:32:38,719 we would first calculate how much amount of memory\xa0\n 1384 02:32:38,719 --> 02:32:47,119 store an integer, just one in teacher. So I need\xa0\n 1385 02:32:47,120 --> 02:32:53,280 So we typically use the function size off, which\xa0\n 1386 02:32:53,840 --> 02:33:00,239 to figure out the size of the data type. And\xa0\n 1387 02:33:00,239 --> 02:33:06,398 returned by size of by the number of units\xa0\n 1388 02:33:07,200 --> 02:33:12,399 if we need memory for just one integer, and\xa0\n 1389 02:33:12,398 --> 02:33:18,959 we would write something like this, give me 10\xa0\n 1390 02:33:18,959 --> 02:33:23,599 type. Sometimes you may know that I know that size\xa0\n 1391 02:33:23,600 --> 02:33:28,640 like 10 into four here, but it's not a good\xa0\n 1392 02:33:28,639 --> 02:33:34,079 upon the compiler. And we will also be using\xa0\n 1393 02:33:34,639 --> 02:33:41,680 So we must use malloc. Like this. Total number of\xa0\n 1394 02:33:41,680 --> 02:33:49,440 size of one unit, one unit of the data. Let's\xa0\n 1395 02:33:50,000 --> 02:33:55,120 I have picked up three so that I have enough\xa0\n 1396 02:33:55,120 --> 02:34:01,280 I'm showing here. Let's say this block of 12 bytes\xa0\n 1397 02:34:01,840 --> 02:34:08,559 typically, so the starting address would\xa0\n 1398 02:34:08,559 --> 02:34:14,719 if you see malloc returns a void pointer and\xa0\n 1399 02:34:14,719 --> 02:34:18,479 you cannot write a statement something\xa0\n 1400 02:34:19,520 --> 02:34:24,479 This is incorrect, you cannot dereference\xa0\n 1401 02:34:24,478 --> 02:34:29,838 used as a generic pointer type which is normally\xa0\n 1402 02:34:30,559 --> 02:34:36,240 data type. And then it is used because malloc\xa0\n 1403 02:34:36,239 --> 02:34:41,519 some number of bytes in memory in heap. It does\xa0\n 1404 02:34:41,520 --> 02:34:47,520 memory to store character or to store integer or\xa0\n 1405 02:34:47,520 --> 02:34:52,640 a void pointer to the starting address. To be\xa0\n 1406 02:34:52,639 --> 02:34:58,879 typecast this void pointer into a pointer of some\xa0\n 1407 02:34:59,600 --> 02:35:05,680 Hanes Star p, instead of picking up a void\xa0\n 1408 02:35:05,680 --> 02:35:12,159 because we want to operate with integers, so to\xa0\n 1409 02:35:12,159 --> 02:35:17,599 we do the typecasting here of the void pointer\xa0\n 1410 02:35:18,478 --> 02:35:24,398 If we want to assign some value to the second\xa0\n 1411 02:35:24,398 --> 02:35:29,599 will do something like we will dereference the\xa0\n 1412 02:35:29,600 --> 02:35:35,760 four here. And if we want to access the third\xa0\n 1413 02:35:36,318 --> 02:35:42,000 Let's set the value of sex here. By asking for a\xa0\n 1414 02:35:42,559 --> 02:35:50,240 creating an array of integers with three elements,\xa0\n 1415 02:35:50,239 --> 02:35:55,519 And we could also write as strict p plus one as\xa0\n 1416 02:35:56,159 --> 02:36:03,439 the mean the same. All the manipulation on\xa0\n 1417 02:36:03,439 --> 02:36:08,479 pointers, you have a pointer to the starting\xa0\n 1418 02:36:09,040 --> 02:36:15,280 element in the array, you increment the pointer.\xa0\n 1419 02:36:15,280 --> 02:36:20,800 functions that allocate block of memory.\xa0\n 1420 02:36:20,799 --> 02:36:28,239 or definition of calloc is something like this\xa0\n 1421 02:36:28,239 --> 02:36:34,879 only a slightly different calloc also returns\xa0\n 1422 02:36:34,879 --> 02:36:40,079 instead of one argument, it takes two arguments.\xa0\n 1423 02:36:40,079 --> 02:36:44,478 of a particular data type. And the second\xa0\n 1424 02:36:45,120 --> 02:36:50,160 So with malloc, if we have to declare an array\xa0\n 1425 02:36:50,159 --> 02:36:55,039 we would say three into size of int. With\xa0\n 1426 02:36:55,600 --> 02:37:01,840 the first argument is how many units of the data\xa0\n 1427 02:37:01,840 --> 02:37:07,440 the second argument is the size of data type in\xa0\n 1428 02:37:07,439 --> 02:37:12,799 and calloc. When malloc allocate some amount of\xa0\n 1429 02:37:13,359 --> 02:37:19,920 any value. So if you do not fill in any value into\xa0\n 1430 02:37:19,920 --> 02:37:27,120 some garbage there. But if you allocate memory\xa0\n 1431 02:37:27,120 --> 02:37:36,000 value zero, so it also initializes the memory that\xa0\n 1432 02:37:36,000 --> 02:37:43,920 want to talk about is realloc. If you have a block\xa0\n 1433 02:37:43,920 --> 02:37:49,359 and if you want to change the size of this\xa0\n 1434 02:37:49,359 --> 02:37:54,960 The definition or the signature of this particular\xa0\n 1435 02:37:54,959 --> 02:38:02,559 first argument is pointed to the starting address\xa0\n 1436 02:38:03,359 --> 02:38:09,920 the size of the new block, there can be a couple\xa0\n 1437 02:38:09,920 --> 02:38:16,000 that we want may be larger than than the size\xa0\n 1438 02:38:16,000 --> 02:38:21,760 may create an entirely new block and copy the\xa0\n 1439 02:38:21,760 --> 02:38:26,800 previous block into the new block, if contiguous\xa0\n 1440 02:38:27,520 --> 02:38:32,240 with the existing block and the existing\xa0\n 1441 02:38:32,239 --> 02:38:37,439 some code examples and see what we can do with\xa0\n 1442 02:38:38,159 --> 02:38:45,680 function three in our code itself. I'll write some\xa0\n 1443 02:38:45,680 --> 02:38:52,319 we can use dynamic memory allocation. The first\xa0\n 1444 02:38:52,879 --> 02:38:58,719 And we want to first asks the user the size of\xa0\n 1445 02:38:58,719 --> 02:39:02,959 exactly of this particular size entered by\xa0\n 1446 02:39:02,959 --> 02:39:10,719 n and I write a print statement like enter size\xa0\n 1447 02:39:10,719 --> 02:39:19,039 console. Now I want to declare an array of size n\xa0\n 1448 02:39:19,040 --> 02:39:24,960 this particular value in the braces cannot be a\xa0\n 1449 02:39:24,959 --> 02:39:29,839 we need to know the size of the array we cannot\xa0\n 1450 02:39:29,840 --> 02:39:35,360 In such a scenario, we can allocate the memory\xa0\n 1451 02:39:35,359 --> 02:39:42,799 in Star A is equal to and we will make a\xa0\n 1452 02:39:42,799 --> 02:39:50,879 equal to the size of an integers. And this will\xa0\n 1453 02:39:50,879 --> 02:39:58,000 this particular the return of malloc to integer\xa0\n 1454 02:39:58,000 --> 02:40:03,920 we can fill in some data into the dynamically into\xa0\n 1455 02:40:03,920 --> 02:40:08,879 Let's say we want to put data something like first\xa0\n 1456 02:40:08,879 --> 02:40:15,519 and so on. So we will write something like a i is\xa0\n 1457 02:40:15,520 --> 02:40:22,161 the array. Let's say we want to pick up size of\xa0\n 1458 02:40:23,120 --> 02:40:28,800 we have 10 elements from one to 10. If\xa0\n 1459 02:40:31,680 --> 02:40:34,639 then this is what we get, we\xa0\n 1460 02:40:36,079 --> 02:40:40,558 If we wanted to use calloc, instead of\xa0\n 1461 02:40:40,559 --> 02:40:44,399 code would be that we would use calloc\xa0\n 1462 02:40:45,520 --> 02:40:49,440 and would separate out as first argument and\xa0\n 1463 02:40:50,239 --> 02:40:56,398 And this program will run around seamlessly. There\xa0\n 1464 02:40:56,398 --> 02:41:03,039 If we do not use this initialization, then with\xa0\n 1465 02:41:03,040 --> 02:41:09,760 being printed as zero, they're all initialized\xa0\n 1466 02:41:11,120 --> 02:41:16,559 then these elements are not initialized, there is\xa0\n 1467 02:41:16,559 --> 02:41:21,760 array. So this is one difference between malloc\xa0\n 1468 02:41:21,760 --> 02:41:29,280 zero into each byte while malloc doesn't do this\xa0\n 1469 02:41:29,279 --> 02:41:35,519 loop again. And now we will talk about free any\xa0\n 1470 02:41:35,520 --> 02:41:41,201 allocated till the lifetime of the program till\xa0\n 1471 02:41:41,200 --> 02:41:49,120 explicitly D allocated and to deallocate memory\xa0\n 1472 02:41:49,120 --> 02:41:53,760 we have the function three and to the function\xa0\n 1473 02:41:53,760 --> 02:42:00,639 memory block as argument. Now what will happen\xa0\n 1474 02:42:00,639 --> 02:42:07,279 memory is erased, it may or may not be erased, it\xa0\n 1475 02:42:07,279 --> 02:42:12,639 But that memory will be available for a location\xa0\n 1476 02:42:12,639 --> 02:42:17,760 see what happens in this case when we are printing\xa0\n 1477 02:42:17,760 --> 02:42:22,399 array sizes five and as you can see the elements\xa0\n 1478 02:42:22,398 --> 02:42:29,119 being printed. If this tree was not there, we\xa0\n 1479 02:42:29,920 --> 02:42:35,200 Now the obvious question would be even though\xa0\n 1480 02:42:35,200 --> 02:42:40,639 access that the value at that particular memory\xa0\n 1481 02:42:40,639 --> 02:42:45,920 the element at index i Well, that is one dangerous\xa0\n 1482 02:42:45,920 --> 02:42:50,960 you can look at the value at that address. But\xa0\n 1483 02:42:50,959 --> 02:42:55,919 if that address is allocated to you. What if that\xa0\n 1484 02:42:56,478 --> 02:43:00,078 Well, you do not know what you are reading\xa0\n 1485 02:43:00,079 --> 02:43:05,360 here it will have, it actually depends upon\xa0\n 1486 02:43:05,359 --> 02:43:10,719 let's say after freeing we try to access the third\xa0\n 1487 02:43:11,520 --> 02:43:16,319 Let's see what happens if we run this program.\xa0\n 1488 02:43:16,318 --> 02:43:22,478 even after free, we are able to modify the value\xa0\n 1489 02:43:22,478 --> 02:43:28,878 machine such a program may cause our program to\xa0\n 1490 02:43:28,879 --> 02:43:33,278 that is allocated. Otherwise, it's like shooting\xa0\n 1491 02:43:35,040 --> 02:43:41,279 We will not talk about Maria Alok. If we want\xa0\n 1492 02:43:41,279 --> 02:43:47,519 we have a memory block to store n elements in an\xa0\n 1493 02:43:47,520 --> 02:43:53,041 to double the size of the array. Or maybe we\xa0\n 1494 02:43:53,040 --> 02:43:58,080 For such scenario we use real lock and call\xa0\n 1495 02:43:58,079 --> 02:44:04,318 Let's say we take another pointer variable b\xa0\n 1496 02:44:04,959 --> 02:44:10,479 and size of the new block. So the new block is\xa0\n 1497 02:44:10,478 --> 02:44:16,558 to of course do the typecasting here okay now\xa0\n 1498 02:44:16,559 --> 02:44:23,680 memory block of size to n and copy the values in\xa0\n 1499 02:44:24,478 --> 02:44:30,878 into this new memory block. how realloc works is\xa0\n 1500 02:44:30,879 --> 02:44:36,159 the size of the previous block, then if it is\xa0\n 1501 02:44:36,159 --> 02:44:42,318 constitutive memory with the same block. Then\xa0\n 1502 02:44:42,318 --> 02:44:47,679 A new block is allocated and the previous block\xa0\n 1503 02:44:47,680 --> 02:44:52,399 and the previous block is d allocated this\xa0\n 1504 02:44:52,398 --> 02:44:57,599 print statement. I'll print the previous block\xa0\n 1505 02:44:57,600 --> 02:45:05,440 in B and I'll also print all the two elements in\xa0\n 1506 02:45:05,439 --> 02:45:11,279 B in one single line. Let's say size of there is\xa0\n 1507 02:45:12,398 --> 02:45:18,398 9920128. And if you see the new address is also\xa0\n 1508 02:45:18,398 --> 02:45:23,599 block only. And in B, the first five elements are\xa0\n 1509 02:45:24,398 --> 02:45:30,718 If we wanted to reduce the array size to let's\xa0\n 1510 02:45:30,719 --> 02:45:36,000 itself will be reduced. So let's say I want to\xa0\n 1511 02:45:36,000 --> 02:45:40,000 Now as you can see, the first two elements\xa0\n 1512 02:45:40,000 --> 02:45:44,719 in fact, they are there already, the rest three\xa0\n 1513 02:45:44,719 --> 02:45:49,359 we take only the integral part. So we kind of\xa0\n 1514 02:45:50,079 --> 02:45:56,558 In fact, if we give the size to be zero, here, all\xa0\n 1515 02:45:56,559 --> 02:46:01,920 a will be de allocated. So this statement\xa0\n 1516 02:46:03,760 --> 02:46:08,960 In most cases, we will put the return address\xa0\n 1517 02:46:08,959 --> 02:46:15,599 so we can write instead of writing B here we can\xa0\n 1518 02:46:15,600 --> 02:46:21,840 to realloc as null if the first argument\xa0\n 1519 02:46:22,398 --> 02:46:29,119 And let's say we want to create something like a\xa0\n 1520 02:46:29,680 --> 02:46:34,479 that is the first argument is null, then\xa0\n 1521 02:46:36,398 --> 02:46:41,278 This only creates a new block does not\xa0\n 1522 02:46:41,279 --> 02:46:47,519 So realloc can be used with the right arguments\xa0\n 1523 02:46:47,520 --> 02:46:53,360 for malloc. This was all about malloc,\xa0\n 1524 02:46:53,359 --> 02:46:59,838 code on dynamic memory allocation in the coming\xa0\n 1525 02:46:59,840 --> 02:47:07,920 we will talk about pointers as return types. For\xa0\n 1526 02:47:07,920 --> 02:47:13,359 It's just that a pointer stores the address\xa0\n 1527 02:47:13,359 --> 02:47:19,120 for a function to return pointer. But we must\xa0\n 1528 02:47:19,120 --> 02:47:25,439 return a pointer from a function. So let us try\xa0\n 1529 02:47:25,439 --> 02:47:31,279 some code. Now I want to write a very simple C\xa0\n 1530 02:47:32,000 --> 02:47:38,799 ad that will take two integers as argument\xa0\n 1531 02:47:38,799 --> 02:47:45,039 So let's say we declare another variable c and c\xa0\n 1532 02:47:46,398 --> 02:47:52,000 Now in the main method, I'll initialize two\xa0\n 1533 02:47:52,000 --> 02:47:58,318 let's say x is equal to two and y is equal to\xa0\n 1534 02:47:58,318 --> 02:48:04,879 which will be the return of this function\xa0\n 1535 02:48:04,879 --> 02:48:13,519 this function. And finally, I'll print something\xa0\n 1536 02:48:13,520 --> 02:48:20,720 the output here. I'll modify this code slightly.\xa0\n 1537 02:48:20,719 --> 02:48:30,879 once again, that x, y and Zed are local variables\xa0\n 1538 02:48:30,879 --> 02:48:36,479 function. And what really happens when we call\xa0\n 1539 02:48:36,478 --> 02:48:45,759 x of Maine is copied to variable a of add,\xa0\n 1540 02:48:45,760 --> 02:48:52,000 And what if we named these variables in\xa0\n 1541 02:48:53,279 --> 02:48:59,920 if we run this code output will be the same. This\xa0\n 1542 02:48:59,920 --> 02:49:06,799 Maine is copied to a of ad and the value in B of\xa0\n 1543 02:49:06,799 --> 02:49:13,039 this a in AD, they are not the same. You can\xa0\n 1544 02:49:13,920 --> 02:49:20,318 I'm printing the addresses of these two A's in\xa0\n 1545 02:49:20,318 --> 02:49:28,558 in Maine is something like 2883032 and in\xa0\n 1546 02:49:28,559 --> 02:49:34,240 That means these variables are not the same they\xa0\n 1547 02:49:34,239 --> 02:49:40,079 variables are local or specific to a particular\xa0\n 1548 02:49:40,079 --> 02:49:47,439 function mean can be called calling function and\xa0\n 1549 02:49:48,079 --> 02:49:54,478 In this particular call, when we are saying that c\xa0\n 1550 02:49:54,478 --> 02:50:01,358 where a and b in Maine are getting copied to\xa0\n 1551 02:50:02,478 --> 02:50:08,958 this can be called a call by value. Now, what\xa0\n 1552 02:50:08,959 --> 02:50:13,278 I want to pass the addresses of these two\xa0\n 1553 02:50:13,279 --> 02:50:20,719 to pass address of A and address of b to\xa0\n 1554 02:50:20,719 --> 02:50:25,519 function should be such that it should receive\xa0\n 1555 02:50:25,520 --> 02:50:34,800 it takes two pointer to integers A and B. And\xa0\n 1556 02:50:35,439 --> 02:50:42,000 by using this Asterix operator which is used to\xa0\n 1557 02:50:42,000 --> 02:50:47,600 called call by reference, a and b are integers\xa0\n 1558 02:50:48,398 --> 02:50:55,199 ad, A and B are not integer variables, A and\xa0\n 1559 02:50:56,559 --> 02:51:02,399 So the type is different. They're not in\xa0\n 1560 02:51:02,398 --> 02:51:08,638 they are also variables which are local to the\xa0\n 1561 02:51:09,200 --> 02:51:16,000 And now I'm using these two variables which are\xa0\n 1562 02:51:16,000 --> 02:51:21,120 a and b in the main method, and to do so we use\xa0\n 1563 02:51:21,120 --> 02:51:29,120 also work. I'll write a few more print statements\xa0\n 1564 02:51:29,120 --> 02:51:34,479 a and I have tried to print Astra K. And\xa0\n 1565 02:51:34,478 --> 02:51:40,079 So now ampersand a should give us the address of\xa0\n 1566 02:51:40,079 --> 02:51:47,519 of A in Maine because that is what this variable\xa0\n 1567 02:51:47,520 --> 02:51:55,121 A in Maine let us see. And the output. As you can\xa0\n 1568 02:51:57,120 --> 02:52:02,960 And address of A in AD is something else, but\xa0\n 1569 02:52:02,959 --> 02:52:09,358 a of Maine is equal to 353762. So the first\xa0\n 1570 02:52:09,359 --> 02:52:14,399 address we are printing the value, which is\xa0\n 1571 02:52:16,079 --> 02:52:21,840 Now I'll clean up some of these print statements\xa0\n 1572 02:52:21,840 --> 02:52:28,159 this value C and once again in the main method we\xa0\n 1573 02:52:28,159 --> 02:52:36,079 which is C of main why not do something like pass\xa0\n 1574 02:52:36,079 --> 02:52:40,398 we will do now is we will say that we want to\xa0\n 1575 02:52:40,959 --> 02:52:45,438 And here we will return ampersand see now\xa0\n 1576 02:52:45,439 --> 02:52:51,759 a variable it gives us the address. Now of course\xa0\n 1577 02:52:51,760 --> 02:52:56,960 address. So we will have to define a pointer\xa0\n 1578 02:52:56,959 --> 02:53:03,358 are printing, we will have to print the value at\xa0\n 1579 02:53:03,359 --> 02:53:10,559 So what we just did was, what we just did is we\xa0\n 1580 02:53:10,559 --> 02:53:18,399 to integer there are two syntax, we can say int\xa0\n 1581 02:53:18,398 --> 02:53:25,679 and then put this Asterix and in front of the\xa0\n 1582 02:53:26,398 --> 02:53:32,638 Now this function is returning pointer to integer.\xa0\n 1583 02:53:33,680 --> 02:53:40,000 Well, let me also strike off this print statement\xa0\n 1584 02:53:41,439 --> 02:53:48,079 Now have you got some logical error with this\xa0\n 1585 02:53:48,079 --> 02:53:53,439 some time. Now what I want to do is I want\xa0\n 1586 02:53:53,439 --> 02:53:58,559 that will print hello world. So I'll name this\xa0\n 1587 02:53:58,559 --> 02:54:05,680 I'll write a simple print statement. And now\xa0\n 1588 02:54:05,680 --> 02:54:11,920 call this function print hello world. And let's\xa0\n 1589 02:54:12,719 --> 02:54:18,479 Some is not correct. Now, I just saw that it\xa0\n 1590 02:54:18,478 --> 02:54:23,918 call this print hello world. What happened? So\xa0\n 1591 02:54:24,719 --> 02:54:30,559 I'll come back to this family a diagram of various\xa0\n 1592 02:54:30,559 --> 02:54:36,240 that is allocated to a program is typically\xa0\n 1593 02:54:36,239 --> 02:54:43,119 variables, and the information about function\xa0\n 1594 02:54:43,120 --> 02:54:48,000 run through this code. Let us simulate this code\xa0\n 1595 02:54:48,000 --> 02:54:54,000 For each function calls some part of the memory\xa0\n 1596 02:54:54,000 --> 02:55:00,239 call this the stack frame of that method or that\xa0\n 1597 02:55:00,239 --> 02:55:06,718 The main method is invoked. So in the stack frame,\xa0\n 1598 02:55:06,719 --> 02:55:12,000 and all the local variables of main function\xa0\n 1599 02:55:12,000 --> 02:55:19,120 the starting address of this stack frame is 100.\xa0\n 1600 02:55:19,120 --> 02:55:26,399 we will have three local variables created here,\xa0\n 1601 02:55:26,959 --> 02:55:37,199 integer pointer, let's say is at address 100, b\xa0\n 1602 02:55:37,200 --> 02:55:42,559 just making these assumptions. Now when the main\xa0\n 1603 02:55:42,559 --> 02:55:49,519 calling ad function, its execution will pause and\xa0\n 1604 02:55:49,520 --> 02:55:57,041 ad at any time whatever function is at the top\xa0\n 1605 02:55:57,040 --> 02:56:04,000 add function to complete and return. So here I\xa0\n 1606 02:56:04,000 --> 02:56:11,040 And now add comes here in the stack, let's say\xa0\n 1607 02:56:11,040 --> 02:56:18,319 has three local variables a B and C A and B are\xa0\n 1608 02:56:18,318 --> 02:56:30,159 and the value of B will be 112. Let's say their\xa0\n 1609 02:56:31,120 --> 02:56:38,640 Once again these are just random assumptions. Now\xa0\n 1610 02:56:38,639 --> 02:56:45,760 is pointing to this location and B is pointing to\xa0\n 1611 02:56:45,760 --> 02:56:52,719 in a and astrick B's value at address stored in B\xa0\n 1612 02:56:52,719 --> 02:57:00,799 be added. Now this add function will return the\xa0\n 1613 02:57:00,799 --> 02:57:07,840 finish its execution. So this PTR will be 144 and\xa0\n 1614 02:57:08,398 --> 02:57:15,920 will be de allocated. Now this memory above\xa0\n 1615 02:57:16,639 --> 02:57:23,279 And even though this variable pointer PTR\xa0\n 1616 02:57:23,279 --> 02:57:29,040 of this particular block it kind of points to this\xa0\n 1617 02:57:29,040 --> 02:57:34,880 because this memory has been de allocated. Now\xa0\n 1618 02:57:34,879 --> 02:57:39,358 a memory from stack will be allocated for\xa0\n 1619 02:57:39,359 --> 02:57:44,880 of main method. This is main. So let's say\xa0\n 1620 02:57:45,439 --> 02:57:52,079 from address 132 address 150. But there is no\xa0\n 1621 02:57:52,079 --> 02:57:58,719 But still function call execution involves storage\xa0\n 1622 02:57:58,719 --> 02:58:04,799 to 150 is for print hello world, I'll write\xa0\n 1623 02:58:04,799 --> 02:58:11,359 has been overwritten. So this block at 144 no\xa0\n 1624 02:58:11,359 --> 02:58:18,079 at this print statement to print the value at this\xa0\n 1625 02:58:18,079 --> 02:58:23,120 the obvious question would be why did the diode\xa0\n 1626 02:58:23,120 --> 02:58:28,800 making a call to print hello world, I would say\xa0\n 1627 02:58:28,799 --> 02:58:35,359 call any other function after making a call to add\xa0\n 1628 02:58:35,359 --> 02:58:40,559 that particular memory location. But when I made\xa0\n 1629 02:58:42,079 --> 02:58:49,279 If you see we have passed the addresses of these\xa0\n 1630 02:58:49,279 --> 02:58:54,559 but that is all right because called function\xa0\n 1631 02:58:54,559 --> 02:58:59,840 the stack. So anytime this called function is\xa0\n 1632 02:58:59,840 --> 02:59:05,840 So if ad is executing main is guaranteed to be\xa0\n 1633 02:59:05,840 --> 02:59:11,680 will be accessible to add. But if we try to return\xa0\n 1634 02:59:11,680 --> 02:59:17,440 the calling function, like if we want to return\xa0\n 1635 02:59:17,439 --> 02:59:22,398 function finishes and the control returns back\xa0\n 1636 02:59:22,398 --> 02:59:28,959 been de allocated. So it is okay to pass something\xa0\n 1637 02:59:28,959 --> 02:59:34,639 rather say that it's okay to pass a local variable\xa0\n 1638 02:59:34,639 --> 02:59:40,959 in this stack but it is not okay to return the\xa0\n 1639 02:59:40,959 --> 02:59:46,000 in the stack in the call stack. I hope this\xa0\n 1640 02:59:46,000 --> 02:59:52,559 be what are the use cases in which we may want to\xa0\n 1641 02:59:52,559 --> 02:59:58,399 address of some memory block in the heap section\xa0\n 1642 02:59:58,398 --> 03:00:03,358 then we can safely return that address Have\xa0\n 1643 03:00:03,359 --> 03:00:09,679 to be explicitly de allocated we control it's\xa0\n 1644 03:00:09,680 --> 03:00:15,120 is in the global section, a global variable\xa0\n 1645 03:00:15,920 --> 03:00:22,879 I can use malloc or new operator in c++ to get\xa0\n 1646 03:00:22,879 --> 03:00:30,959 something like this, I will declare this C as a\xa0\n 1647 03:00:30,959 --> 03:00:38,879 on the heap using a call to malloc. Now malloc\xa0\n 1648 03:00:38,879 --> 03:00:45,278 but it returns a pointer to an address which\xa0\n 1649 03:00:45,279 --> 03:00:52,079 using this pointer variable now, we can write this\xa0\n 1650 03:00:52,079 --> 03:00:58,398 memory block and then we can return this address C\xa0\n 1651 03:00:59,040 --> 03:01:05,600 but we are safe now because we are returning\xa0\n 1652 03:01:05,600 --> 03:01:12,559 not on the stack and this will work now. Now in\xa0\n 1653 03:01:12,559 --> 03:01:18,240 this block at address 500 in the heap see now\xa0\n 1654 03:01:18,239 --> 03:01:24,638 have written this data here this value six and\xa0\n 1655 03:01:24,639 --> 03:01:31,039 the add function which is address 500 is still\xa0\n 1656 03:01:31,040 --> 03:01:37,040 will not be de allocated anything on the heap\xa0\n 1657 03:01:37,040 --> 03:01:42,319 So while returning pointers from functions, we\xa0\n 1658 03:01:42,318 --> 03:01:48,239 sure that the address is not reused to store\xa0\n 1659 03:01:48,239 --> 03:01:55,279 that address. In most cases we will be returning\xa0\n 1660 03:01:55,279 --> 03:02:00,880 or memory that is in the global section the global\xa0\n 1661 03:02:00,879 --> 03:02:06,000 where we will be using pointers as function\xa0\n 1662 03:02:06,000 --> 03:02:11,359 linked list data structure. So this was pointers\xa0\n 1663 03:02:13,279 --> 03:02:17,840 In this lesson, we are going to talk about\xa0\n 1664 03:02:17,840 --> 03:02:25,600 as the name suggests, are used to store address\xa0\n 1665 03:02:25,600 --> 03:02:31,280 as variables that would store address of other\xa0\n 1666 03:02:31,279 --> 03:02:37,519 that can be used to store the address of some data\xa0\n 1667 03:02:37,520 --> 03:02:43,760 to point to or refer to some data. And it doesn't\xa0\n 1668 03:02:43,760 --> 03:02:50,000 as constant also. And we use pointers not just to\xa0\n 1669 03:02:50,000 --> 03:02:56,559 value at whatever address the pointer is pointing\xa0\n 1670 03:02:56,559 --> 03:03:02,719 that can store address or functions or in other\xa0\n 1671 03:03:02,719 --> 03:03:09,119 pointer to function to dereference and execute\xa0\n 1672 03:03:09,760 --> 03:03:15,840 Some basic questions would pop up what really\xa0\n 1673 03:03:15,840 --> 03:03:21,200 if we can have pointers to functions, what are\xa0\n 1674 03:03:21,840 --> 03:03:25,840 There are really interesting use cases of function\xa0\n 1675 03:03:26,478 --> 03:03:32,398 Let's first try to understand the core logic here.\xa0\n 1676 03:03:32,398 --> 03:03:38,159 the memory that is allocated to an application\xa0\n 1677 03:03:38,159 --> 03:03:42,959 these four segments. We have talked about\xa0\n 1678 03:03:43,840 --> 03:03:49,920 Okay, now a program is basically a set or\xa0\n 1679 03:03:49,920 --> 03:03:56,318 to the computer to perform a task, we can write\xa0\n 1680 03:03:56,318 --> 03:04:04,638 or c++, but at lowest level in its architecture,\xa0\n 1681 03:04:04,639 --> 03:04:10,879 any instruction that has to be executed by the\xa0\n 1682 03:04:10,879 --> 03:04:18,318 there will be some rules for encoding. So what we\xa0\n 1683 03:04:18,318 --> 03:04:26,398 in a high level language like C or c++, and we\xa0\n 1684 03:04:26,398 --> 03:04:33,278 and corresponding to the source code compiler\xa0\n 1685 03:04:33,279 --> 03:04:40,239 code, which is instructions encoded in binary.\xa0\n 1686 03:04:40,239 --> 03:04:46,638 compiler basically takes one or more source files.\xa0\n 1687 03:04:46,639 --> 03:04:54,479 in a file named program dot c. Now a compiler for\xa0\n 1688 03:04:54,478 --> 03:05:01,119 an executable file that will have the machine code\xa0\n 1689 03:05:01,680 --> 03:05:09,440 e x e, an executable file will be stored in\xa0\n 1690 03:05:09,439 --> 03:05:15,838 Whenever we say memory, just memory in context\xa0\n 1691 03:05:15,840 --> 03:05:22,000 or RAM that we also call the main memory or\xa0\n 1692 03:05:22,000 --> 03:05:28,639 we're talking about here will be a chunk of main\xa0\n 1693 03:05:28,639 --> 03:05:34,799 memory only when it starts execution. When the\xa0\n 1694 03:05:34,799 --> 03:05:42,079 claimed back. What really happens is that when we\xa0\n 1695 03:05:42,079 --> 03:05:47,760 some amount of memory is allocated to it. And that\xa0\n 1696 03:05:47,760 --> 03:05:54,319 The cold or text segment of applications memory\xa0\n 1697 03:05:54,318 --> 03:06:00,558 copied from the executable file. Instructions\xa0\n 1698 03:06:00,559 --> 03:06:07,120 Therefore, they are first copied to main memory,\xa0\n 1699 03:06:07,120 --> 03:06:13,279 execution, we need memory not just to store\xa0\n 1700 03:06:13,279 --> 03:06:20,159 a lot of data that we would work upon in a\xa0\n 1701 03:06:20,159 --> 03:06:25,519 storing and managing data. What I'm going to\xa0\n 1702 03:06:25,520 --> 03:06:30,161 text segment, let's assume that each\xa0\n 1703 03:06:30,159 --> 03:06:34,959 four bytes. I'm trying to show the section\xa0\n 1704 03:06:35,520 --> 03:06:40,319 each partition here is a block of four bytes\xa0\n 1705 03:06:40,959 --> 03:06:46,398 So, we have instruction zero at address 200.\xa0\n 1706 03:06:46,398 --> 03:06:53,039 And the next is at 208. And so on, instructions\xa0\n 1707 03:06:53,600 --> 03:07:00,319 Only Exception will be when instruction itself\xa0\n 1708 03:07:00,318 --> 03:07:06,638 instruction at this particular address, which will\xa0\n 1709 03:07:07,279 --> 03:07:13,200 if instructions 01 Nat address 200 is currently\xa0\n 1710 03:07:13,200 --> 03:07:20,639 be executed is instruction 02 at address 204\xa0\n 1711 03:07:21,359 --> 03:07:28,960 go to instruction 05 at address 216 which will\xa0\n 1712 03:07:28,959 --> 03:07:34,398 is nothing but a set of instructions\xa0\n 1713 03:07:34,398 --> 03:07:40,959 Let's say this block containing instructions five\xa0\n 1714 03:07:41,600 --> 03:07:47,600 if you and see one. Basically, a function is\xa0\n 1715 03:07:47,600 --> 03:07:53,600 in memory a function will be one contiguous\xa0\n 1716 03:07:53,600 --> 03:08:00,079 a function what we also call entry point of\xa0\n 1717 03:08:00,079 --> 03:08:09,840 instruction in the function. In this example here,\xa0\n 1718 03:08:09,840 --> 03:08:16,479 machine language will basically be an instruction\xa0\n 1719 03:08:16,478 --> 03:08:21,838 first instruction in a function. Now we will not\xa0\n 1720 03:08:21,840 --> 03:08:27,279 enough to understand function pointers. When we\xa0\n 1721 03:08:27,920 --> 03:08:35,040 we basically mean that function pointers store\xa0\n 1722 03:08:35,040 --> 03:08:40,399 of memory containing all the instructions in a\xa0\n 1723 03:08:40,398 --> 03:08:47,519 use function pointers in a C or c++ program. I am\xa0\n 1724 03:08:47,520 --> 03:08:53,680 thing that I'm going to do is I'm going to write\xa0\n 1725 03:08:53,680 --> 03:08:59,680 as argument and return the sum of these two\xa0\n 1726 03:08:59,680 --> 03:09:05,920 pointer that should point to this function\xa0\n 1727 03:09:06,799 --> 03:09:11,278 first type in the return type of the\xa0\n 1728 03:09:11,920 --> 03:09:19,279 at will return int so I typed in int, then\xa0\n 1729 03:09:19,279 --> 03:09:26,479 Asterix, and then name of the variable so I'm\xa0\n 1730 03:09:26,478 --> 03:09:32,558 within paranthesis. Type in all the argument types\xa0\n 1731 03:09:33,520 --> 03:09:39,680 argument types in function declaration should be\xa0\n 1732 03:09:39,680 --> 03:09:45,680 pointer will point. Because both arguments\xa0\n 1733 03:09:45,680 --> 03:09:50,880 in the declaration of function pointer also,\xa0\n 1734 03:09:50,879 --> 03:09:57,039 the address of a function we can use a statement\xa0\n 1735 03:09:57,040 --> 03:10:04,640 the address of something. This statement p equal\xa0\n 1736 03:10:05,680 --> 03:10:13,440 So P now points to AD. And using p, we can execute\xa0\n 1737 03:10:13,439 --> 03:10:20,318 going to declare an integer variable named C. And\xa0\n 1738 03:10:20,318 --> 03:10:26,238 I've done here is first I have used the Asterix\xa0\n 1739 03:10:26,799 --> 03:10:33,119 And then I have passed arguments just like I\xa0\n 1740 03:10:33,120 --> 03:10:39,279 two integers two and three. And if everything is\xa0\n 1741 03:10:39,279 --> 03:10:43,759 should be integer value five, when I\xa0\n 1742 03:10:44,719 --> 03:10:49,359 So, this is really cool, we just used a\xa0\n 1743 03:10:50,000 --> 03:10:55,279 and then execute the function. One thing about\xa0\n 1744 03:10:55,279 --> 03:11:01,439 we are putting the identifier or name of the\xa0\n 1745 03:11:01,439 --> 03:11:09,679 use the paranthesis then it will mean that we are\xa0\n 1746 03:11:09,680 --> 03:11:15,360 to integer in this case, if I would write\xa0\n 1747 03:11:15,359 --> 03:11:21,920 something like this, then this is declaring a\xa0\n 1748 03:11:21,920 --> 03:11:27,520 we can have an Asterix just before the function\xa0\n 1749 03:11:28,478 --> 03:11:36,878 these two syntax are same, but if I would put\xa0\n 1750 03:11:36,879 --> 03:11:43,439 function pointer. Okay, a few more things. In this\xa0\n 1751 03:11:43,439 --> 03:11:50,639 operator. Even if we do not use this ampersand\xa0\n 1752 03:11:50,639 --> 03:11:57,119 function name will also return us address of the\xa0\n 1753 03:11:57,120 --> 03:12:03,840 to data friends, instead of using this paranthesis\xa0\n 1754 03:12:03,840 --> 03:12:09,840 we can simply use the function pointer name\xa0\n 1755 03:12:09,840 --> 03:12:15,040 pointer name or identifier just like function\xa0\n 1756 03:12:15,040 --> 03:12:21,600 expected. So, we have two possible syntax for\xa0\n 1757 03:12:21,600 --> 03:12:28,000 whichever you like the second one is more famous,\xa0\n 1758 03:12:28,000 --> 03:12:34,559 type of the function pointer must be appropriate.\xa0\n 1759 03:12:34,559 --> 03:12:40,559 it something like this, then this function pointer\xa0\n 1760 03:12:40,559 --> 03:12:48,639 two integers as argument and should return void\xa0\n 1761 03:12:48,639 --> 03:12:53,439 to add this will give me compile ation error.\xa0\n 1762 03:12:53,439 --> 03:12:58,238 something like this, if I will have only one\xa0\n 1763 03:12:58,959 --> 03:13:05,438 then P cannot point to add. Okay, now let's use\xa0\n 1764 03:13:05,439 --> 03:13:10,559 and try to execute them through function\xa0\n 1765 03:13:10,559 --> 03:13:16,399 it will simply print hello on screen. And we will\xa0\n 1766 03:13:18,238 --> 03:13:24,879 And then we can initialize the pointer and fill\xa0\n 1767 03:13:24,879 --> 03:13:30,639 execute the function through the pointer. Let's\xa0\n 1768 03:13:31,680 --> 03:13:37,680 Now let's say we want to say hello to someone\xa0\n 1769 03:13:37,680 --> 03:13:43,440 to take a string as argument the declaration\xa0\n 1770 03:13:44,799 --> 03:13:49,759 and while calling we will have to pass a\xa0\n 1771 03:13:49,760 --> 03:13:56,000 what I get. And this is also looking fine. So this\xa0\n 1772 03:13:56,000 --> 03:14:01,920 pointers in C or c++ function pointers are used\xa0\n 1773 03:14:01,920 --> 03:14:08,000 interesting use cases. We will talk about the use\xa0\n 1774 03:14:08,000 --> 03:14:14,398 Thanks for watching. In our previous lesson,\xa0\n 1775 03:14:14,398 --> 03:14:22,000 wrote a simple program to understand how we can\xa0\n 1776 03:14:22,000 --> 03:14:27,279 we did not discuss the real use cases of function\xa0\n 1777 03:14:27,279 --> 03:14:32,479 can be useful. So in this lesson, we are going\xa0\n 1778 03:14:33,359 --> 03:14:38,880 All the use cases of function pointers are\xa0\n 1779 03:14:38,879 --> 03:14:45,839 be passed as arguments to functions. And then a\xa0\n 1780 03:14:45,840 --> 03:14:52,799 argument can call back the function that this\xa0\n 1781 03:14:52,799 --> 03:14:58,079 I will have to write some code. First of all,\xa0\n 1782 03:14:58,079 --> 03:15:06,959 a dysfunction. No argument returns void and\xa0\n 1783 03:15:06,959 --> 03:15:14,398 another function named p, this function takes\xa0\n 1784 03:15:14,398 --> 03:15:22,238 to a function that should take no argument and\xa0\n 1785 03:15:22,238 --> 03:15:29,359 I will simply use this function pointer PTR to\xa0\n 1786 03:15:29,359 --> 03:15:36,159 other words, I can say that I will call back the\xa0\n 1787 03:15:36,959 --> 03:15:42,159 In the main function, I will declare a function\xa0\n 1788 03:15:42,159 --> 03:15:48,079 point to a function that should take no argument\xa0\n 1789 03:15:48,959 --> 03:15:56,398 function pointer p with address of A. And now\xa0\n 1790 03:15:56,398 --> 03:16:02,798 function pointer P. Let's run this program and see\xa0\n 1791 03:16:02,799 --> 03:16:08,559 So, basically function a is getting executed,\xa0\n 1792 03:16:09,279 --> 03:16:13,279 In the main function instead of writing\xa0\n 1793 03:16:14,639 --> 03:16:19,279 a statement like this and this should be good\xa0\n 1794 03:16:19,279 --> 03:16:25,840 discussed this earlier also returns a pointer.\xa0\n 1795 03:16:25,840 --> 03:16:31,760 statements, when reference to a function is passed\xa0\n 1796 03:16:31,760 --> 03:16:39,040 is called a callback function. So, a is a callback\xa0\n 1797 03:16:39,040 --> 03:16:44,000 the reference through the function pointer.\xa0\n 1798 03:16:44,000 --> 03:16:50,318 through function pointer is a callback, we are\xa0\n 1799 03:16:50,318 --> 03:16:57,278 What's the point in calling a indirectly through\xa0\n 1800 03:16:57,279 --> 03:17:03,120 So let's now look at a scenario where using a\xa0\n 1801 03:17:03,120 --> 03:17:10,720 easier. I have a simple scenario, I have a list of\xa0\n 1802 03:17:11,279 --> 03:17:18,000 in increasing order of the value of integers.\xa0\n 1803 03:17:18,000 --> 03:17:24,398 I'm writing a function named sort that will take\xa0\n 1804 03:17:25,040 --> 03:17:31,200 the function will take an integer array,\xa0\n 1805 03:17:31,840 --> 03:17:37,760 I can write Asterix a these two are alternate\xa0\n 1806 03:17:38,719 --> 03:17:45,840 I have renamed this function as bubble sort. In\xa0\n 1807 03:17:45,840 --> 03:17:52,318 And in each pass as we go from left to right,\xa0\n 1808 03:17:52,318 --> 03:17:58,879 we compare adjacent elements. And if the element\xa0\n 1809 03:17:58,879 --> 03:18:05,119 higher index, while comparing adjacent elements,\xa0\n 1810 03:18:05,120 --> 03:18:12,160 is a pass on array. And this outer loop is\xa0\n 1811 03:18:12,159 --> 03:18:19,200 after first pass the largest element in the\xa0\n 1812 03:18:19,200 --> 03:18:23,840 and in the next pass, the second largest\xa0\n 1813 03:18:23,840 --> 03:18:29,760 this will go on and it finally in n passes, the\xa0\n 1814 03:18:29,760 --> 03:18:34,319 refresh bubblesort. There is a link to my\xa0\n 1815 03:18:34,318 --> 03:18:39,278 description of this video. In the main function\xa0\n 1816 03:18:39,279 --> 03:18:46,079 array A, and then I'm printing all the elements\xa0\n 1817 03:18:46,079 --> 03:18:52,238 increasing order. The output is as expected, we\xa0\n 1818 03:18:52,238 --> 03:18:57,840 want to sort my list in decreasing order of the\xa0\n 1819 03:18:57,840 --> 03:19:05,040 my code here? Think about it. The only change will\xa0\n 1820 03:19:05,040 --> 03:19:11,120 while comparing the adjacent elements. Now, I will\xa0\n 1821 03:19:11,120 --> 03:19:18,000 element at lower index is smaller than swap, or\xa0\n 1822 03:19:18,000 --> 03:19:24,079 comparing adjacent elements and swapping is\xa0\n 1823 03:19:24,079 --> 03:19:30,159 towards higher indices. With this condition.\xa0\n 1824 03:19:30,159 --> 03:19:35,359 number towards higher index. Let's see the output\xa0\n 1825 03:19:35,359 --> 03:19:40,238 decreasing order. Now my list is sorted in\xa0\n 1826 03:19:41,359 --> 03:19:47,599 Now let's say I have a requirement like this.\xa0\n 1827 03:19:47,600 --> 03:19:53,760 of integers in increasing order. And sometimes\xa0\n 1828 03:19:53,760 --> 03:19:59,840 value of integers. So what all can I do? Think\xa0\n 1829 03:20:00,398 --> 03:20:06,478 To start functions, the first sort function\xa0\n 1830 03:20:06,478 --> 03:20:12,638 function to sort in decreasing order. But if we\xa0\n 1831 03:20:12,639 --> 03:20:18,319 the two functions will be same, except\xa0\n 1832 03:20:18,959 --> 03:20:24,879 or greater than sign. Writing duplicate code has\xa0\n 1833 03:20:24,879 --> 03:20:30,799 better. Another thing that we can do is we can\xa0\n 1834 03:20:30,799 --> 03:20:35,599 say whether we want the list sorted in increasing\xa0\n 1835 03:20:36,159 --> 03:20:41,840 let's say when flag is one, we will start in\xa0\n 1836 03:20:41,840 --> 03:20:47,120 start in decreasing order. Using this approach,\xa0\n 1837 03:20:48,318 --> 03:20:52,959 What I'm going to do is I'm going to use\xa0\n 1838 03:20:52,959 --> 03:20:58,238 always done on basis of some ranking mechanism.\xa0\n 1839 03:20:58,238 --> 03:21:03,679 compare elements and say that they should come\xa0\n 1840 03:21:03,680 --> 03:21:10,239 logic in sorting will always be the same. Only the\xa0\n 1841 03:21:10,238 --> 03:21:16,638 What I'm going to do is I'm going to decide which\xa0\n 1842 03:21:16,639 --> 03:21:21,279 lesser through a function, basically, I will\xa0\n 1843 03:21:21,920 --> 03:21:27,120 my function will take a function pointer as\xa0\n 1844 03:21:27,120 --> 03:21:33,040 pointer as argument, the callback function, or\xa0\n 1845 03:21:33,040 --> 03:21:39,920 must take two integers as argument, it should\xa0\n 1846 03:21:39,920 --> 03:21:45,760 return back an integer. It should return\xa0\n 1847 03:21:46,639 --> 03:21:51,840 zero if the elements are equal, and minus\xa0\n 1848 03:21:52,559 --> 03:21:58,799 has higher rank. And let's say the element\xa0\n 1849 03:21:58,799 --> 03:22:06,319 in sorted array towards higher indices. These are\xa0\n 1850 03:22:06,318 --> 03:22:13,840 Now I'm going to use my callback function for\xa0\n 1851 03:22:13,840 --> 03:22:20,159 as arguments to this callback function, and\xa0\n 1852 03:22:20,959 --> 03:22:27,278 we will swap in this condition because AJ will\xa0\n 1853 03:22:27,279 --> 03:22:35,040 function to return one. So we will try to push\xa0\n 1854 03:22:35,040 --> 03:22:40,399 this particular implementation of bubblesort,\xa0\n 1855 03:22:40,398 --> 03:22:46,798 I have written a function like this. Now in main,\xa0\n 1856 03:22:46,799 --> 03:22:54,159 return me a function pointer and pass it in this\xa0\n 1857 03:22:54,159 --> 03:23:00,159 I have not written a statement to return zero if\xa0\n 1858 03:23:00,159 --> 03:23:06,799 logic, so far equality also I will return minus\xa0\n 1859 03:23:06,799 --> 03:23:12,639 output. This is what I'm getting the numbers\xa0\n 1860 03:23:12,639 --> 03:23:20,079 change the comparison logic a bit, I will return\xa0\n 1861 03:23:20,079 --> 03:23:25,680 I have changed the ranking mechanism, the\xa0\n 1862 03:23:25,680 --> 03:23:30,319 and will go towards the end of the array. This\xa0\n 1863 03:23:30,879 --> 03:23:36,799 The array is now sorted in decreasing order of\xa0\n 1864 03:23:36,799 --> 03:23:44,238 design because now our sort function can sort a\xa0\n 1865 03:23:44,238 --> 03:23:49,039 we can have one callback function for each\xa0\n 1866 03:23:49,040 --> 03:23:55,519 example. Let's say we have a list of integers\xa0\n 1867 03:23:55,520 --> 03:24:02,720 I have modified a here a now has both positive\xa0\n 1868 03:24:02,719 --> 03:24:10,319 array in increasing order of the absolute value\xa0\n 1869 03:24:10,318 --> 03:24:18,000 we will just take a mod and then compare\xa0\n 1870 03:24:18,000 --> 03:24:23,920 this. To be able to sort I will have to write a\xa0\n 1871 03:24:23,920 --> 03:24:28,559 the same compare function, but actually we\xa0\n 1872 03:24:28,559 --> 03:24:35,359 for each sorting scenario. So I will write a new\xa0\n 1873 03:24:35,359 --> 03:24:42,159 named absolute compare. I have included a\xa0\n 1874 03:24:42,159 --> 03:24:48,799 a BS function from this library that will give me\xa0\n 1875 03:24:48,799 --> 03:24:56,399 a is greater than absolute value of b, then it's\xa0\n 1876 03:24:56,398 --> 03:25:03,358 return minus one. Let's now use this function to\xa0\n 1877 03:25:04,079 --> 03:25:09,840 Okay, instead of passing this compare function,\xa0\n 1878 03:25:09,840 --> 03:25:14,960 to bubblesort. This basically is passing\xa0\n 1879 03:25:15,600 --> 03:25:20,800 Let's now run this program and see what happens.\xa0\n 1880 03:25:20,799 --> 03:25:26,959 in increasing order of their absolute values.\xa0\n 1881 03:25:26,959 --> 03:25:34,000 array of integers, but we have a library function\xa0\n 1882 03:25:34,639 --> 03:25:41,599 This library function is in STD\xa0\n 1883 03:25:41,600 --> 03:25:48,960 q SOT q SOT for quicksort and to this function\xa0\n 1884 03:25:48,959 --> 03:25:56,000 it can be an array of integers or it can be an\xa0\n 1885 03:25:56,000 --> 03:26:01,200 structured first argument will be the array second\xa0\n 1886 03:26:01,200 --> 03:26:07,519 third argument will be the size of the data type.\xa0\n 1887 03:26:08,079 --> 03:26:14,478 size of int, size of data type in bytes size\xa0\n 1888 03:26:14,478 --> 03:26:22,079 bytes. last argument should be a function pointer\xa0\n 1889 03:26:22,079 --> 03:26:29,039 of the comparison function should be like this, it\xa0\n 1890 03:26:29,040 --> 03:26:36,399 and return an integer. Why word pointers? What\xa0\n 1891 03:26:36,398 --> 03:26:42,318 them to a pointer of any data type. And this\xa0\n 1892 03:26:42,318 --> 03:26:46,798 it should be passed a pointer to such a\xa0\n 1893 03:26:46,799 --> 03:26:53,679 back. Let's write the comparison function. You\xa0\n 1894 03:26:54,238 --> 03:27:01,760 passed as a void pointer to get the element.\xa0\n 1895 03:27:02,799 --> 03:27:10,079 typecast the void pointer to int pointer. And\xa0\n 1896 03:27:10,079 --> 03:27:15,920 dereference and get the value, we will do the same\xa0\n 1897 03:27:15,920 --> 03:27:24,238 must return any positive integer, if A is ranked\xa0\n 1898 03:27:24,799 --> 03:27:31,599 and zero if both are ranked same, we can simply\xa0\n 1899 03:27:31,600 --> 03:27:37,680 A is higher in value, A minus B will be positive.\xa0\n 1900 03:27:37,680 --> 03:27:42,639 function here that would rank and integer with\xa0\n 1901 03:27:42,639 --> 03:27:48,000 function can be used to sort the array in\xa0\n 1902 03:27:48,000 --> 03:27:53,120 like I said, returning positive value means a\xa0\n 1903 03:27:53,760 --> 03:27:58,719 B is ranked higher with this comparison\xa0\n 1904 03:27:58,719 --> 03:28:05,119 Q sort and after the call to cusat print the\xa0\n 1905 03:28:05,680 --> 03:28:10,159 as you can see the list is sorted in\xa0\n 1906 03:28:10,159 --> 03:28:16,799 If I will change this comparison function to\xa0\n 1907 03:28:16,799 --> 03:28:22,559 integer with lesser value will be ranked higher,\xa0\n 1908 03:28:22,559 --> 03:28:28,719 value of integers. And if I will use abstract\xa0\n 1909 03:28:29,279 --> 03:28:35,599 this is what I will get. Remember, in this\xa0\n 1910 03:28:35,600 --> 03:28:43,840 compared are being passed through reference, their\xa0\n 1911 03:28:43,840 --> 03:28:49,840 const keyword here means you cannot modify the\xa0\n 1912 03:28:49,840 --> 03:28:56,719 to use void pointer because of generic design of Q\xa0\n 1913 03:28:56,719 --> 03:29:03,039 not just an integer array, it's just that you\xa0\n 1914 03:29:03,040 --> 03:29:10,080 we just discussed one of the use cases of function\xa0\n 1915 03:29:10,079 --> 03:29:15,920 a lot of interesting design scenarios. One more\xa0\n 1916 03:29:15,920 --> 03:29:21,680 life easier is something called event handling. If\xa0\n 1917 03:29:21,680 --> 03:29:27,840 this video for some resources on event handling.\xa0\n 1918 03:29:28,719 --> 03:29:33,199 In our previous lessons, we have learned\xa0\n 1919 03:29:33,200 --> 03:29:39,040 we have understood what is stack and what is\xa0\n 1920 03:29:39,040 --> 03:29:45,600 we are going to talk about one situation which is\xa0\n 1921 03:29:45,600 --> 03:29:51,840 memory on the heap and this situation is memory\xa0\n 1922 03:29:51,840 --> 03:29:57,680 we have discussed in our previous lessons. The\xa0\n 1923 03:29:57,680 --> 03:30:03,280 of a program or what we can also Call\xa0\n 1924 03:30:03,279 --> 03:30:10,079 into these four segments or these four sections.\xa0\n 1925 03:30:10,079 --> 03:30:15,840 that need to be executed. This section is\xa0\n 1926 03:30:15,840 --> 03:30:21,200 Another section is used to store the global\xa0\n 1927 03:30:21,200 --> 03:30:27,120 inside functions and have lifetime of the whole\xa0\n 1928 03:30:27,120 --> 03:30:33,920 used to execute the function calls and store all\xa0\n 1929 03:30:34,639 --> 03:30:40,000 The size of these three segments that code segment\xa0\n 1930 03:30:40,000 --> 03:30:46,879 are fixed and decided when the program is\xa0\n 1931 03:30:46,879 --> 03:30:54,479 fourth section, which is called heap, or dynamic\xa0\n 1932 03:30:55,200 --> 03:31:02,639 as per need. As we know, we get memory from the\xa0\n 1933 03:31:03,200 --> 03:31:08,479 And when we are done using that memory on the\xa0\n 1934 03:31:08,478 --> 03:31:14,238 deallocate or free that particular memory. In\xa0\n 1935 03:31:15,200 --> 03:31:20,960 the operator new to get some memory and the\xa0\n 1936 03:31:20,959 --> 03:31:27,119 leak is a situation when we get some memory\xa0\n 1937 03:31:27,120 --> 03:31:34,000 done using it. So our application is actually\xa0\n 1938 03:31:34,000 --> 03:31:40,398 But why do we call this situation memory leak? And\xa0\n 1939 03:31:40,398 --> 03:31:46,478 memory only due to improper use of heap only and\xa0\n 1940 03:31:46,478 --> 03:31:50,958 we will try to understand this through\xa0\n 1941 03:31:51,520 --> 03:31:57,280 I'll write one simple program and show the\xa0\n 1942 03:31:57,279 --> 03:32:02,719 To explain these concepts. In my C program here\xa0\n 1943 03:32:03,439 --> 03:32:10,398 And the game is that we have three positions\xa0\n 1944 03:32:10,398 --> 03:32:15,599 jack is at the first position key queen is at\xa0\n 1945 03:32:15,600 --> 03:32:24,800 position. And then computer shuffles these cards\xa0\n 1946 03:32:24,799 --> 03:32:32,639 is randomized. And now the player has to guess the\xa0\n 1947 03:32:32,639 --> 03:32:39,359 of money. Let's say it's virtual cash. And if he\xa0\n 1948 03:32:39,359 --> 03:32:44,639 the position of Queen correctly, he takes away\xa0\n 1949 03:32:44,639 --> 03:32:52,239 if he loses, he simply loses the bet amount. Let's\xa0\n 1950 03:32:52,238 --> 03:32:59,119 and he can play as many times as he wants until\xa0\n 1951 03:32:59,120 --> 03:33:04,479 this game. The first thing that I want to do\xa0\n 1952 03:33:04,478 --> 03:33:11,918 cash that at any point will store the virtual cash\xa0\n 1953 03:33:11,920 --> 03:33:17,600 virtual cash. And then in the main method, I will\xa0\n 1954 03:33:17,600 --> 03:33:24,161 code something like this. While cash is greater\xa0\n 1955 03:33:25,040 --> 03:33:33,040 we will ask him to bet something and we will take\xa0\n 1956 03:33:33,040 --> 03:33:39,200 negative scenarios like bet equals zero or bad\xa0\n 1957 03:33:39,200 --> 03:33:43,920 we will break out of this loop we will end our\xa0\n 1958 03:33:43,920 --> 03:33:48,479 make a call to play function. And of course,\xa0\n 1959 03:33:49,439 --> 03:33:56,559 we will be passing bet to the play function. So\xa0\n 1960 03:33:56,559 --> 03:34:00,879 function I will declare a character\xa0\n 1961 03:34:02,000 --> 03:34:08,478 we will have the character j at the first position\xa0\n 1962 03:34:08,478 --> 03:34:14,318 J is for jack and similarly Q is for Queen\xa0\n 1963 03:34:14,318 --> 03:34:19,359 perform a randomized shuffling. So we will write\xa0\n 1964 03:34:19,359 --> 03:34:25,200 a logic to make a random shuffle of cards. One\xa0\n 1965 03:34:25,200 --> 03:34:31,040 number generator function in C to use the random\xa0\n 1966 03:34:31,600 --> 03:34:38,161 RAND function and pass it an argument something\xa0\n 1967 03:34:38,159 --> 03:34:44,478 in a strand by making a call to a strand. Now\xa0\n 1968 03:34:44,478 --> 03:34:51,278 choose any two random positions among 01 and two\xa0\n 1969 03:34:51,279 --> 03:34:56,399 array. Let's say these positions are x and y. Now\xa0\n 1970 03:34:56,398 --> 03:35:02,798 a random number but we want a number between Zero\xa0\n 1971 03:35:02,799 --> 03:35:09,840 modulo by three so that we either get zero or one\xa0\n 1972 03:35:10,879 --> 03:35:17,679 with statements like this. And now, what we can do\xa0\n 1973 03:35:17,680 --> 03:35:24,639 So we will use a temporary variable First, we\xa0\n 1974 03:35:24,639 --> 03:35:30,239 and then we will do something like this. I'm\xa0\n 1975 03:35:30,879 --> 03:35:34,399 statements in the same line.\xa0\n 1976 03:35:37,120 --> 03:35:44,720 So we are swapping or shuffling characters or two\xa0\n 1977 03:35:44,719 --> 03:35:51,679 two positions x and y are randomly by making call\xa0\n 1978 03:35:51,680 --> 03:35:57,520 So this will guarantee us that x and y is between\xa0\n 1979 03:35:58,238 --> 03:36:05,439 by making a call to RAND function, we will\xa0\n 1980 03:36:05,439 --> 03:36:14,799 Rand once and pass this time null, the return\xa0\n 1981 03:36:14,799 --> 03:36:20,639 there is something there is one concept of seeding\xa0\n 1982 03:36:20,639 --> 03:36:25,279 seed to random number generator, we will not go\xa0\n 1983 03:36:25,279 --> 03:36:32,399 Now what I'll do in the proof play function is\xa0\n 1984 03:36:32,398 --> 03:36:41,119 I'll ask the player to guess the position of Queen\xa0\n 1985 03:36:41,120 --> 03:36:48,319 if the player is correct, then the character at\xa0\n 1986 03:36:48,318 --> 03:36:54,159 and the position will be one minus what the player\xa0\n 1987 03:36:54,159 --> 03:37:00,398 two or three that will map to 01 and two in the\xa0\n 1988 03:37:01,040 --> 03:37:09,760 So his overall cash will be incremented by three\xa0\n 1989 03:37:09,760 --> 03:37:14,960 cache will be decremented by the bet amount. So in\xa0\n 1990 03:37:14,959 --> 03:37:20,879 like this, we will say that you have one and the\xa0\n 1991 03:37:20,879 --> 03:37:26,479 this. Remember, cash is a global variable. And we\xa0\n 1992 03:37:27,760 --> 03:37:33,840 Finally, our play function is looking something\xa0\n 1993 03:37:33,840 --> 03:37:38,799 cash at the top. In the main method, I've\xa0\n 1994 03:37:39,439 --> 03:37:45,439 Let's now play this game and see what happens.\xa0\n 1995 03:37:45,439 --> 03:37:52,000 and created an executable named game dot txt. And\xa0\n 1996 03:37:52,000 --> 03:38:01,040 asking, what's your bet. Let's say we want to bet\xa0\n 1997 03:38:01,040 --> 03:38:08,479 $95. Let's bet again, this time again, I lose. And\xa0\n 1998 03:38:11,600 --> 03:38:15,680 Finally, a win after long time, I can go\xa0\n 1999 03:38:15,680 --> 03:38:22,639 you something else, I have opened the task\xa0\n 2000 03:38:22,639 --> 03:38:28,799 this highlighted row is for game dot e xe. The\xa0\n 2001 03:38:28,799 --> 03:38:35,840 executable game dot e xe you will have to see\xa0\n 2002 03:38:36,799 --> 03:38:42,879 the memory consumption here, the memory that\xa0\n 2003 03:38:44,318 --> 03:38:48,879 Now I'll go ahead and make some changes\xa0\n 2004 03:38:48,879 --> 03:38:54,318 this particular character array that I'm creating\xa0\n 2005 03:38:54,318 --> 03:39:00,638 now it's created as a local variable, so it will\xa0\n 2006 03:39:00,639 --> 03:39:06,479 array on the heap. So what I'll do is, I'll write\xa0\n 2007 03:39:06,478 --> 03:39:13,918 pointer named C and then I'll use malloc. To\xa0\n 2008 03:39:15,439 --> 03:39:21,519 In c++, we could have said something like this,\xa0\n 2009 03:39:21,520 --> 03:39:30,880 set a value at z zero at index as j, one at index\xa0\n 2010 03:39:30,879 --> 03:39:38,879 array on the heap and c is a pointer to the base\xa0\n 2011 03:39:38,879 --> 03:39:45,519 but now it is a pointer to character and we can\xa0\n 2012 03:39:45,520 --> 03:39:50,800 will just work fine. Let us run this code and see\xa0\n 2013 03:39:50,799 --> 03:39:57,439 I'm running the executable. Watch for the memory\xa0\n 2014 03:39:59,600 --> 03:40:05,121 as you You can see right now the memory\xa0\n 2015 03:40:05,120 --> 03:40:12,239 after some time the memory consumption is 480 8k,\xa0\n 2016 03:40:12,238 --> 03:40:17,359 game further, after some time it will shoot up\xa0\n 2017 03:40:17,359 --> 03:40:23,199 up for sure. So why is it happening, or why was it\xa0\n 2018 03:40:23,199 --> 03:40:28,559 not on the stack, not on the heap and it was on\xa0\n 2019 03:40:28,559 --> 03:40:34,161 sections of applications memory here. And let us\xa0\n 2020 03:40:34,159 --> 03:40:39,279 are playing our game. As we know from our previous\xa0\n 2021 03:40:39,279 --> 03:40:44,399 execution goes into the stack section of\xa0\n 2022 03:40:44,959 --> 03:40:50,000 some amount of memory from the stack is allocated\xa0\n 2023 03:40:50,000 --> 03:40:54,959 executing. First, the main method is invoked or\xa0\n 2024 03:40:54,959 --> 03:41:01,119 allocated for the execution of Maine. This is what\xa0\n 2025 03:41:01,120 --> 03:41:06,880 stack frame of Maine and all the local variables\xa0\n 2026 03:41:06,879 --> 03:41:12,879 code we had a local variable named bet, so it will\xa0\n 2027 03:41:13,439 --> 03:41:19,359 So initially, let's say main function is executing\xa0\n 2028 03:41:19,359 --> 03:41:24,159 calls to play function. And what really happens\xa0\n 2029 03:41:24,159 --> 03:41:28,799 is that that particular function is paused\xa0\n 2030 03:41:29,359 --> 03:41:35,120 the called function. So main will pause and play\xa0\n 2031 03:41:35,120 --> 03:41:40,239 main in the stack. Now, we had a couple of local\xa0\n 2032 03:41:40,799 --> 03:41:47,278 i x y players guess and for the case\xa0\n 2033 03:41:47,279 --> 03:41:54,639 on the stack itself, so it was not created using\xa0\n 2034 03:41:54,639 --> 03:41:59,920 sit in this stack frame. Now when the execution\xa0\n 2035 03:41:59,920 --> 03:42:04,960 back to main and the memory that was allocated for\xa0\n 2036 03:42:05,600 --> 03:42:10,720 Anytime a function call finishes, the memory\xa0\n 2037 03:42:10,719 --> 03:42:16,000 So there is one stack frame corresponding to\xa0\n 2038 03:42:16,000 --> 03:42:21,600 that memory is claimed back. Now main will make\xa0\n 2039 03:42:21,600 --> 03:42:26,960 rounds. So play will again come into the stack\xa0\n 2040 03:42:26,959 --> 03:42:31,599 As you can see, all the local variables get\xa0\n 2041 03:42:31,600 --> 03:42:36,880 For anything on the stack, we do not have to worry\xa0\n 2042 03:42:36,879 --> 03:42:41,198 when the function call finishes. Now let's talk\xa0\n 2043 03:42:41,199 --> 03:42:46,880 created on the heap using a call to malloc\xa0\n 2044 03:42:46,879 --> 03:42:54,479 to play function. Now, what will happen this time\xa0\n 2045 03:42:54,478 --> 03:43:00,079 we will still have a variable named c a local\xa0\n 2046 03:43:00,079 --> 03:43:06,159 be of type character array of size three, this\xa0\n 2047 03:43:06,159 --> 03:43:11,039 we will make a call to malloc function to create\xa0\n 2048 03:43:11,040 --> 03:43:16,239 which is a pointer to character will only point\xa0\n 2049 03:43:16,238 --> 03:43:22,159 on the heap has to be accessed through a pointer\xa0\n 2050 03:43:22,159 --> 03:43:27,599 the heap and we have kept only a pointer variable\xa0\n 2051 03:43:27,600 --> 03:43:34,239 will finish the memory allocated for the execution\xa0\n 2052 03:43:34,238 --> 03:43:41,439 the local variables will go away. But this memory\xa0\n 2053 03:43:41,439 --> 03:43:46,960 it will not get d allocated anything on the heap\xa0\n 2054 03:43:46,959 --> 03:43:52,799 to free function or by using delete operator and\xa0\n 2055 03:43:52,799 --> 03:43:59,359 function as we play multiple rounds of our game.\xa0\n 2056 03:43:59,359 --> 03:44:06,000 one such memory block on the heap that will lie\xa0\n 2057 03:44:06,000 --> 03:44:13,439 will finish. If we will play our game 100 rounds\xa0\n 2058 03:44:13,439 --> 03:44:19,359 unused memory blocks of three characters and\xa0\n 2059 03:44:19,359 --> 03:44:25,759 application can claim can get more memory in\xa0\n 2060 03:44:25,760 --> 03:44:31,280 is not running out of memory. And if we are not\xa0\n 2061 03:44:31,279 --> 03:44:37,040 depleting and wasting memory which is an important\xa0\n 2062 03:44:37,040 --> 03:44:42,399 only keep on growing with time. memory leaks\xa0\n 2063 03:44:43,199 --> 03:44:49,599 Anything unused and unreferenced on the heap\xa0\n 2064 03:44:49,600 --> 03:44:55,040 as programmers that garbage is not created on the\xa0\n 2065 03:44:55,040 --> 03:45:00,640 in the heap. in languages like Java and C sharp\xa0\n 2066 03:45:00,639 --> 03:45:05,199 so a programmer does not have to worry about\xa0\n 2067 03:45:05,199 --> 03:45:10,239 which is a cool feature to have. It avoids memory\xa0\n 2068 03:45:10,238 --> 03:45:15,198 of three characters on the heap. What if we were\xa0\n 2069 03:45:15,199 --> 03:45:19,840 freeing freeing the memory after we were done\xa0\n 2070 03:45:19,840 --> 03:45:25,440 consumption would have shot up like anything.\xa0\n 2071 03:45:25,439 --> 03:45:32,879 I have created a character array of size 10,000 of\xa0\n 2072 03:45:32,879 --> 03:45:38,959 I'll just use first three positions in the array.\xa0\n 2073 03:45:38,959 --> 03:45:46,238 end of this particular function, when we are done\xa0\n 2074 03:45:46,238 --> 03:45:52,558 to the free function, passing it the address of\xa0\n 2075 03:45:52,559 --> 03:45:57,760 will just work like before. But let's run this\xa0\n 2076 03:45:57,760 --> 03:46:03,120 Once again, I'm showing you the task manager\xa0\n 2077 03:46:03,920 --> 03:46:11,120 Now watch out the memory consumption of game dot\xa0\n 2078 03:46:11,120 --> 03:46:18,880 the memory consumption will shoot up. As you can\xa0\n 2079 03:46:18,879 --> 03:46:25,759 even after playing for a long time. And\xa0\n 2080 03:46:25,760 --> 03:46:32,960 free to D allocate the memory when we were done\xa0\n 2081 03:46:32,959 --> 03:46:39,358 we had created an array of size 10,000. And if\xa0\n 2082 03:46:39,359 --> 03:46:44,799 shot up like anything the memory consumption\xa0\n 2083 03:46:44,799 --> 03:46:50,639 we are freeing at the end of the function, it's\xa0\n 2084 03:46:50,639 --> 03:46:57,039 to summarize it, memory leak is improper use\xa0\n 2085 03:46:57,040 --> 03:47:02,960 that causes the memory consumption of our program\xa0\n 2086 03:47:02,959 --> 03:47:08,879 memory leak always happens because of unused and\xa0\n 2087 03:47:08,879 --> 03:47:14,318 on the stack is de allocated automatically\xa0\n 2088 03:47:14,318 --> 03:47:22,238 most we can have an overflow in stack. So this\xa0\n 184192

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