Hi everyone.
In this lesson we are going to learn what is dynamic memory allocation.
So let's start today's class.
So let us discuss what is the biggest problem with the ADI.
So whenever you want to create an entity you have to give the size.
So let's say I want to create an area which will contain 15 digits at the max.
So this 50 is actually a fixed number.
So basically we are creating a fixed sized Eddie.
Now we can also do like this in 10 seeing and and then into a and we can definitely do like this.
But ideally we should never do this.
No C++ does not get that this thing will work.
It may work on some compiler and it may not work on other compilers so there is no guarantee that this
thing will work.
So that's why we should never use this.
We should never create our Eddie.
So this is and is actually a variable.
So basically what we are doing we are creating a variable sized Eddie and in this approach we were creating
a fixed sized Eddie.
So this is hundred percent correct but there are problem with this way of creating Eddie.
So we are creating a variable sized Eddie and the value of an.
So the value of end will be given by the user at runtime.
So when our program will be running then the user will give what is the value of n and then we are creating
Eddie of that size.
So this is actually wrong and in this time.
So this is fixed sized Eddie.
So this is fixed as it is.
This is compile time we know Ed the compile demo leader.
How much memory we required.
But this is runtime.
So we do not know how much memory our Eddie will take.
So there are actually two types of memory allocation.
So there are actually two types of memory stack memory and heap memory.
So let's see what is a stack and heap memory.
So the first type of memory is stack.
And the second type of memory is heap so stack memory is small and a heap memory is very big as compared
to the stack memory now whenever you do something like into a question or you do something like into
a 20.
So actually these two things.
So the memory is going to be allocated on the stack.
Fine.
Now even if you do this I want to create an array of ten thousand sites.
So in this case again the memory is going to be allocated on stack.
So what happens is whenever I a program run.
So when I let a program start running it will take some along the stack memory and it will run the program
in this case when you're creating a big sized area.
So I'm creating a big error so I know at the compile family that I need a big memory I need a large
memory.
So what will happen.
Our program will take comparatively a bigger stack.
It will come it will comparatively take a bigger stick to hold this much amount of memory.
And then our program will run.
So I mean I'm building myself.
So whenever a system runs our program what will happen.
So system will take some amount of stack memory and it will run our program.
So even if you want to create an array of 10000 size.
So the system knows at the compile time will lead that you're creating this big error.
So you need a bigger stack memory.
So the system will allocate us a bigger aspect memory and it will run our program perfectly fine.
So the problem occurs when you will do something like this in an CNN now and in again.
So this memory is going to be located on heap only sorry on stack.
This memory is going to be allocated on a stack only.
The problem will occur if the user will give the value of n to be a very large number.
Let's return to the power six.
So what will happen.
System will take some amount of spec memory and it will run our program and add the runtime user can
give the value of n let's say a very large number.
So if user give a very large number as input then basically you expect will not have that much amount
of memory to store this much big along very.
So this much begetting I am repeating myself the problem will occur if you will do something like this
so our system will run our program with some amount of stack memory fine.
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
very large number then what will happen your stack will not have enough amount of memory to hold this
Big Eddie.
So your program may crash.
Now the obvious solution for this problem is C heap memory is a very big memory so you can allocate
your Eddie in this memory if you want to do something like this then look at the memory in heap because
heap is much much larger than Stack and one more thing the value of n is given by the user at runtime
so the stack space.
So this stack size could not be increased during that time okay.
We cannot increase our stack memory during runtime so the obvious solution is add brand name allocate
the memory inside heap because heap is much much bigger than stack.
So this added can easily be stored inside heap.
So this is called heap memory allocation
heap memory allocation and the second name of heat memory allocation is actually dynamic memory allocation
so let's see.
Let us differentiate what is dynamic memory allocation.
So we have stack memory and we have heat memory so in stack memory when we do something like this in
day.
So everything is going to be stored inside my spec memory.
So this is called static allocation starting a location means your data is going to be stored on a stack
memory.
Now in heat memory.
So if you want to store something heat memory then we call it dynamic memory allocation or you can say
heat memory allocation or dynamic memory allocation so this memory is actually the memory is allocated
during compile time so you can see you can write memory allocated
during compile time
because in date.
So this will get compiled and memory will get allocated at compile time only now a heap.
So you want to create a variable sized Eddie.
You want to do something like this.
And basically you want to create a variable sized Eddie.
So that means so the value of end will be given by the user runtime.
So it is called runtime memory allocation random memory allocation or you can say the memory will get
allocated at runtime.
So this is not actually this index for allocating memory on inside heap.
You will see this index but this is not the right index.
Now the stack memory is much smaller than the heap memory.
So this is less and my heap memory is very large memory so heap is a very large memory.
So what I want to do I want to do something like this.
So with the current knowledge that we have we only know how to allocate memory inside stack.
We don't know how to allocate memory inside heap.
So now let us learn dynamic allocation how to allocate memory inside heap so let's see so if you will
do something like this entire question.
So what will happen this AI is going to be stored inside my stack memory.
This idea will be stored inside these tech memory fine.
Now what I want to do I want to store something inside my memory.
So how can I write this index.
So what is this index.
So what you have to write like this new and you want to create and Deja.
So you will write new and so with this statement so new is actually a keyword new the keyword.
So this new and what will happen you will get four bytes of memory space inside you and heap you will
get four bytes of memory space inside your heap.
Now let's say you want to insert 10 you want to put pen here but this memory doesn't have a name it
doesn't have a name.
So here if I want to do something with a so I have a name but this doesn't have a name.
So how can we put 10 inside these four bytes.
So actually this new thing it returns the address of this memory.
So if I do this let's say it addresses hundred.
So this new thing will return me the address.
And for storing the address we started pointers.
So I will write it like this and start I.
So I will write it like this in START I is new and
so what is the effect of this statement but it might really with the effect of the slain.
So new is going to be done the address of the memory inside heap.
So what will happen.
So this new and what it will do so it will go inside the heap memory and it will create four bytes it
will give me four bytes and the let's say it does is hundred.
So this I think so this I use a pointer so what is happening this is U.S. tech.
This is U.S. tech memory and science tech memory you have created guy Deja 2.0.
So this is your digital point that you disk containing and it just ended and we know point does sod
off you two bytes so add this line total bill bytes of memory are allocated out of tool you two bytes
are allocated on stack and four bytes are allocated on the heap now if you want to put something inside
this memory this index is quite simple do it do it like this.
I equals ten So ten will be inserted here.
Okay so this I is actually pointing towards this heap memory address.
So if you want to insert then you can do it like this spot I equals 10.
Similarly if you want to create a double so you will write it like this.
Double Star P is new double
again what will happen.
So this is good heat memory
so double is off eight bytes.
This is a debate.
This is U.S. tech memory inside stack.
You are storing double pointer.
So this is B let's say it addresses hundred so it is storing at this hundred and it is pointing towards
this if you want to put something if you want to put something inside this box you will do it like this
star B equals three point fold simple.
So let's write the code and let's see
so into start B is new keywords so new is a keyword.
And so at this line how much amount of memory space is allocated.
So 8 bytes on stack because you are creating AI integer pointer and for byte 1 heap now how to access
heap memory ERP because bees containing bees containing the address of the memory so start to be closed
down and let's say you want to print the content.
So if you would like to see out bee it will print the address if you want to paint the content you have
to use deet offense operator
so Lucy out start bee
so this is the address of the memory which is located inside heap and this is the value
similarly what you can do it can also do it like this so double star D is new double
similarly you can do good start C is new get so this new thing it is are dunning me address of the memory
so again I'm repeating myself so when you do something like this many will write the statement like
this and start B is new int So what's going to happen memory will be located inside the heap and it
will also be located inside the stack.
So this is just tech stack memory is smaller than heap memory.
So new and so you will get four bytes here this is your four bytes start B.
So this is you're in Deja point to a debate and B is pointing towards this memory location.
So let's say if you want to put something here let's say 20 so you have to write star B equals twenty
so first start P means D defense better first and go to the memory location and then put 20 did fine.
So this new is actually a keyword.
And where does the address off this memory does of the memory which is located inside heap.
OK so now let's see how we can allocate at a dynamically how we can put Eddie inside the heap.
So I want to create entity inside the heap.
So let's see what will be this index.
So this index is quite simple you have to write it like this and start e is new in 50.
So what will happen again.
This is you to heap memory
so new int and 50 so 50 means forward into 50.
That means you will get it to wondered bytes of data so you will get an net inside the heap.
This is your daddy index zero index 49 and this new thing what it will get done it will have done the
address of the first element it will be done the address of the first element and I have created an
digital pointer to store that address simple.
So now we know how to create added dynamically.
So after knowing this we can definitely do something like this.
So I can write into n CNN and then I will write it like this in start.
E is new and and so now this is Connect.
So what will happen the value of end value given by the user at runtime and at that time what will happen
at that time.
We can allocate memory inside the heap.
So the value of N will be given by the user.
And then you will create an array of sized and inside the heap.
So let's say the value of n is 50.
So this will be at index 50.
So this will be an index 49 and this is your index is you.
Okay.
So now you can write like this.
So this is perfectly fine.
So what did happen.
Program our system will take some stack memory and it will then the program.
And if you want to allocate the memory at that time then allocate the memory inside the heap because
each memory is much much larger than distinct memory.
So now our problem has been solved.
So there were two problems with daddy.
So there does discuss the edit problems and let us discuss how discord will help us to solve that problem
so and has proven them.
So the first problem is let's say I have created at a offsite thousand and let's say I am doing this
in then in.
And then I am doing I equals zero.
I listed in a plus place and I am doing the scene if I now let's say the value of Aniston.
Let's say the value often given by the user is 10.
So what would happen.
I have a good memory four thousand elements but I will use memory only for 10 elements.
So basically the elements.
So how much amount of memory space I am wasting here.
So 9 and elements multiply each element.
So each integer will take four bytes.
So this much amount of memory space will get wasted if I will use the static allocation.
If I'm using this a location so this is the first problem of the areas that we have a lot of amount
of memory wastage.
If we are creating a fixed sized Eddie and with the help of dining memory allocation we will not waste
any space.
So our first problem will get resolved with the help of dynamic memory allocation.
Now what is the second problem with daddy.
So the second problem is let's say you are creating an array of size 10.
Then again you will do the same thing and then you will take how many number of elements you want to
insert.
And again you will be equals you know less than I plus plus and you will do something like this scene
a if.
Now let's say the value of randomly given by the user that in time now led to the value of and is ended.
If you give the value of hundred then what will happen.
Your program will crash because you can add the max a store then elements and the user has given the
value of N hundred so your program is going to be correct.
So again if you will use a dynamic memory allocation.
So the second problem of daddy will get resolved.
So it is the power of dynamic memory allocation.
What the problems associated with Daddy.
Will get resolved.
We will not waste any memory.
And also we will not face problem of this type so dynamic memory allocation is really really good.
So after creating the eddy how can it put the elements inside.
So just like the state to get it you can do it like this.
If 0 is 10 so then will be here.
You can write like this.
So each of 20 is let's say 40.
So you can add the next 20.
I will insert 40 so you're going definitely do it like this.
Just like the.
Just like the state to get a vibe because.
So what is it if I so I'll fight is same as the start of E plus I
so that's why you can write this index like this.
So this is for the inside so let's say I want to find out the largest element given and every find the
largest element inside the 80.
So let's solve this question okay.
So let us write the code for finding out the largest element
so n n seen and and start a is new int and now what will happen.
So the value of end will be given by the user at one time.
So this edit is going to be present inside the heap.
We are going to create that inside heap and 8 bytes will be allocated on the stack because we are getting
a digital pointer.
So now let us take input from the user.
So like was 0 I lost and in a plus plus seen a 0 5
and now let us try to find out the maximum element.
So initially let's say the largest element is minus 1.
For now let's assume that the user will give all the positive elements as input so I equals zero.
I listed in a plus plus.
So if the current element.
So if you if I if the current element is good then the largest element then you can update your largest
element.
And finally you can bring your largest element
so let's an area called okay.
So here I forgot to add 7 column
so there are 5 elements and the element solid C 1 2 3 5 and 4.
So 5 is the largest element civil code is working fine.
Now tell me how much memory is going to be allocated if I write something like this.
So if I write and start a is new and 50.
So how much memory will get allocated.
So there are two parts.
First the ad is going to be created inside the heap of 50 sites.
So inside the heap how much memory will be allocated.
So 50 in 2 4.
So two hundred bytes are going to be allocated on heap and on stack.
How much memory will be allocated.
So we are creating a integer pointer.
So this is a.
So even right.
So in totally add this line two hundred eight bytes will be allocated.
Fine.
Now let us see one more difference between the static and dynamic allocation and the most important
difference.
So for example this is used to mean and let's say this is you if and inside.
If you are writing int i and yet if will end here.
So there are some more line of code.
So what will happen.
So as soon as this I point out it will reach this line.
What will happen.
So the memory will be automatically deleted.
So as soon as the scope of the variable i finish its memory will be delegated.
So this is static allocation.
So this is stack.
So this a memory.
So this i.e. variable is going to be present inside the stack and as soon as I will reached this line.
So the memory allocated.
So basically the four bytes allocated to the variability will get deleted automatically.
But this is not the case with the dynamic and location.
So if you will write it like this.
If then if you were creating a dynamic memory if you are using dynamic communication then what will
happen.
So this i.e..
So this variable ie it doesn't have any scope so no scope is there for a variable.
Because this is heap memory allocation dynamic memory location so it will not get deleted.
So the biggest difference between the static memory allocation and the dynamic memory allocation is
so if you are allocating memory statically for example in die then what will happen.
So they will be automatic release of memory on bases of scope
whereas in the dining memory relegation there is no concept of scope.
So if you are doing something like this in study is new and so what you have to do.
So there is no scope there is no concept of scope.
So you have to manually release the memory.
You have to manually release the memory.
So what will happen if I will do something like this.
For example
vital through
a and similarly what will happen if I will do something like this while to in start I is new and so
what will happen if I will run this code so you entered the VI loop.
So first of all this loop is vital.
So that means this is going to run in finite times.
So this is in finite loop then what will happen.
So as soon as you reach this line 4 bytes will be allocated inside your stack memory then you will go
above.
So as soon as you will go above this 4 bytes idea located and then you will again reach this line.
So again the 4 bytes will be allocated.
Then again you will go up.
So this 4 bytes will be deleted.
Then you will come here again so 4 bytes will be allocated again.
Then you will go up again.
So 4 bytes will be deleted.
So this will go on.
So basically at max so at any point of time at max you will.
You are using food by itself memory space so your program will look perfectly fine no doubt.
Now let us consider this case what will happen if I will run Discworld so enter this.
So as soon as you reach this line what will happen.
So eat bites are allocated inside you and stack memory because you are creating and Deja pointer and
4 bytes are allocated inside your heap.
So inside heap 4 bytes are allocated.
Now you will reach this line.
So this 8 bytes will be dedicated.
Then you will again go up.
You will reach this line so again 8 bytes will be allocated for Stack.
But this for bytes are not deleted.
So again you are writing new int so the 4 bytes will get allocated again then you will reach this line.
So this 8 bytes will be allocated because this is stack.
Memory allocation.
Then again you will go up.
You will reach this line so again 8 bytes will be allocated on stack memory and new and so forth.
Bytes will be allocated on heap again.
So again you will reach this line.
So this 8 bytes will be d allocated.
And again you will go up.
You will reach this line so again 8 bytes will be allocated inside to stack memory and 4 plus 4 here.
Because new integer.
So this will go on.
So ultimately what will happen.
Your memory usage is increasing your memory usage is continuously increasing and at some point of time
what will happen.
Your app your program will crash.
At some point of time our program will crash.
So basically if you will write code like this
so if I will do something like this
and if I really lose something like this.
So what will happen.
So this is your homework.
What you will do.
So first you will run this piece of code and you will command out this piece of code.
So if you will read this piece of code what you will do is you will open your activity monitor or you
will open your task manager and you will notice what is your memory usage.
So in this case your memory usage will be constant because at max we will be using for by of memory
space.
Now command out this code and run this code run this three pieces off line then this three lines what
will happen again.
You will open your task manager or activity monitor and then you will again check your memory usage
so your memory usage will keep increasing.
So your memory usage will keep increasing.
And finally your program is going to be crashed because of lack of memory so you can do this.
So for now I am commending our discord
so what is happening here why it is happening because we discussed about that.
The static memory will be deleted automatically.
But if you want or delete the heap memory you have to manually release the memory if you want to delete
the heap memory.
You have to manually believed that memory.
Now how we can manually delete the memory.
So just like a new keyword.
We also have a keyword delete.
So what they are doing here.
And start B is new and so what will happen.
This is you your heat memory D idea for bytes
fine.
This is U.S. tech memory these are 8 bytes so p and eight bytes so what is B is pointing towards this
memory location and I want to delete this memory because this memory we have to lose this memory and
this memory will get deleted automatically as soon as it will reach out of scope.
So for deleting the memory we have a keyword delete and we have to give the address.
So let's say that this is hundred so various address present addresses presented so you will write to
delete b you will write delete B.
Now after this line get executed what will happen.
So delete B means go to this address.
So go to this address and delete the memory so delete the memory.
Now the memory has been deleted so this be pointer.
So this memory will not get deleted.
Just remember many people have this confusion that this memory will also get deleted.
No this memory will be deleted automatically then it will reach out of scope.
So basically we are deleting this memory simple.
Now one thing after deleting the memory.
Please do not try to system and again.
So do not try to do this.
Why.
Because if you will try to do this.
What they are doing.
So this memory has been deleted.
So you are going to this address and basically this memory is now not belonging to us.
This memory does not belongs to us and you are trying to put 10 inside this memory.
So this is very dangerous.
So do not try to do this.
So do not try to do this because this is very dangerous because after deleting the memory the memory
does not belongs to us.
So do not try to do something like this now one more thing.
So we have deleted this memory.
We can also do like this after deleting the memory after deleting the memory.
We will never do this and we can also allocate new memory so I can write new and also I can write new
it again.
So what will happen.
I will get an idea for byte and now this people point here.
Then again I can do delete B so this memory will get deleted and then again what can I do.
I can write.
So this is only b not start B.
So again I can write new intended.
So what will happen.
So I will get an eddy.
Now this time.
Pointing towards this eddy.
So how do we dilute entity if you want to delete one element.
This is the syntax but if you want to delete entity you have to give.
You have to write it like this.
So this is the syntax for deleting any.
So this is for deleting Eddie.
Okay so this is for single element deletion.
So what will happen go to delete B and the meaning of this is actually so BS an idea or destroy the
whole Eddy.
So go to the first memory location and destroy the complete Eddy.
So let's see in.
So again it is very simple.
You can do
and start B is new int now I want to delete the memory and so delete B now after deleting the b you
can again allocate the memory so B and again delete the memories or delete B.
Now you can again allocate the memory so and let's say 50.
So to delete identity you have to write this index this one you have to give this a squared records
and delete B.
So this is this index for deleting 80 so delete at is index
and this is just index for deleting one single element so delete single.
Element so this is all for this video I will see you in the next one.