Hello everyone.
So in this lesson we will learn dynamic allocation of 2D eddies.
So first of all how we will create one be added dynamically.
So I will write you end and five simple but I want to create the edit.
So you might be thinking this index will be new and let's say 10 rows and columns.
But this index is not right.
This is not the way we create two data dynamically.
So what do we want to do differently.
So we want 10 rules should be there and 20 columns should be there.
So this is our requirement that 10 rows and 20 columns.
So basically we can say that our identities.
So ultimately we can say what the studio is or it is just a combination of areas.
So I can't say there are 10 it is and each of size 20.
So let's call it even.
And this is it then.
So there are identities and each of size 20.
So what I want to do.
So to create just one Eddie to clear just one area.
What I have to lose.
I will use this index.
So if I will write new into 20 because I want my area of size 20.
So if I want to clear just one Eddie what this new thing will have done.
So this will return me the address of the first element.
So basically two older than me in Star similarly if I want to create an area of size 20 I will use this
above index.
So this is in star be new it and 20.
I want 20 columns.
Basically I want to create an array of size 20.
So again it will have done me the element it within me that does with the first element.
So it is going to return and start similarly.
This is going to be done in start and similarly this is also going to have done me the first element
that was the first element.
So this is going to have done me in start.
Correct.
Now what I have to do so basically I have to store all these addresses
if you want to create a truly array and so you have to store all that.
This is now to store all that.
This is where to do so I will create an 80 often size 10.
And each element will be installed.
So each element will be in in integer point.
So I'm going to create an area of integer pointer.
So this is very often disappointed.
And the size is going to be then.
So how do you create an area of in digits.
So first you write to do the type.
So in our case that it diapers in Star that it debuts in Star then we type start.
So start then we write be basically the name then we write to do then the data type.
So in this case I'm creating additive in Tejas.
But here I want to create Adi often being appointed so I will write new in Star data type.
So this is not easy to type.
And then the size is so I want to create an area of size 10 so this way if we will use this approach
I will be able to create this Eddie.
Now after doing this we have to create all these eddies we have to create all these areas.
So can I write b 0 is new into 20.
Similarly I will write B1 is new into 20s and this will go on at the last.
I will write B nine is new end and 20.
So these are all in beta ready.
So these are all in D ready so that's why I have to do something like this and it is obviously that
I will not write like this I will obviously use a loop so I will just write data what this Eddie and
I will use this I will write this simple now after doing this for example if you want to access to for
example after doing all the hard work if you want to access be a four and five.
So what is our structure.
So I was structure is this is an area of integer pointer this is in a different digit pointer.
This is you going to be your b's binding towards the first element and for each index you have an 80
so.
So each index is storing the address of the first element of its Eddy.
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
and then go to the fifth index.
So you will go to the fifth index.
Simple so finally what is my situation.
So this is you're in Deja appointed
and each index is distorting and address affinity
each index is storing the address of vanity
and all this work is basically all the memory is basically the heap memory so all this is heap.
So what is stack memory so in stack memory what do we have that double point.
So this is B since you're disappointed it really be of a debate and it is playing towards it distorting
the address of the first element.
So this is the structure and what is the goal.
So first of all we have to create this integer ready so for creating this and B the address index is
very simple.
This will be an area of integer point and so forth that is the type that I believe this one.
Then you have the right start operator because you want to store the address then the name of the area
and let's say B.
So this is new data diapers in Deja 2.0.
So each element is an integer point that because it is going to store the index of the first element
and let's say I want to create any of size 10.
Similarly what we have to do so you have to hydrate or what this salary.
So let s I read or disagree.
So I equals zero i.e. less than 10 a plus plus.
What are you going to do.
You have to create this wouldn't be added.
So for creating this one lira you will write B of A is new.
And 20.
So finally if you will do this.
What will happen.
This is seem as if you want to lose technical locations or this is B of 10 and 20.
Okay so this is how we allocate to the area dynamically.
So let us write the code.
So first of all I need to add a often digit point.
So now I started being w pointer Then new in start and led to the size is 10.
So I want to create an eddy of 10 rows and 20 columns to the area of 10 those and 20 columns.
Now I just have to wait wait i equals zero.
I list then then I bless less and now be off I is new top 20.
So I've created an array of vendors and 20 columns to the ad often losing 20 columns.
So for example if I want to take the rows and columns input from the user so you can definitely do like
this in and my nine rows and columns.
So give me the value of those.
Give me the value of columns.
So after doing this don't happen.
So this will be your m. you will I trade on the middle floors and this is your columns.
Simple.
Now after doing this you want to take input.
So for taking the input we remembered i equals zero 9 less than name I plus press then similarly j equals
zero g list and energy plus plus.
And you will simply write B of A G.
So this is how we will take input or instead of writing this whole code what you can do so you can write
here only.
So let us comment out this one
so you can write.
You can likely take input here only I do to all those that is so lightweight I am editing over those
add is that we have created in the above line.
So I will take you off AIG simple.
So what is happening here.
Okay so what is the meaning of this line.
So you're creating an additive in disappointed
so the sizes M so each and B's an integer point and then what they're doing you are doing all this at
a and b if I use new ink.
And so this is why I'm thinking towards I am creating a new Eddy.
So after creating the idea of size n I am I creating all this eddy.
So I related to what this edit and I am taking input b if I j.
Similarly you would again create entity of size and then you would like it or what do you say and you
will dig the input then you will again create an area of size n then you will rotate over this idea
and you will take input then you will again I refer to the.
You will again create an area of size n and do related all the area and you will take input.
Now one thing.
So here what we are doing.
So we are creating basically a matrix.
So I will have 20 rows and 20 columns.
So basically each row has same amount of columns so each row is having 20 columns.
But we can do what we can do so we can create a structure like so we can do something like this.
So what is the meaning.
So the first rule is containing only one column.
The second lowest containing only two columns or you can say it will contain only two element.
The third row will contain three elements.
Basically the site the number of columns would be three and the fourth column for total will contain
four elements.
And the fifth row will contain five elements so we can definitely create a structure like this.
So basically what.
What will happen to destructively become what do you do.
So you have Ambrose.
Here we are giving a fixed value and we are giving and let's say you will change the value.
So it will change the value.
So the first rule the size is 1.
Now the size is 2.
Now the size is 3 now the size is 4.
So this is the power of dynamic allocation you can create a structure like this.
So let's do this.
So instead of n where you can do you can write a plus one.
So if the value of i.e. zero the number of columns will become 1 if the value if I ever become when
the number of columns will become 2 and we will obtain a structure like this.
So I was to actively become like this
but most of the time.
What will happen.
We will simply create this matrix only each and all will have seen on tough columns each row are going
to have same number of columns but we can definitely do like this.
There is nothing that can stop you from doing this.
Okay so no one can stop you from doing this.
Okay so what else is remaining.
So I think we have used dynamic memory allocation and dynamic memory.
V have to delete ourself manually deletion.
Always remember.
So just remember one thing whenever you are using new whenever you are using new.
Always used delete.
So we have to delete the memory out ourself.
So what we're doing here we are creating IPG pointed at different digit points.
So let's just delete.
Let's just delete our diary so delete.
Be if you will leave B do you think this is right.
We did delete.
I don't think so.
Why.
Because what is our structure our structure is first.
We have an area of integer pointer.
First we haven't had in often digital pointer and each and at each index I am storing the address of
an 80.
So each element of the integer point that is storing the address of an 80.
So this is my stuff.
Basically.
Okay so this is in digital a pointer and these are these are a lot in Delia.
So this whole thing is actually inside a heap this whole thing is actually inside the heap and inside
stack we do have.
So this is your stack memory inside stack.
I have it by itself memory and this is B and Bs containing the address of the first element.
So if this is the first element let's say it's addresses and so B's and storing hundred or against a
piece pointing towards the the address of the 0 8 element fine.
Now what I want to do so I want to delete this whole memory.
So if you will write delete B if you will write delete B then what will happen.
So basically this Eddie will get deleted this Eddie will get deleted and then you can not delete this
why we can not delete these eddies because once you have deleted this memory.
Now this memory does not belong to you and we discussed in the previous lesson and the previous we do
that after deleting this you should not try to access that memory again because that memory now does
not belong to you.
So what we have to do.
So first of all we have to delete these eddies we have to delete these areas and after deleting all
that is we will delete this.
Finally.
Okay so what is our approach.
So we know this is wrong because if you delete this big eddy then there is no way for us to delete these
small eddies.
So our logic is very simple our approach is very simple.
First I tripped over this eddy and called the lead on these small small eddies after deleting all the
errors.
We will delete this big begetting simple so let's do this.
So before deleting the big eddy before deleting the diesel point ready we have to hydrate and we have
to delete small small eddies so I equals zero.
We have to delete in B.J..
So I call you two I left in him.
I bless bless my dad.
Do we have to call delete function delete be off.
We are deleting small one that is since BFI.
Would this be if I use a -- is an eddy.
It does point towards entity since it does entity.
I have to write like this.
Now this is correct.
OK you can give space or you do not give space.
Both are correct.
So first we have to delete all the individual areas and then we will delete our in D or appointed any.
OK so.
Do not forget to delete the memory which you have allocated using the dining memory location.
So always remember this.
This is a good practice so if you have any doubt in this lesson you can ask me.
I will see you in the next video.
Bye bye.