1
00:00:01,140 --> 00:00:01,810
Hi everyone.

2
00:00:01,830 --> 00:00:06,240
In this lesson we are going to learn what is dynamic memory allocation.

3
00:00:06,360 --> 00:00:08,430
So let's start today's class.

4
00:00:08,580 --> 00:00:12,730
So let us discuss what is the biggest problem with the ADI.

5
00:00:12,900 --> 00:00:17,800
So whenever you want to create an entity you have to give the size.

6
00:00:17,820 --> 00:00:23,350
So let's say I want to create an area which will contain 15 digits at the max.

7
00:00:23,370 --> 00:00:25,560
So this 50 is actually a fixed number.

8
00:00:26,310 --> 00:00:30,690
So basically we are creating a fixed sized Eddie.

9
00:00:30,750 --> 00:00:42,360
Now we can also do like this in 10 seeing and and then into a and we can definitely do like this.

10
00:00:42,550 --> 00:00:45,640
But ideally we should never do this.

11
00:00:45,740 --> 00:00:50,010
No C++ does not get that this thing will work.

12
00:00:50,050 --> 00:00:56,140
It may work on some compiler and it may not work on other compilers so there is no guarantee that this

13
00:00:56,140 --> 00:00:57,470
thing will work.

14
00:00:57,490 --> 00:01:00,210
So that's why we should never use this.

15
00:01:00,310 --> 00:01:03,040
We should never create our Eddie.

16
00:01:03,070 --> 00:01:06,260
So this is and is actually a variable.

17
00:01:06,280 --> 00:01:13,560
So basically what we are doing we are creating a variable sized Eddie and in this approach we were creating

18
00:01:13,590 --> 00:01:14,950
a fixed sized Eddie.

19
00:01:14,970 --> 00:01:20,400
So this is hundred percent correct but there are problem with this way of creating Eddie.

20
00:01:20,430 --> 00:01:24,110
So we are creating a variable sized Eddie and the value of an.

21
00:01:24,400 --> 00:01:27,650
So the value of end will be given by the user at runtime.

22
00:01:27,660 --> 00:01:33,480
So when our program will be running then the user will give what is the value of n and then we are creating

23
00:01:33,540 --> 00:01:34,890
Eddie of that size.

24
00:01:35,550 --> 00:01:38,990
So this is actually wrong and in this time.

25
00:01:38,990 --> 00:01:41,070
So this is fixed sized Eddie.

26
00:01:41,240 --> 00:01:42,500
So this is fixed as it is.

27
00:01:42,590 --> 00:01:47,520
This is compile time we know Ed the compile demo leader.

28
00:01:47,590 --> 00:01:49,620
How much memory we required.

29
00:01:49,660 --> 00:01:51,210
But this is runtime.

30
00:01:51,310 --> 00:01:54,820
So we do not know how much memory our Eddie will take.

31
00:01:54,820 --> 00:01:58,300
So there are actually two types of memory allocation.

32
00:01:58,390 --> 00:02:03,520
So there are actually two types of memory stack memory and heap memory.

33
00:02:03,520 --> 00:02:06,100
So let's see what is a stack and heap memory.

34
00:02:08,350 --> 00:02:11,560
So the first type of memory is stack.

35
00:02:11,560 --> 00:02:20,510
And the second type of memory is heap so stack memory is small and a heap memory is very big as compared

36
00:02:20,510 --> 00:02:32,200
to the stack memory now whenever you do something like into a question or you do something like into

37
00:02:32,250 --> 00:02:34,230
a 20.

38
00:02:34,230 --> 00:02:36,840
So actually these two things.

39
00:02:36,840 --> 00:02:40,530
So the memory is going to be allocated on the stack.

40
00:02:40,530 --> 00:02:41,690
Fine.

41
00:02:41,790 --> 00:02:48,290
Now even if you do this I want to create an array of ten thousand sites.

42
00:02:48,390 --> 00:02:52,230
So in this case again the memory is going to be allocated on stack.

43
00:02:52,230 --> 00:02:54,950
So what happens is whenever I a program run.

44
00:02:55,080 --> 00:03:00,240
So when I let a program start running it will take some along the stack memory and it will run the program

45
00:03:01,430 --> 00:03:04,590
in this case when you're creating a big sized area.

46
00:03:04,610 --> 00:03:10,850
So I'm creating a big error so I know at the compile family that I need a big memory I need a large

47
00:03:10,850 --> 00:03:11,490
memory.

48
00:03:11,540 --> 00:03:12,770
So what will happen.

49
00:03:12,770 --> 00:03:16,010
Our program will take comparatively a bigger stack.

50
00:03:16,040 --> 00:03:20,530
It will come it will comparatively take a bigger stick to hold this much amount of memory.

51
00:03:20,660 --> 00:03:23,170
And then our program will run.

52
00:03:23,240 --> 00:03:24,770
So I mean I'm building myself.

53
00:03:24,770 --> 00:03:28,010
So whenever a system runs our program what will happen.

54
00:03:28,010 --> 00:03:33,690
So system will take some amount of stack memory and it will run our program.

55
00:03:33,710 --> 00:03:36,880
So even if you want to create an array of 10000 size.

56
00:03:37,190 --> 00:03:41,690
So the system knows at the compile time will lead that you're creating this big error.

57
00:03:41,700 --> 00:03:43,740
So you need a bigger stack memory.

58
00:03:43,760 --> 00:03:49,900
So the system will allocate us a bigger aspect memory and it will run our program perfectly fine.

59
00:03:49,910 --> 00:04:01,070
So the problem occurs when you will do something like this in an CNN now and in again.

60
00:04:01,100 --> 00:04:05,930
So this memory is going to be located on heap only sorry on stack.

61
00:04:05,930 --> 00:04:09,430
This memory is going to be allocated on a stack only.

62
00:04:09,680 --> 00:04:14,350
The problem will occur if the user will give the value of n to be a very large number.

63
00:04:14,360 --> 00:04:16,990
Let's return to the power six.

64
00:04:17,180 --> 00:04:17,960
So what will happen.

65
00:04:17,960 --> 00:04:24,740
System will take some amount of spec memory and it will run our program and add the runtime user can

66
00:04:24,740 --> 00:04:27,690
give the value of n let's say a very large number.

67
00:04:27,710 --> 00:04:33,170
So if user give a very large number as input then basically you expect will not have that much amount

68
00:04:33,170 --> 00:04:36,450
of memory to store this much big along very.

69
00:04:36,500 --> 00:04:42,990
So this much begetting I am repeating myself the problem will occur if you will do something like this

70
00:04:45,490 --> 00:04:51,340
so our system will run our program with some amount of stack memory fine.

71
00:04:51,400 --> 00:04:56,620
Now the value of end will be decided at that time and let's say the user give the value of n to be a

72
00:04:56,620 --> 00:05:03,370
very large number then what will happen your stack will not have enough amount of memory to hold this

73
00:05:03,370 --> 00:05:04,370
Big Eddie.

74
00:05:04,420 --> 00:05:07,710
So your program may crash.

75
00:05:07,720 --> 00:05:15,370
Now the obvious solution for this problem is C heap memory is a very big memory so you can allocate

76
00:05:15,670 --> 00:05:22,840
your Eddie in this memory if you want to do something like this then look at the memory in heap because

77
00:05:22,840 --> 00:05:30,040
heap is much much larger than Stack and one more thing the value of n is given by the user at runtime

78
00:05:30,310 --> 00:05:31,810
so the stack space.

79
00:05:31,810 --> 00:05:36,710
So this stack size could not be increased during that time okay.

80
00:05:36,730 --> 00:05:42,430
We cannot increase our stack memory during runtime so the obvious solution is add brand name allocate

81
00:05:42,430 --> 00:05:46,210
the memory inside heap because heap is much much bigger than stack.

82
00:05:46,300 --> 00:05:49,630
So this added can easily be stored inside heap.

83
00:05:51,190 --> 00:05:53,590
So this is called heap memory allocation

84
00:05:57,230 --> 00:06:03,560
heap memory allocation and the second name of heat memory allocation is actually dynamic memory allocation

85
00:06:08,560 --> 00:06:09,410
so let's see.

86
00:06:09,520 --> 00:06:14,410
Let us differentiate what is dynamic memory allocation.

87
00:06:14,410 --> 00:06:24,850
So we have stack memory and we have heat memory so in stack memory when we do something like this in

88
00:06:24,870 --> 00:06:25,200
day.

89
00:06:25,230 --> 00:06:28,980
So everything is going to be stored inside my spec memory.

90
00:06:28,980 --> 00:06:37,440
So this is called static allocation starting a location means your data is going to be stored on a stack

91
00:06:37,440 --> 00:06:38,520
memory.

92
00:06:38,580 --> 00:06:39,650
Now in heat memory.

93
00:06:39,660 --> 00:06:44,700
So if you want to store something heat memory then we call it dynamic memory allocation or you can say

94
00:06:44,700 --> 00:06:54,430
heat memory allocation or dynamic memory allocation so this memory is actually the memory is allocated

95
00:06:54,460 --> 00:06:59,200
during compile time so you can see you can write memory allocated

96
00:07:01,750 --> 00:07:02,950
during compile time

97
00:07:06,480 --> 00:07:07,500
because in date.

98
00:07:07,530 --> 00:07:13,470
So this will get compiled and memory will get allocated at compile time only now a heap.

99
00:07:13,470 --> 00:07:15,780
So you want to create a variable sized Eddie.

100
00:07:15,990 --> 00:07:17,640
You want to do something like this.

101
00:07:17,940 --> 00:07:20,670
And basically you want to create a variable sized Eddie.

102
00:07:20,730 --> 00:07:24,330
So that means so the value of end will be given by the user runtime.

103
00:07:24,360 --> 00:07:31,650
So it is called runtime memory allocation random memory allocation or you can say the memory will get

104
00:07:31,650 --> 00:07:35,360
allocated at runtime.

105
00:07:35,650 --> 00:07:39,880
So this is not actually this index for allocating memory on inside heap.

106
00:07:39,880 --> 00:07:43,010
You will see this index but this is not the right index.

107
00:07:43,210 --> 00:07:47,770
Now the stack memory is much smaller than the heap memory.

108
00:07:47,770 --> 00:07:56,640
So this is less and my heap memory is very large memory so heap is a very large memory.

109
00:07:56,720 --> 00:08:00,050
So what I want to do I want to do something like this.

110
00:08:00,290 --> 00:08:06,000
So with the current knowledge that we have we only know how to allocate memory inside stack.

111
00:08:06,080 --> 00:08:09,170
We don't know how to allocate memory inside heap.

112
00:08:09,170 --> 00:08:19,150
So now let us learn dynamic allocation how to allocate memory inside heap so let's see so if you will

113
00:08:19,150 --> 00:08:22,810
do something like this entire question.

114
00:08:22,900 --> 00:08:29,110
So what will happen this AI is going to be stored inside my stack memory.

115
00:08:29,110 --> 00:08:32,730
This idea will be stored inside these tech memory fine.

116
00:08:32,770 --> 00:08:37,220
Now what I want to do I want to store something inside my memory.

117
00:08:37,330 --> 00:08:40,120
So how can I write this index.

118
00:08:40,120 --> 00:08:41,540
So what is this index.

119
00:08:41,650 --> 00:08:47,200
So what you have to write like this new and you want to create and Deja.

120
00:08:47,230 --> 00:08:52,810
So you will write new and so with this statement so new is actually a keyword new the keyword.

121
00:08:52,810 --> 00:09:01,630
So this new and what will happen you will get four bytes of memory space inside you and heap you will

122
00:09:01,630 --> 00:09:04,380
get four bytes of memory space inside your heap.

123
00:09:04,390 --> 00:09:11,230
Now let's say you want to insert 10 you want to put pen here but this memory doesn't have a name it

124
00:09:11,230 --> 00:09:12,120
doesn't have a name.

125
00:09:12,190 --> 00:09:18,050
So here if I want to do something with a so I have a name but this doesn't have a name.

126
00:09:18,220 --> 00:09:21,790
So how can we put 10 inside these four bytes.

127
00:09:21,790 --> 00:09:25,630
So actually this new thing it returns the address of this memory.

128
00:09:25,660 --> 00:09:28,260
So if I do this let's say it addresses hundred.

129
00:09:28,270 --> 00:09:30,930
So this new thing will return me the address.

130
00:09:31,090 --> 00:09:34,070
And for storing the address we started pointers.

131
00:09:34,180 --> 00:09:39,210
So I will write it like this and start I.

132
00:09:39,430 --> 00:09:43,750
So I will write it like this in START I is new and

133
00:09:47,350 --> 00:09:51,710
so what is the effect of this statement but it might really with the effect of the slain.

134
00:09:51,850 --> 00:09:57,890
So new is going to be done the address of the memory inside heap.

135
00:09:58,100 --> 00:09:59,090
So what will happen.

136
00:09:59,090 --> 00:10:07,370
So this new and what it will do so it will go inside the heap memory and it will create four bytes it

137
00:10:07,370 --> 00:10:11,470
will give me four bytes and the let's say it does is hundred.

138
00:10:11,550 --> 00:10:17,810
So this I think so this I use a pointer so what is happening this is U.S. tech.

139
00:10:17,880 --> 00:10:23,410
This is U.S. tech memory and science tech memory you have created guy Deja 2.0.

140
00:10:23,430 --> 00:10:28,860
So this is your digital point that you disk containing and it just ended and we know point does sod

141
00:10:28,890 --> 00:10:37,640
off you two bytes so add this line total bill bytes of memory are allocated out of tool you two bytes

142
00:10:37,730 --> 00:10:49,550
are allocated on stack and four bytes are allocated on the heap now if you want to put something inside

143
00:10:49,580 --> 00:10:53,230
this memory this index is quite simple do it do it like this.

144
00:10:53,230 --> 00:10:55,960
I equals ten So ten will be inserted here.

145
00:10:56,880 --> 00:11:03,010
Okay so this I is actually pointing towards this heap memory address.

146
00:11:03,240 --> 00:11:07,530
So if you want to insert then you can do it like this spot I equals 10.

147
00:11:07,560 --> 00:11:12,850
Similarly if you want to create a double so you will write it like this.

148
00:11:13,100 --> 00:11:17,280
Double Star P is new double

149
00:11:20,370 --> 00:11:21,750
again what will happen.

150
00:11:21,810 --> 00:11:23,070
So this is good heat memory

151
00:11:26,860 --> 00:11:28,970
so double is off eight bytes.

152
00:11:29,090 --> 00:11:30,520
This is a debate.

153
00:11:30,620 --> 00:11:35,120
This is U.S. tech memory inside stack.

154
00:11:35,170 --> 00:11:37,000
You are storing double pointer.

155
00:11:37,030 --> 00:11:43,970
So this is B let's say it addresses hundred so it is storing at this hundred and it is pointing towards

156
00:11:43,970 --> 00:11:50,060
this if you want to put something if you want to put something inside this box you will do it like this

157
00:11:50,150 --> 00:11:54,080
star B equals three point fold simple.

158
00:11:54,380 --> 00:11:56,150
So let's write the code and let's see

159
00:12:03,110 --> 00:12:09,160
so into start B is new keywords so new is a keyword.

160
00:12:09,350 --> 00:12:13,390
And so at this line how much amount of memory space is allocated.

161
00:12:13,670 --> 00:12:28,050
So 8 bytes on stack because you are creating AI integer pointer and for byte 1 heap now how to access

162
00:12:28,460 --> 00:12:35,280
heap memory ERP because bees containing bees containing the address of the memory so start to be closed

163
00:12:35,280 --> 00:12:38,860
down and let's say you want to print the content.

164
00:12:38,870 --> 00:12:47,080
So if you would like to see out bee it will print the address if you want to paint the content you have

165
00:12:47,080 --> 00:12:48,880
to use deet offense operator

166
00:12:51,750 --> 00:12:53,320
so Lucy out start bee

167
00:13:03,450 --> 00:13:09,760
so this is the address of the memory which is located inside heap and this is the value

168
00:13:15,720 --> 00:13:22,950
similarly what you can do it can also do it like this so double star D is new double

169
00:13:26,090 --> 00:13:36,990
similarly you can do good start C is new get so this new thing it is are dunning me address of the memory

170
00:13:37,680 --> 00:13:44,540
so again I'm repeating myself so when you do something like this many will write the statement like

171
00:13:44,540 --> 00:13:55,090
this and start B is new int So what's going to happen memory will be located inside the heap and it

172
00:13:55,090 --> 00:14:00,360
will also be located inside the stack.

173
00:14:00,370 --> 00:14:04,420
So this is just tech stack memory is smaller than heap memory.

174
00:14:04,420 --> 00:14:11,920
So new and so you will get four bytes here this is your four bytes start B.

175
00:14:11,980 --> 00:14:19,050
So this is you're in Deja point to a debate and B is pointing towards this memory location.

176
00:14:19,060 --> 00:14:26,690
So let's say if you want to put something here let's say 20 so you have to write star B equals twenty

177
00:14:27,050 --> 00:14:35,860
so first start P means D defense better first and go to the memory location and then put 20 did fine.

178
00:14:35,880 --> 00:14:37,640
So this new is actually a keyword.

179
00:14:37,650 --> 00:14:45,810
And where does the address off this memory does of the memory which is located inside heap.

180
00:14:45,960 --> 00:14:53,850
OK so now let's see how we can allocate at a dynamically how we can put Eddie inside the heap.

181
00:14:53,880 --> 00:14:56,090
So I want to create entity inside the heap.

182
00:14:56,100 --> 00:14:58,620
So let's see what will be this index.

183
00:14:58,620 --> 00:15:08,640
So this index is quite simple you have to write it like this and start e is new in 50.

184
00:15:09,300 --> 00:15:12,180
So what will happen again.

185
00:15:12,560 --> 00:15:13,670
This is you to heap memory

186
00:15:16,930 --> 00:15:21,400
so new int and 50 so 50 means forward into 50.

187
00:15:21,400 --> 00:15:27,910
That means you will get it to wondered bytes of data so you will get an net inside the heap.

188
00:15:27,930 --> 00:15:36,380
This is your daddy index zero index 49 and this new thing what it will get done it will have done the

189
00:15:36,380 --> 00:15:42,260
address of the first element it will be done the address of the first element and I have created an

190
00:15:42,270 --> 00:15:46,010
digital pointer to store that address simple.

191
00:15:46,640 --> 00:15:51,040
So now we know how to create added dynamically.

192
00:15:51,200 --> 00:15:55,040
So after knowing this we can definitely do something like this.

193
00:15:55,310 --> 00:16:04,290
So I can write into n CNN and then I will write it like this in start.

194
00:16:04,330 --> 00:16:13,030
E is new and and so now this is Connect.

195
00:16:13,060 --> 00:16:19,840
So what will happen the value of end value given by the user at runtime and at that time what will happen

196
00:16:19,870 --> 00:16:20,490
at that time.

197
00:16:20,500 --> 00:16:22,610
We can allocate memory inside the heap.

198
00:16:23,860 --> 00:16:25,870
So the value of N will be given by the user.

199
00:16:25,870 --> 00:16:29,560
And then you will create an array of sized and inside the heap.

200
00:16:29,560 --> 00:16:33,780
So let's say the value of n is 50.

201
00:16:33,880 --> 00:16:36,090
So this will be at index 50.

202
00:16:36,340 --> 00:16:40,460
So this will be an index 49 and this is your index is you.

203
00:16:41,040 --> 00:16:41,450
Okay.

204
00:16:41,550 --> 00:16:44,540
So now you can write like this.

205
00:16:44,550 --> 00:16:47,560
So this is perfectly fine.

206
00:16:47,590 --> 00:16:48,340
So what did happen.

207
00:16:48,350 --> 00:16:52,100
Program our system will take some stack memory and it will then the program.

208
00:16:52,180 --> 00:16:56,770
And if you want to allocate the memory at that time then allocate the memory inside the heap because

209
00:16:56,800 --> 00:16:59,650
each memory is much much larger than distinct memory.

210
00:16:59,650 --> 00:17:01,990
So now our problem has been solved.

211
00:17:02,020 --> 00:17:03,820
So there were two problems with daddy.

212
00:17:04,570 --> 00:17:12,190
So there does discuss the edit problems and let us discuss how discord will help us to solve that problem

213
00:17:13,940 --> 00:17:15,630
so and has proven them.

214
00:17:15,650 --> 00:17:25,590
So the first problem is let's say I have created at a offsite thousand and let's say I am doing this

215
00:17:25,670 --> 00:17:28,110
in then in.

216
00:17:28,590 --> 00:17:31,800
And then I am doing I equals zero.

217
00:17:31,800 --> 00:17:39,800
I listed in a plus place and I am doing the scene if I now let's say the value of Aniston.

218
00:17:40,190 --> 00:17:42,750
Let's say the value often given by the user is 10.

219
00:17:42,800 --> 00:17:43,840
So what would happen.

220
00:17:45,000 --> 00:17:50,700
I have a good memory four thousand elements but I will use memory only for 10 elements.

221
00:17:50,700 --> 00:17:52,190
So basically the elements.

222
00:17:52,210 --> 00:17:55,370
So how much amount of memory space I am wasting here.

223
00:17:55,410 --> 00:17:57,900
So 9 and elements multiply each element.

224
00:17:58,020 --> 00:18:00,260
So each integer will take four bytes.

225
00:18:00,300 --> 00:18:06,140
So this much amount of memory space will get wasted if I will use the static allocation.

226
00:18:06,600 --> 00:18:14,520
If I'm using this a location so this is the first problem of the areas that we have a lot of amount

227
00:18:14,520 --> 00:18:15,800
of memory wastage.

228
00:18:15,990 --> 00:18:23,420
If we are creating a fixed sized Eddie and with the help of dining memory allocation we will not waste

229
00:18:23,480 --> 00:18:24,290
any space.

230
00:18:24,830 --> 00:18:30,150
So our first problem will get resolved with the help of dynamic memory allocation.

231
00:18:30,170 --> 00:18:32,170
Now what is the second problem with daddy.

232
00:18:32,240 --> 00:18:36,290
So the second problem is let's say you are creating an array of size 10.

233
00:18:36,480 --> 00:18:41,390
Then again you will do the same thing and then you will take how many number of elements you want to

234
00:18:41,390 --> 00:18:42,150
insert.

235
00:18:42,230 --> 00:18:47,660
And again you will be equals you know less than I plus plus and you will do something like this scene

236
00:18:47,670 --> 00:18:48,860
a if.

237
00:18:49,430 --> 00:18:55,550
Now let's say the value of randomly given by the user that in time now led to the value of and is ended.

238
00:18:55,610 --> 00:18:58,510
If you give the value of hundred then what will happen.

239
00:18:58,520 --> 00:19:05,150
Your program will crash because you can add the max a store then elements and the user has given the

240
00:19:05,150 --> 00:19:09,500
value of N hundred so your program is going to be correct.

241
00:19:09,590 --> 00:19:13,550
So again if you will use a dynamic memory allocation.

242
00:19:13,910 --> 00:19:17,830
So the second problem of daddy will get resolved.

243
00:19:18,020 --> 00:19:21,130
So it is the power of dynamic memory allocation.

244
00:19:21,260 --> 00:19:23,290
What the problems associated with Daddy.

245
00:19:23,330 --> 00:19:24,370
Will get resolved.

246
00:19:24,500 --> 00:19:26,200
We will not waste any memory.

247
00:19:26,360 --> 00:19:32,300
And also we will not face problem of this type so dynamic memory allocation is really really good.

248
00:19:33,620 --> 00:19:37,730
So after creating the eddy how can it put the elements inside.

249
00:19:37,730 --> 00:19:40,760
So just like the state to get it you can do it like this.

250
00:19:40,760 --> 00:19:44,180
If 0 is 10 so then will be here.

251
00:19:44,180 --> 00:19:45,200
You can write like this.

252
00:19:45,200 --> 00:19:49,310
So each of 20 is let's say 40.

253
00:19:49,330 --> 00:19:51,420
So you can add the next 20.

254
00:19:51,830 --> 00:19:54,880
I will insert 40 so you're going definitely do it like this.

255
00:19:54,890 --> 00:19:56,170
Just like the.

256
00:19:56,330 --> 00:19:58,820
Just like the state to get a vibe because.

257
00:19:58,820 --> 00:20:04,220
So what is it if I so I'll fight is same as the start of E plus I

258
00:20:10,150 --> 00:20:13,040
so that's why you can write this index like this.

259
00:20:13,390 --> 00:20:21,380
So this is for the inside so let's say I want to find out the largest element given and every find the

260
00:20:21,470 --> 00:20:23,270
largest element inside the 80.

261
00:20:23,330 --> 00:20:27,320
So let's solve this question okay.

262
00:20:27,340 --> 00:20:30,590
So let us write the code for finding out the largest element

263
00:20:37,500 --> 00:20:48,960
so n n seen and and start a is new int and now what will happen.

264
00:20:48,980 --> 00:20:51,680
So the value of end will be given by the user at one time.

265
00:20:51,980 --> 00:20:55,250
So this edit is going to be present inside the heap.

266
00:20:55,250 --> 00:21:01,220
We are going to create that inside heap and 8 bytes will be allocated on the stack because we are getting

267
00:21:01,220 --> 00:21:02,040
a digital pointer.

268
00:21:02,510 --> 00:21:04,640
So now let us take input from the user.

269
00:21:04,670 --> 00:21:11,000
So like was 0 I lost and in a plus plus seen a 0 5

270
00:21:15,020 --> 00:21:17,620
and now let us try to find out the maximum element.

271
00:21:17,660 --> 00:21:21,370
So initially let's say the largest element is minus 1.

272
00:21:21,440 --> 00:21:26,960
For now let's assume that the user will give all the positive elements as input so I equals zero.

273
00:21:27,090 --> 00:21:29,640
I listed in a plus plus.

274
00:21:29,690 --> 00:21:31,610
So if the current element.

275
00:21:31,640 --> 00:21:38,000
So if you if I if the current element is good then the largest element then you can update your largest

276
00:21:38,000 --> 00:21:39,820
element.

277
00:21:40,430 --> 00:21:43,040
And finally you can bring your largest element

278
00:21:46,110 --> 00:21:49,890
so let's an area called okay.

279
00:21:49,900 --> 00:21:52,710
So here I forgot to add 7 column

280
00:21:57,950 --> 00:22:03,910
so there are 5 elements and the element solid C 1 2 3 5 and 4.

281
00:22:04,340 --> 00:22:08,550
So 5 is the largest element civil code is working fine.

282
00:22:08,660 --> 00:22:13,220
Now tell me how much memory is going to be allocated if I write something like this.

283
00:22:13,580 --> 00:22:21,330
So if I write and start a is new and 50.

284
00:22:21,520 --> 00:22:24,640
So how much memory will get allocated.

285
00:22:24,640 --> 00:22:25,670
So there are two parts.

286
00:22:25,690 --> 00:22:33,800
First the ad is going to be created inside the heap of 50 sites.

287
00:22:33,930 --> 00:22:37,410
So inside the heap how much memory will be allocated.

288
00:22:37,440 --> 00:22:38,920
So 50 in 2 4.

289
00:22:39,300 --> 00:22:44,750
So two hundred bytes are going to be allocated on heap and on stack.

290
00:22:44,760 --> 00:22:47,150
How much memory will be allocated.

291
00:22:47,160 --> 00:22:49,230
So we are creating a integer pointer.

292
00:22:49,680 --> 00:22:51,480
So this is a.

293
00:22:51,570 --> 00:22:52,870
So even right.

294
00:22:53,010 --> 00:22:59,900
So in totally add this line two hundred eight bytes will be allocated.

295
00:23:00,310 --> 00:23:01,500
Fine.

296
00:23:01,630 --> 00:23:06,220
Now let us see one more difference between the static and dynamic allocation and the most important

297
00:23:06,220 --> 00:23:07,680
difference.

298
00:23:07,720 --> 00:23:17,470
So for example this is used to mean and let's say this is you if and inside.

299
00:23:17,480 --> 00:23:22,520
If you are writing int i and yet if will end here.

300
00:23:22,550 --> 00:23:25,370
So there are some more line of code.

301
00:23:25,550 --> 00:23:26,500
So what will happen.

302
00:23:26,510 --> 00:23:29,720
So as soon as this I point out it will reach this line.

303
00:23:29,750 --> 00:23:30,380
What will happen.

304
00:23:30,380 --> 00:23:33,270
So the memory will be automatically deleted.

305
00:23:33,380 --> 00:23:39,330
So as soon as the scope of the variable i finish its memory will be delegated.

306
00:23:39,350 --> 00:23:41,580
So this is static allocation.

307
00:23:41,780 --> 00:23:43,550
So this is stack.

308
00:23:43,580 --> 00:23:45,170
So this a memory.

309
00:23:45,170 --> 00:23:52,320
So this i.e. variable is going to be present inside the stack and as soon as I will reached this line.

310
00:23:52,510 --> 00:23:54,210
So the memory allocated.

311
00:23:54,310 --> 00:24:00,010
So basically the four bytes allocated to the variability will get deleted automatically.

312
00:24:00,010 --> 00:24:05,080
But this is not the case with the dynamic and location.

313
00:24:05,080 --> 00:24:06,630
So if you will write it like this.

314
00:24:06,670 --> 00:24:15,900
If then if you were creating a dynamic memory if you are using dynamic communication then what will

315
00:24:15,900 --> 00:24:16,200
happen.

316
00:24:16,200 --> 00:24:17,550
So this i.e..

317
00:24:17,640 --> 00:24:23,920
So this variable ie it doesn't have any scope so no scope is there for a variable.

318
00:24:24,150 --> 00:24:30,000
Because this is heap memory allocation dynamic memory location so it will not get deleted.

319
00:24:30,060 --> 00:24:36,510
So the biggest difference between the static memory allocation and the dynamic memory allocation is

320
00:24:38,190 --> 00:24:42,630
so if you are allocating memory statically for example in die then what will happen.

321
00:24:42,900 --> 00:24:49,440
So they will be automatic release of memory on bases of scope

322
00:24:52,510 --> 00:24:56,560
whereas in the dining memory relegation there is no concept of scope.

323
00:24:56,660 --> 00:25:03,040
So if you are doing something like this in study is new and so what you have to do.

324
00:25:03,070 --> 00:25:05,510
So there is no scope there is no concept of scope.

325
00:25:05,540 --> 00:25:08,540
So you have to manually release the memory.

326
00:25:08,730 --> 00:25:13,210
You have to manually release the memory.

327
00:25:13,480 --> 00:25:15,760
So what will happen if I will do something like this.

328
00:25:15,760 --> 00:25:16,510
For example

329
00:25:20,330 --> 00:25:21,080
vital through

330
00:25:27,590 --> 00:25:43,980
a and similarly what will happen if I will do something like this while to in start I is new and so

331
00:25:43,990 --> 00:25:49,740
what will happen if I will run this code so you entered the VI loop.

332
00:25:49,850 --> 00:25:52,210
So first of all this loop is vital.

333
00:25:52,220 --> 00:25:54,830
So that means this is going to run in finite times.

334
00:25:54,830 --> 00:25:57,650
So this is in finite loop then what will happen.

335
00:25:57,650 --> 00:26:05,540
So as soon as you reach this line 4 bytes will be allocated inside your stack memory then you will go

336
00:26:05,540 --> 00:26:06,190
above.

337
00:26:06,200 --> 00:26:12,180
So as soon as you will go above this 4 bytes idea located and then you will again reach this line.

338
00:26:12,260 --> 00:26:14,470
So again the 4 bytes will be allocated.

339
00:26:14,480 --> 00:26:16,220
Then again you will go up.

340
00:26:16,220 --> 00:26:18,390
So this 4 bytes will be deleted.

341
00:26:18,560 --> 00:26:22,220
Then you will come here again so 4 bytes will be allocated again.

342
00:26:22,220 --> 00:26:23,690
Then you will go up again.

343
00:26:23,720 --> 00:26:25,310
So 4 bytes will be deleted.

344
00:26:25,310 --> 00:26:26,620
So this will go on.

345
00:26:26,660 --> 00:26:33,020
So basically at max so at any point of time at max you will.

346
00:26:33,020 --> 00:26:40,490
You are using food by itself memory space so your program will look perfectly fine no doubt.

347
00:26:40,490 --> 00:26:46,340
Now let us consider this case what will happen if I will run Discworld so enter this.

348
00:26:46,340 --> 00:26:49,620
So as soon as you reach this line what will happen.

349
00:26:49,670 --> 00:26:57,860
So eat bites are allocated inside you and stack memory because you are creating and Deja pointer and

350
00:26:57,920 --> 00:27:00,850
4 bytes are allocated inside your heap.

351
00:27:00,850 --> 00:27:04,360
So inside heap 4 bytes are allocated.

352
00:27:04,580 --> 00:27:06,140
Now you will reach this line.

353
00:27:06,230 --> 00:27:08,620
So this 8 bytes will be dedicated.

354
00:27:08,720 --> 00:27:10,670
Then you will again go up.

355
00:27:10,670 --> 00:27:14,830
You will reach this line so again 8 bytes will be allocated for Stack.

356
00:27:14,930 --> 00:27:17,070
But this for bytes are not deleted.

357
00:27:17,150 --> 00:27:24,870
So again you are writing new int so the 4 bytes will get allocated again then you will reach this line.

358
00:27:24,870 --> 00:27:28,520
So this 8 bytes will be allocated because this is stack.

359
00:27:28,530 --> 00:27:29,370
Memory allocation.

360
00:27:29,370 --> 00:27:30,640
Then again you will go up.

361
00:27:30,750 --> 00:27:36,470
You will reach this line so again 8 bytes will be allocated on stack memory and new and so forth.

362
00:27:36,480 --> 00:27:39,430
Bytes will be allocated on heap again.

363
00:27:39,660 --> 00:27:42,630
So again you will reach this line.

364
00:27:42,630 --> 00:27:44,940
So this 8 bytes will be d allocated.

365
00:27:45,030 --> 00:27:46,440
And again you will go up.

366
00:27:46,440 --> 00:27:52,770
You will reach this line so again 8 bytes will be allocated inside to stack memory and 4 plus 4 here.

367
00:27:52,800 --> 00:27:54,410
Because new integer.

368
00:27:54,630 --> 00:27:55,590
So this will go on.

369
00:27:55,590 --> 00:27:57,090
So ultimately what will happen.

370
00:27:57,240 --> 00:28:06,540
Your memory usage is increasing your memory usage is continuously increasing and at some point of time

371
00:28:06,550 --> 00:28:07,510
what will happen.

372
00:28:07,590 --> 00:28:10,380
Your app your program will crash.

373
00:28:10,530 --> 00:28:14,250
At some point of time our program will crash.

374
00:28:14,250 --> 00:28:16,740
So basically if you will write code like this

375
00:28:21,370 --> 00:28:22,850
so if I will do something like this

376
00:28:29,080 --> 00:28:40,950
and if I really lose something like this.

377
00:28:41,090 --> 00:28:42,970
So what will happen.

378
00:28:43,070 --> 00:28:44,140
So this is your homework.

379
00:28:44,150 --> 00:28:45,010
What you will do.

380
00:28:45,050 --> 00:28:50,440
So first you will run this piece of code and you will command out this piece of code.

381
00:28:50,540 --> 00:28:55,190
So if you will read this piece of code what you will do is you will open your activity monitor or you

382
00:28:55,190 --> 00:28:59,480
will open your task manager and you will notice what is your memory usage.

383
00:28:59,480 --> 00:29:05,650
So in this case your memory usage will be constant because at max we will be using for by of memory

384
00:29:05,650 --> 00:29:07,060
space.

385
00:29:07,160 --> 00:29:13,670
Now command out this code and run this code run this three pieces off line then this three lines what

386
00:29:13,670 --> 00:29:14,850
will happen again.

387
00:29:14,930 --> 00:29:20,000
You will open your task manager or activity monitor and then you will again check your memory usage

388
00:29:20,090 --> 00:29:22,550
so your memory usage will keep increasing.

389
00:29:22,550 --> 00:29:24,940
So your memory usage will keep increasing.

390
00:29:24,950 --> 00:29:31,400
And finally your program is going to be crashed because of lack of memory so you can do this.

391
00:29:31,400 --> 00:29:33,590
So for now I am commending our discord

392
00:29:38,990 --> 00:29:43,490
so what is happening here why it is happening because we discussed about that.

393
00:29:43,550 --> 00:29:47,570
The static memory will be deleted automatically.

394
00:29:47,570 --> 00:29:52,520
But if you want or delete the heap memory you have to manually release the memory if you want to delete

395
00:29:52,550 --> 00:29:53,540
the heap memory.

396
00:29:53,630 --> 00:29:56,650
You have to manually believed that memory.

397
00:29:56,690 --> 00:29:59,370
Now how we can manually delete the memory.

398
00:29:59,570 --> 00:30:00,980
So just like a new keyword.

399
00:30:00,980 --> 00:30:02,660
We also have a keyword delete.

400
00:30:03,530 --> 00:30:04,970
So what they are doing here.

401
00:30:06,980 --> 00:30:13,560
And start B is new and so what will happen.

402
00:30:13,770 --> 00:30:20,880
This is you your heat memory D idea for bytes

403
00:30:23,660 --> 00:30:24,920
fine.

404
00:30:25,010 --> 00:30:36,820
This is U.S. tech memory these are 8 bytes so p and eight bytes so what is B is pointing towards this

405
00:30:36,820 --> 00:30:43,180
memory location and I want to delete this memory because this memory we have to lose this memory and

406
00:30:43,180 --> 00:30:48,040
this memory will get deleted automatically as soon as it will reach out of scope.

407
00:30:48,040 --> 00:30:53,420
So for deleting the memory we have a keyword delete and we have to give the address.

408
00:30:53,530 --> 00:30:59,770
So let's say that this is hundred so various address present addresses presented so you will write to

409
00:30:59,770 --> 00:31:03,560
delete b you will write delete B.

410
00:31:03,640 --> 00:31:07,140
Now after this line get executed what will happen.

411
00:31:07,210 --> 00:31:09,930
So delete B means go to this address.

412
00:31:10,060 --> 00:31:15,310
So go to this address and delete the memory so delete the memory.

413
00:31:15,460 --> 00:31:19,420
Now the memory has been deleted so this be pointer.

414
00:31:19,420 --> 00:31:21,440
So this memory will not get deleted.

415
00:31:21,490 --> 00:31:25,920
Just remember many people have this confusion that this memory will also get deleted.

416
00:31:25,930 --> 00:31:32,140
No this memory will be deleted automatically then it will reach out of scope.

417
00:31:32,140 --> 00:31:36,600
So basically we are deleting this memory simple.

418
00:31:36,610 --> 00:31:39,330
Now one thing after deleting the memory.

419
00:31:39,460 --> 00:31:41,580
Please do not try to system and again.

420
00:31:41,590 --> 00:31:43,380
So do not try to do this.

421
00:31:43,410 --> 00:31:44,010
Why.

422
00:31:44,080 --> 00:31:45,610
Because if you will try to do this.

423
00:31:45,610 --> 00:31:46,570
What they are doing.

424
00:31:46,570 --> 00:31:48,020
So this memory has been deleted.

425
00:31:48,280 --> 00:31:54,070
So you are going to this address and basically this memory is now not belonging to us.

426
00:31:54,100 --> 00:31:59,310
This memory does not belongs to us and you are trying to put 10 inside this memory.

427
00:31:59,440 --> 00:32:01,290
So this is very dangerous.

428
00:32:01,300 --> 00:32:03,670
So do not try to do this.

429
00:32:03,730 --> 00:32:08,890
So do not try to do this because this is very dangerous because after deleting the memory the memory

430
00:32:08,920 --> 00:32:10,000
does not belongs to us.

431
00:32:10,390 --> 00:32:15,540
So do not try to do something like this now one more thing.

432
00:32:15,540 --> 00:32:17,120
So we have deleted this memory.

433
00:32:17,130 --> 00:32:21,100
We can also do like this after deleting the memory after deleting the memory.

434
00:32:21,120 --> 00:32:28,260
We will never do this and we can also allocate new memory so I can write new and also I can write new

435
00:32:28,260 --> 00:32:28,700
it again.

436
00:32:28,710 --> 00:32:29,430
So what will happen.

437
00:32:29,430 --> 00:32:33,330
I will get an idea for byte and now this people point here.

438
00:32:33,360 --> 00:32:41,080
Then again I can do delete B so this memory will get deleted and then again what can I do.

439
00:32:41,100 --> 00:32:44,050
I can write.

440
00:32:44,280 --> 00:32:47,000
So this is only b not start B.

441
00:32:47,040 --> 00:32:51,430
So again I can write new intended.

442
00:32:52,030 --> 00:32:53,060
So what will happen.

443
00:32:53,140 --> 00:32:55,280
So I will get an eddy.

444
00:32:55,540 --> 00:32:56,400
Now this time.

445
00:32:56,470 --> 00:32:57,780
Pointing towards this eddy.

446
00:32:58,240 --> 00:33:03,450
So how do we dilute entity if you want to delete one element.

447
00:33:03,470 --> 00:33:06,790
This is the syntax but if you want to delete entity you have to give.

448
00:33:06,890 --> 00:33:08,690
You have to write it like this.

449
00:33:08,690 --> 00:33:11,610
So this is the syntax for deleting any.

450
00:33:11,780 --> 00:33:14,970
So this is for deleting Eddie.

451
00:33:15,020 --> 00:33:17,960
Okay so this is for single element deletion.

452
00:33:17,960 --> 00:33:24,080
So what will happen go to delete B and the meaning of this is actually so BS an idea or destroy the

453
00:33:24,080 --> 00:33:24,830
whole Eddy.

454
00:33:24,860 --> 00:33:29,270
So go to the first memory location and destroy the complete Eddy.

455
00:33:29,360 --> 00:33:30,350
So let's see in.

456
00:33:31,730 --> 00:33:33,410
So again it is very simple.

457
00:33:33,460 --> 00:33:34,040
You can do

458
00:33:36,930 --> 00:33:45,780
and start B is new int now I want to delete the memory and so delete B now after deleting the b you

459
00:33:45,780 --> 00:33:51,630
can again allocate the memory so B and again delete the memories or delete B.

460
00:33:51,690 --> 00:33:56,090
Now you can again allocate the memory so and let's say 50.

461
00:33:56,580 --> 00:34:01,730
So to delete identity you have to write this index this one you have to give this a squared records

462
00:34:01,740 --> 00:34:03,760
and delete B.

463
00:34:03,870 --> 00:34:11,170
So this is this index for deleting 80 so delete at is index

464
00:34:18,120 --> 00:34:23,830
and this is just index for deleting one single element so delete single.

465
00:34:27,440 --> 00:34:32,680
Element so this is all for this video I will see you in the next one.
