1
00:00:02,260 --> 00:00:02,860
Hello everyone.

2
00:00:02,860 --> 00:00:08,220
So in this lesson we will learn dynamic allocation of 2D eddies.

3
00:00:08,260 --> 00:00:12,760
So first of all how we will create one be added dynamically.

4
00:00:12,760 --> 00:00:19,380
So I will write you end and five simple but I want to create the edit.

5
00:00:19,390 --> 00:00:26,540
So you might be thinking this index will be new and let's say 10 rows and columns.

6
00:00:26,920 --> 00:00:29,810
But this index is not right.

7
00:00:29,840 --> 00:00:33,650
This is not the way we create two data dynamically.

8
00:00:33,750 --> 00:00:35,570
So what do we want to do differently.

9
00:00:35,580 --> 00:00:41,170
So we want 10 rules should be there and 20 columns should be there.

10
00:00:41,190 --> 00:00:44,450
So this is our requirement that 10 rows and 20 columns.

11
00:00:44,460 --> 00:00:48,020
So basically we can say that our identities.

12
00:00:48,150 --> 00:00:54,360
So ultimately we can say what the studio is or it is just a combination of areas.

13
00:00:54,570 --> 00:00:59,050
So I can't say there are 10 it is and each of size 20.

14
00:00:59,140 --> 00:01:00,580
So let's call it even.

15
00:01:00,850 --> 00:01:02,480
And this is it then.

16
00:01:02,590 --> 00:01:05,290
So there are identities and each of size 20.

17
00:01:06,670 --> 00:01:08,290
So what I want to do.

18
00:01:08,290 --> 00:01:13,130
So to create just one Eddie to clear just one area.

19
00:01:13,160 --> 00:01:13,920
What I have to lose.

20
00:01:13,960 --> 00:01:15,410
I will use this index.

21
00:01:15,490 --> 00:01:20,370
So if I will write new into 20 because I want my area of size 20.

22
00:01:20,500 --> 00:01:24,850
So if I want to clear just one Eddie what this new thing will have done.

23
00:01:24,910 --> 00:01:27,790
So this will return me the address of the first element.

24
00:01:27,820 --> 00:01:35,380
So basically two older than me in Star similarly if I want to create an area of size 20 I will use this

25
00:01:35,410 --> 00:01:36,190
above index.

26
00:01:36,220 --> 00:01:39,940
So this is in star be new it and 20.

27
00:01:39,940 --> 00:01:41,910
I want 20 columns.

28
00:01:42,100 --> 00:01:44,580
Basically I want to create an array of size 20.

29
00:01:44,860 --> 00:01:48,190
So again it will have done me the element it within me that does with the first element.

30
00:01:48,190 --> 00:01:51,980
So it is going to return and start similarly.

31
00:01:51,980 --> 00:01:59,180
This is going to be done in start and similarly this is also going to have done me the first element

32
00:01:59,440 --> 00:02:01,120
that was the first element.

33
00:02:01,130 --> 00:02:03,960
So this is going to have done me in start.

34
00:02:04,310 --> 00:02:05,110
Correct.

35
00:02:05,110 --> 00:02:09,020
Now what I have to do so basically I have to store all these addresses

36
00:02:12,130 --> 00:02:15,170
if you want to create a truly array and so you have to store all that.

37
00:02:15,180 --> 00:02:16,830
This is now to store all that.

38
00:02:16,830 --> 00:02:21,380
This is where to do so I will create an 80 often size 10.

39
00:02:21,630 --> 00:02:23,960
And each element will be installed.

40
00:02:24,120 --> 00:02:26,990
So each element will be in in integer point.

41
00:02:27,000 --> 00:02:29,550
So I'm going to create an area of integer pointer.

42
00:02:30,450 --> 00:02:32,490
So this is very often disappointed.

43
00:02:32,490 --> 00:02:34,800
And the size is going to be then.

44
00:02:34,860 --> 00:02:37,440
So how do you create an area of in digits.

45
00:02:37,440 --> 00:02:39,170
So first you write to do the type.

46
00:02:39,240 --> 00:02:47,110
So in our case that it diapers in Star that it debuts in Star then we type start.

47
00:02:47,210 --> 00:02:52,490
So start then we write be basically the name then we write to do then the data type.

48
00:02:52,490 --> 00:02:54,810
So in this case I'm creating additive in Tejas.

49
00:02:54,830 --> 00:03:00,560
But here I want to create Adi often being appointed so I will write new in Star data type.

50
00:03:00,560 --> 00:03:02,280
So this is not easy to type.

51
00:03:02,600 --> 00:03:10,680
And then the size is so I want to create an area of size 10 so this way if we will use this approach

52
00:03:10,920 --> 00:03:13,610
I will be able to create this Eddie.

53
00:03:13,680 --> 00:03:20,070
Now after doing this we have to create all these eddies we have to create all these areas.

54
00:03:20,080 --> 00:03:27,490
So can I write b 0 is new into 20.

55
00:03:27,850 --> 00:03:36,830
Similarly I will write B1 is new into 20s and this will go on at the last.

56
00:03:36,830 --> 00:03:41,990
I will write B nine is new end and 20.

57
00:03:42,950 --> 00:03:44,720
So these are all in beta ready.

58
00:03:44,720 --> 00:03:52,400
So these are all in D ready so that's why I have to do something like this and it is obviously that

59
00:03:52,430 --> 00:03:58,250
I will not write like this I will obviously use a loop so I will just write data what this Eddie and

60
00:03:58,370 --> 00:04:08,520
I will use this I will write this simple now after doing this for example if you want to access to for

61
00:04:08,520 --> 00:04:13,640
example after doing all the hard work if you want to access be a four and five.

62
00:04:13,800 --> 00:04:14,880
So what is our structure.

63
00:04:14,910 --> 00:04:22,460
So I was structure is this is an area of integer pointer this is in a different digit pointer.

64
00:04:22,490 --> 00:04:30,600
This is you going to be your b's binding towards the first element and for each index you have an 80

65
00:04:31,340 --> 00:04:31,780
so.

66
00:04:31,910 --> 00:04:38,120
So each index is storing the address of the first element of its Eddy.

67
00:04:38,220 --> 00:04:45,600
So you want go to photo so I will go to photo it will go to photo you will be able to find an array

68
00:04:46,560 --> 00:04:48,330
and then go to the fifth index.

69
00:04:48,390 --> 00:04:50,610
So you will go to the fifth index.

70
00:04:50,730 --> 00:04:59,410
Simple so finally what is my situation.

71
00:04:59,450 --> 00:05:01,490
So this is you're in Deja appointed

72
00:05:04,230 --> 00:05:09,060
and each index is distorting and address affinity

73
00:05:11,680 --> 00:05:13,900
each index is storing the address of vanity

74
00:05:16,740 --> 00:05:26,360
and all this work is basically all the memory is basically the heap memory so all this is heap.

75
00:05:26,510 --> 00:05:33,400
So what is stack memory so in stack memory what do we have that double point.

76
00:05:33,430 --> 00:05:40,480
So this is B since you're disappointed it really be of a debate and it is playing towards it distorting

77
00:05:40,480 --> 00:05:42,580
the address of the first element.

78
00:05:43,510 --> 00:05:46,780
So this is the structure and what is the goal.

79
00:05:46,810 --> 00:05:51,840
So first of all we have to create this integer ready so for creating this and B the address index is

80
00:05:51,840 --> 00:05:52,410
very simple.

81
00:05:52,410 --> 00:05:56,800
This will be an area of integer point and so forth that is the type that I believe this one.

82
00:05:56,880 --> 00:06:02,530
Then you have the right start operator because you want to store the address then the name of the area

83
00:06:02,550 --> 00:06:04,050
and let's say B.

84
00:06:04,140 --> 00:06:07,210
So this is new data diapers in Deja 2.0.

85
00:06:07,230 --> 00:06:11,970
So each element is an integer point that because it is going to store the index of the first element

86
00:06:12,780 --> 00:06:17,520
and let's say I want to create any of size 10.

87
00:06:17,700 --> 00:06:20,700
Similarly what we have to do so you have to hydrate or what this salary.

88
00:06:20,760 --> 00:06:22,820
So let s I read or disagree.

89
00:06:23,030 --> 00:06:27,340
So I equals zero i.e. less than 10 a plus plus.

90
00:06:27,610 --> 00:06:28,410
What are you going to do.

91
00:06:28,500 --> 00:06:31,470
You have to create this wouldn't be added.

92
00:06:31,470 --> 00:06:37,360
So for creating this one lira you will write B of A is new.

93
00:06:38,200 --> 00:06:40,220
And 20.

94
00:06:40,250 --> 00:06:42,660
So finally if you will do this.

95
00:06:43,280 --> 00:06:44,390
What will happen.

96
00:06:44,390 --> 00:06:52,200
This is seem as if you want to lose technical locations or this is B of 10 and 20.

97
00:06:52,420 --> 00:06:57,200
Okay so this is how we allocate to the area dynamically.

98
00:06:57,610 --> 00:07:03,560
So let us write the code.

99
00:07:04,130 --> 00:07:07,960
So first of all I need to add a often digit point.

100
00:07:08,000 --> 00:07:14,230
So now I started being w pointer Then new in start and led to the size is 10.

101
00:07:14,320 --> 00:07:21,620
So I want to create an eddy of 10 rows and 20 columns to the area of 10 those and 20 columns.

102
00:07:21,630 --> 00:07:25,290
Now I just have to wait wait i equals zero.

103
00:07:25,410 --> 00:07:36,650
I list then then I bless less and now be off I is new top 20.

104
00:07:36,690 --> 00:07:44,240
So I've created an array of vendors and 20 columns to the ad often losing 20 columns.

105
00:07:44,250 --> 00:07:48,990
So for example if I want to take the rows and columns input from the user so you can definitely do like

106
00:07:48,990 --> 00:07:52,110
this in and my nine rows and columns.

107
00:07:52,110 --> 00:07:53,460
So give me the value of those.

108
00:07:53,510 --> 00:07:55,080
Give me the value of columns.

109
00:07:55,140 --> 00:07:57,450
So after doing this don't happen.

110
00:07:57,450 --> 00:08:04,300
So this will be your m. you will I trade on the middle floors and this is your columns.

111
00:08:04,530 --> 00:08:05,700
Simple.

112
00:08:05,700 --> 00:08:07,940
Now after doing this you want to take input.

113
00:08:08,050 --> 00:08:19,530
So for taking the input we remembered i equals zero 9 less than name I plus press then similarly j equals

114
00:08:19,530 --> 00:08:22,030
zero g list and energy plus plus.

115
00:08:22,530 --> 00:08:29,330
And you will simply write B of A G.

116
00:08:29,370 --> 00:08:36,180
So this is how we will take input or instead of writing this whole code what you can do so you can write

117
00:08:36,190 --> 00:08:36,850
here only.

118
00:08:36,870 --> 00:08:38,520
So let us comment out this one

119
00:08:49,710 --> 00:08:50,560
so you can write.

120
00:08:50,580 --> 00:08:57,870
You can likely take input here only I do to all those that is so lightweight I am editing over those

121
00:08:57,870 --> 00:09:00,780
add is that we have created in the above line.

122
00:09:01,030 --> 00:09:04,380
So I will take you off AIG simple.

123
00:09:04,480 --> 00:09:07,030
So what is happening here.

124
00:09:07,180 --> 00:09:09,360
Okay so what is the meaning of this line.

125
00:09:09,390 --> 00:09:11,600
So you're creating an additive in disappointed

126
00:09:15,790 --> 00:09:23,000
so the sizes M so each and B's an integer point and then what they're doing you are doing all this at

127
00:09:23,000 --> 00:09:25,150
a and b if I use new ink.

128
00:09:25,330 --> 00:09:30,420
And so this is why I'm thinking towards I am creating a new Eddy.

129
00:09:30,430 --> 00:09:35,140
So after creating the idea of size n I am I creating all this eddy.

130
00:09:35,170 --> 00:09:39,090
So I related to what this edit and I am taking input b if I j.

131
00:09:39,490 --> 00:09:46,780
Similarly you would again create entity of size and then you would like it or what do you say and you

132
00:09:46,780 --> 00:09:52,040
will dig the input then you will again create an area of size n then you will rotate over this idea

133
00:09:52,050 --> 00:09:54,990
and you will take input then you will again I refer to the.

134
00:09:55,180 --> 00:10:01,840
You will again create an area of size n and do related all the area and you will take input.

135
00:10:01,860 --> 00:10:02,800
Now one thing.

136
00:10:02,850 --> 00:10:04,200
So here what we are doing.

137
00:10:04,230 --> 00:10:07,100
So we are creating basically a matrix.

138
00:10:07,110 --> 00:10:09,620
So I will have 20 rows and 20 columns.

139
00:10:10,080 --> 00:10:15,210
So basically each row has same amount of columns so each row is having 20 columns.

140
00:10:15,210 --> 00:10:27,590
But we can do what we can do so we can create a structure like so we can do something like this.

141
00:10:27,620 --> 00:10:28,720
So what is the meaning.

142
00:10:28,720 --> 00:10:31,260
So the first rule is containing only one column.

143
00:10:31,430 --> 00:10:35,740
The second lowest containing only two columns or you can say it will contain only two element.

144
00:10:35,810 --> 00:10:38,060
The third row will contain three elements.

145
00:10:38,060 --> 00:10:44,210
Basically the site the number of columns would be three and the fourth column for total will contain

146
00:10:44,210 --> 00:10:44,860
four elements.

147
00:10:44,870 --> 00:10:52,230
And the fifth row will contain five elements so we can definitely create a structure like this.

148
00:10:52,290 --> 00:10:53,170
So basically what.

149
00:10:53,250 --> 00:10:56,180
What will happen to destructively become what do you do.

150
00:10:56,250 --> 00:10:59,420
So you have Ambrose.

151
00:10:59,490 --> 00:11:04,680
Here we are giving a fixed value and we are giving and let's say you will change the value.

152
00:11:04,680 --> 00:11:06,230
So it will change the value.

153
00:11:06,270 --> 00:11:09,600
So the first rule the size is 1.

154
00:11:09,790 --> 00:11:11,580
Now the size is 2.

155
00:11:11,950 --> 00:11:15,560
Now the size is 3 now the size is 4.

156
00:11:15,590 --> 00:11:19,710
So this is the power of dynamic allocation you can create a structure like this.

157
00:11:19,820 --> 00:11:22,210
So let's do this.

158
00:11:22,340 --> 00:11:28,220
So instead of n where you can do you can write a plus one.

159
00:11:28,250 --> 00:11:34,820
So if the value of i.e. zero the number of columns will become 1 if the value if I ever become when

160
00:11:35,030 --> 00:11:40,460
the number of columns will become 2 and we will obtain a structure like this.

161
00:11:40,460 --> 00:11:42,230
So I was to actively become like this

162
00:11:45,600 --> 00:11:46,700
but most of the time.

163
00:11:46,740 --> 00:11:47,920
What will happen.

164
00:11:48,020 --> 00:11:54,540
We will simply create this matrix only each and all will have seen on tough columns each row are going

165
00:11:54,540 --> 00:11:59,810
to have same number of columns but we can definitely do like this.

166
00:11:59,820 --> 00:12:03,260
There is nothing that can stop you from doing this.

167
00:12:03,360 --> 00:12:06,750
Okay so no one can stop you from doing this.

168
00:12:07,840 --> 00:12:10,210
Okay so what else is remaining.

169
00:12:10,210 --> 00:12:14,710
So I think we have used dynamic memory allocation and dynamic memory.

170
00:12:14,710 --> 00:12:18,100
V have to delete ourself manually deletion.

171
00:12:18,220 --> 00:12:19,180
Always remember.

172
00:12:19,300 --> 00:12:24,830
So just remember one thing whenever you are using new whenever you are using new.

173
00:12:24,970 --> 00:12:26,500
Always used delete.

174
00:12:26,500 --> 00:12:28,860
So we have to delete the memory out ourself.

175
00:12:29,140 --> 00:12:33,970
So what we're doing here we are creating IPG pointed at different digit points.

176
00:12:34,000 --> 00:12:35,350
So let's just delete.

177
00:12:36,130 --> 00:12:39,040
Let's just delete our diary so delete.

178
00:12:39,030 --> 00:12:44,440
Be if you will leave B do you think this is right.

179
00:12:44,440 --> 00:12:45,340
We did delete.

180
00:12:45,490 --> 00:12:46,850
I don't think so.

181
00:12:46,870 --> 00:12:47,480
Why.

182
00:12:47,500 --> 00:12:51,460
Because what is our structure our structure is first.

183
00:12:51,490 --> 00:12:55,180
We have an area of integer pointer.

184
00:12:55,290 --> 00:13:03,570
First we haven't had in often digital pointer and each and at each index I am storing the address of

185
00:13:03,570 --> 00:13:04,420
an 80.

186
00:13:04,500 --> 00:13:10,250
So each element of the integer point that is storing the address of an 80.

187
00:13:10,290 --> 00:13:11,260
So this is my stuff.

188
00:13:11,300 --> 00:13:12,140
Basically.

189
00:13:12,520 --> 00:13:18,450
Okay so this is in digital a pointer and these are these are a lot in Delia.

190
00:13:18,660 --> 00:13:28,340
So this whole thing is actually inside a heap this whole thing is actually inside the heap and inside

191
00:13:28,340 --> 00:13:30,830
stack we do have.

192
00:13:30,860 --> 00:13:34,090
So this is your stack memory inside stack.

193
00:13:34,100 --> 00:13:39,230
I have it by itself memory and this is B and Bs containing the address of the first element.

194
00:13:40,010 --> 00:13:46,190
So if this is the first element let's say it's addresses and so B's and storing hundred or against a

195
00:13:46,190 --> 00:13:52,590
piece pointing towards the the address of the 0 8 element fine.

196
00:13:52,660 --> 00:13:56,210
Now what I want to do so I want to delete this whole memory.

197
00:13:56,470 --> 00:14:03,380
So if you will write delete B if you will write delete B then what will happen.

198
00:14:03,400 --> 00:14:12,820
So basically this Eddie will get deleted this Eddie will get deleted and then you can not delete this

199
00:14:13,270 --> 00:14:18,370
why we can not delete these eddies because once you have deleted this memory.

200
00:14:18,370 --> 00:14:22,840
Now this memory does not belong to you and we discussed in the previous lesson and the previous we do

201
00:14:22,900 --> 00:14:28,810
that after deleting this you should not try to access that memory again because that memory now does

202
00:14:28,810 --> 00:14:30,310
not belong to you.

203
00:14:30,340 --> 00:14:31,500
So what we have to do.

204
00:14:31,690 --> 00:14:39,210
So first of all we have to delete these eddies we have to delete these areas and after deleting all

205
00:14:39,210 --> 00:14:41,710
that is we will delete this.

206
00:14:41,730 --> 00:14:43,070
Finally.

207
00:14:43,210 --> 00:14:44,750
Okay so what is our approach.

208
00:14:44,760 --> 00:14:50,790
So we know this is wrong because if you delete this big eddy then there is no way for us to delete these

209
00:14:50,850 --> 00:14:52,490
small eddies.

210
00:14:52,620 --> 00:14:56,010
So our logic is very simple our approach is very simple.

211
00:14:56,010 --> 00:15:02,640
First I tripped over this eddy and called the lead on these small small eddies after deleting all the

212
00:15:02,640 --> 00:15:03,080
errors.

213
00:15:03,120 --> 00:15:07,650
We will delete this big begetting simple so let's do this.

214
00:15:08,310 --> 00:15:13,260
So before deleting the big eddy before deleting the diesel point ready we have to hydrate and we have

215
00:15:13,260 --> 00:15:16,860
to delete small small eddies so I equals zero.

216
00:15:16,890 --> 00:15:18,040
We have to delete in B.J..

217
00:15:18,120 --> 00:15:19,860
So I call you two I left in him.

218
00:15:20,010 --> 00:15:21,750
I bless bless my dad.

219
00:15:21,780 --> 00:15:27,160
Do we have to call delete function delete be off.

220
00:15:27,920 --> 00:15:31,170
We are deleting small one that is since BFI.

221
00:15:31,250 --> 00:15:33,580
Would this be if I use a -- is an eddy.

222
00:15:33,590 --> 00:15:36,500
It does point towards entity since it does entity.

223
00:15:36,500 --> 00:15:38,220
I have to write like this.

224
00:15:38,220 --> 00:15:39,080
Now this is correct.

225
00:15:39,390 --> 00:15:43,150
OK you can give space or you do not give space.

226
00:15:43,160 --> 00:15:45,180
Both are correct.

227
00:15:45,200 --> 00:15:52,030
So first we have to delete all the individual areas and then we will delete our in D or appointed any.

228
00:15:52,070 --> 00:15:52,810
OK so.

229
00:15:52,940 --> 00:15:58,660
Do not forget to delete the memory which you have allocated using the dining memory location.

230
00:15:59,090 --> 00:16:00,790
So always remember this.

231
00:16:00,860 --> 00:16:06,370
This is a good practice so if you have any doubt in this lesson you can ask me.

232
00:16:06,380 --> 00:16:07,540
I will see you in the next video.

233
00:16:07,590 --> 00:16:07,960
Bye bye.
