Coding/General

What Are Dynamic Arrays?

What Are Dynamic Arrays?
What Are Dynamic Arrays?

Alexis your back I didn’t scare you away. No, you didn’t. So I will let me try to hide it so. One thing that we talked about in the previous video was about pointers and how to make pointed different variables, but did you know that we can make a point to point to a variable without even creating a variable or appointed. well. yeah, that’s correct. We can dynamically create variables using pointers. How do you do that Dave?

The way you do, that is, you are going to use a new keyword card new. fancy. And so what new do is new basically allocates. memory. of whatever we tell it to it’s going to allocate it right, but we need a pointer to be able to do that. Now, the problem with allocating memory is when we allocate memory dynamically right, meaning that we do not do that.

We only do that using pointers why we use point is to go and find a place in memory and the static memory. aside from not understanding, we’re going to do it in our free store which I’ll talk about in a second. When we have when we allocate memory in the static memory right that’s when the compiler that the compiler manages that memory. And remember, I said, another video of the compounding knows when to actually delete that variable window like that variable over.

However, when we use dynamic variables, the compiler has no idea when the lack of that variable is done.


So then, how do you release the allocated memory. that’s a great question i’m glad you asked new is not a single new has a spouse. A spouse. yeah and who has a spouse yeah, this is probably one of the one of the few key words that actually have a car in a committed relationship. Would you like to meet new significant other. Of course, all right, well, I would like to introduce you to delete.

And guess what delete does. It releases the allocated memory. Absolutely, yes, it would be allocate the memory. So they are a jealous couple new and delete i’m jealous couple, and so, if you’re going to use new then delete the man’s that you use. delete as well. Are there any instances where that’s not true. There is no instances where that’s true well that’s not true right there can be a time where we use delete we use that new and then the program in and when a program is, of course, everything is still a data memory, but we never want to do that, you know why. Why because that’s bad practice. That.

it’s bad practice so we always want to make sure that when we use new that we use delete because what will happen is, we will be allocate memory and never free that memory up. right we allocate memory and never free it up, what do you think what happened.

We continue to allocate memory and never be allocated. shits gonna go bad. Go bad absolutely. we’re going to run out of memory right our computer only has so much memory that it can use right, I mean if you have a really, really powerful computer you have 16 gigabytes of memory, but that can get used up very, very quickly. So let’s talk about how we would allocate memory right so inside of our previous video we talked about pointers right, and so we can create a pointer simply by using the Asterix and this actually tells us that this variable is now a pointer that can point to integers.

But not only is it a point to that can point to integers it is actually a variable that can create integers in the free store, meaning that we can create an integer in a memory without happen to say something like X equals five. Right, we can we can do that without saying it X equal five So how do you think we do that.

Maybe you could do in pointer equals new. In. Absolutely, if we want to create a new integer right we just say in pointer equals new ED absolutely right, because, as we see here new means that it allocates memory, so what this pointer is doing is now finding some memory address and putting a new interview there.

Well, now well that’s sucks because I don’t have a name for that integers So how do I make changes today integer. 13th reference operate. Absolutely ID reference operator. or be reference operator will say that we can set. That value that new integer to. Five, we can change the value. So let’s just kind of look at what’s happening in the memory. So, in the previous video, we had a little Convention like this, where we have name, we have value, and we have the address.

Right, so when we create our pointer right let’s say a pointer gets created as 5000. As soon as we use the new. keywords keyword. we’re going to go and find a place in memory to hold space for an integer right we said new integer so we’re going to go find a place or a new integer. But we didn’t create an identifier, for it right, so we don’t really know what the name of that variable is, but what we do know is that a you know it has an address and it’s able to hold a value.

So. This pointer is now pointing to this new integer that are dynamically created So what does that mean for the value of this point. So value would be. The memory address of that new integer that you created, which would be 1000. yeah that’s absolutely correct that would be 1000 so now, even though we don’t see that variable that created it did create this variable inside of the memory and then also made it so that it’s pointing to that new variable that it made.

So let’s demonstrate why new why delete is so jealous of new and wants to make sure that it’s always there. So let’s go ahead and do a let’s make an example so let’s say I create a for loop. Right and let’s say inside of my loop i’m going to say pointer is equal to a new entity. So let’s just kind of trace that right so here’s pointer right now is pointing at this whatever we allocated so now what happens when I is equal to zero what is what happens, what happens when is equal to zero.

You create a new integer in memory. Alright, so I created a new integer in memory. And they let’s say that that 1001 and then, what does it what happens to the value of. pointed. It gets changed to whatever that integers belt addresses. All right, so that means that this value here is going to change to the new variable that it created.

Yes. That we’re going to go ahead and change this to 1001. Yes. All right now, what happens when. When is equal to one. It goes ahead and creates another integer in memory.

Alright, so creates another integer and memory and let’s say that this time and puts it at 3007 so, then what happens to. The value of pointer. it’s going to change to 3007. it’s going to change the 3007 and I don’t we don’t need to go through. Five times to see what’s happening right. yeah. What do we see happening right now. what’s happening is that you keep allocating space for these integers and then there’s no real way of going back and like deleting or T allocating that memory.

that’s correct there’s no way to go back right because the pointer can only hold the value of one agile right because it’s only pointing to one agile. And then, when we change that address, then we no longer are pointing to the previous thing at that point, it was pointing to So what does that mean for this all this good stuff what does it mean for these two variables. And they never get to lead it. yeah they just stuck in a memory right so when we look to five times oh big deal, but what we decided that we were going to lose 5 million times. Your computer’s gonna pray.

pray pray guys got can be very happy about that right so so that’s why I delete says hey if you’re going to ever use my spouse, then you better make sure that you use me as well, so we can prevent that. So how do you think we use, how do we, how do you think we use lily. I assume, right after that pointer equals new in statement. So Okay, so we can put it there. We could put it there, so how do you how do, how would, how do you think we write down, what do you think the syntax is for deleting a pointer. Maybe just delete pointer.

yeah absolutely we just say delete and then the name of our pointer. And so what happens now so let’s actually just kind of backup right let’s go back to zero is equal to one let’s let’s go ahead and start all over again. And let’s trace this algorithm.

Alright, so so now let’s say zero is equal to is equal to zero so what’s going to happen first. you’re going to create an integer. And let’s say it does it at 1005 and then what happens to the value of pointed. The pointers value goes to 1005. Absolutely, so we have 1005, then the next thing that happens is we call delete. So that means that we are going to now D allocate this memory right so just for demonstration purposes, that means that we’re going to go and say hey you know what this memory address is now free you can go a sign. Another integer there another variable this memory address is open for business.

So does that mean that pointer is pointing to nothing. So that’s a great question, it means that point two is still pointing to this address but it’s now pointing to an address that is not reserved. Is that. It doesn’t have a value. Not that it doesn’t have a value, but it means that it’s not reserved, meaning that that that that memory address is up for grabs. Okay. yeah so that means that we we really know have no longer have control of that point here because it’s up for grabs it can get changed to whatever by something else, and we have no power over that so it’s just basically freeing up that made me adjust.

Okay. All right, so what happens when. X is equal to one I mean sorry is equal to one. create a new integer and memory. created a memory right so we’ll go and create one here let’s say it goes to 3001 and then what happens to point it. gets changed 3001. Absolutely. And now we call the lead and what happens is. The integer that we just allocated gets freed up so any thing can use it yeah. So. With that said. Do you think that this is kind of useful what’s the point of us, creating a variable and then delete it right away. I don’t think it’s very useful. yeah it’s not very useful it’s not very useful so usually you want to make sure that when you create a variable you created for the reason. You use that variable for whatever reason you needed it for right you recognize when that the life of that variable you recognize when it’s time to kill that variable. Right, just as a compiler knows to do it when we get to the end of a scope, you have to figure out when the scope is complete and then at that time that’s when you actually call your delete. yeah that makes a lot of sense. Alright, so that’s So those are the happy couple new and delete.

Now let’s talk about uh let’s let’s get a little uh you know let’s get a little crazy right, you know we can create one integer about so what Why do I need a pointer to create an integer one I just created a damn into doing call it a day. and say possible to do it with the rays. Absolutely, we can do it with a race right, we can do it with a razor let’s just talk about a raise for a second right let’s just talk about arrays we love arrays, but we also kind of hate arrays right, why do we hate array sometimes. Because they need a definite size. Because they need a definite size, yes, we is a big size. What else. um it’s really hard to insert. Can you insert into an array.

You can write with partial arrays right, you need partial raise to be able to answer. nothing’s really coming to mind about why we hate rates. So big side is a really big one oh, what about. What about you know. Wanting to use our a function. You can’t you can’t really use it there’s no. You can’t really return arrays so. You can’t. Like change the value within that array without. i’m assuming needing a pointer. yeah absolutely absolutely what of what happens if we have an array and we we the REG it’s bigger than we expected. Then we start going into memory that is not ours. yeah yeah so we can we can definitely get out of bounds easily right. yeah. But also, you know if we were we were really careful with the managing our memory if we got out of bounds right we if we have more items that we need right, then our array can hold then we’ll, we have to do is, we have to create another array.

Right, we have to create another resizing. Size it right and then copy everything over to a bigger array. Right and that’s going to be really hard to manage inside of your program because now all of a sudden this name has an identifier this array has a identifier. Right, and now we get a new identifier for this array and now we have to write our code, so it says all right if it gets bigger then we’re going to change the name of our Ray and. We can do an example of that and see how it’s going to be nearly impossible to do and it’s going to make you if you have a lot of hair you’re going to lose a lot of hair trying to trying to do something like that.

A lot of tears. lot of tears absolutely. So we can fix that by doing dynamic arrays. Like you said earlier, yes, we can actually use our pointers to dynamically create raise we raised we don’t just need to be able to create images so let’s talk about that. So how do you think we would create a. Do. pointer excuse me, I knew dynamic arrays and appointed. Well, I would assume that, perhaps you could point your pointer to an array that’s already created. Okay, so let’s do that so let’s go ahead and create a race so let’s say we have our aunt array. So then, how do I want to how would I point well, let me how do I point my point here to this new array. I think you can do P equals.

The array. P equals the array. And if not, that then P equals. ampersand alright. Alright, so the question is, will it be P equals array or is it P equals ampersand array. So let’s talk about that what is actually an array So when I create this variable array this size five does this variable actually have five integers inside. Is this this big variable that’s holding five integers. know. What exactly is an array then. it’s me in a way, just an address. arrays nothing was simply an address right it’s an address and what does that address represented in that array. I think it’s the first index of the array. Absolutely, we have our memory here. i’m just going to use in the in a resort, we have our memory here and we create a re when we say array five it is going to block up five memory addresses right. But the value of the array is nothing simply it’s just simply the address of where the first index is.

Right it’s just simply the address that raised us at 1000 then it’s the value that arrays 1000 right, and you can you can. If you don’t, believe me, you can see how an array right when we see out an array we would love for it to output, the content of the array right, but we know that when we see out an array we actually will get the address. Is that the address of the actual array or is that different. It is the address of it is the address of the consider an array the same thing as a pointer except for. an array is is static.

There they they actually work the exact same way, they are really just they really are the same thing that can be used exactly the same way. Okay, so if you wanted to get the actual address of the array, then you would do the ampersand array. If you want to do, yes, if you want to get the address this is going to give me the actual address of the array not the address of where the where the array began. Okay, so so this right here would not be the right way to do it. Since a pointer can point to an address right the value of a pointer I can only hold addresses of variables, and that is what an array represent, then this would be the correct way to point appointed to an array. awesome.

That makes sense. Alright, great so that’s one way to have a point to point to array with it with another way to have that point to point to to an array. i’m assuming since you emphasize so much on new and delete that there’s probably some way to do it that way. You guessed correctly, there is a way to do it, so if we want to do a dynamic array.

We can do so badly. Creating a pointer back in point two integers right so that means i’m going to say integer and then my pointer. And then we’re going to have an equal at move. integer. array. And they will put in our son. And so now what that does is going to do the exact same thing in in our memory we’re going to create a pointer. Right, the pointer is going to find five slots inside of the memory. And let’s say we start at 1000 so that means the value of the pointer is going to be what. The bio pointer is going to be 1000.

And we can just say the address is that 5000. Right and so now our pointer looks exactly the same as if it was a now a regular pointer so how the how it How does that work why, why is that. it’s still pointing to one value So how are we saying that this is a an array now how do we say how are we saying that this point is pointing to array when it’s still just pointing to. One address. Because it has those five slots already. allocated. yeah so The thing is, it all I did was D holiday with allocate the memory, it just reserve the space, but it. is still just pointing to the beginning of that array right It just wants to know where the beginning other raise it doesn’t need to know. Where the remaining part of the array is because our index take care of that right they the indices of the array will take care of that if I want to get index zero, then I will.

Get the value of my pointer right which is, which is an address. Right i’m going to add my index, which is zero, so let me just label that address. index. Right and then that’s gonna be the address that I go to right, so if i’m saying pointer. Zero then i’m really saying. Address 1000 plus zero and then i’m going to that address, which is going to be the very first address. My right. So, how would you translate that into code. How do I translate that into code so i’m glad you asked that so we can use the square bracket notation on our pointed. However. We won’t be doing that in this class.

We are going to do it the old fashioned way right the old fashioned way says that we are going to. Be reference our address and add the value right, just like we said here, so if I want to get. If I want to find the address for let’s say index three world that was weird if I want to find the address for index three, which is. What is the address for index. three. 1003 1003, then I need to be reference pointer to figure out where zero is. And then I need to add the index right, so I wanted to be reference p. So that I get 1000.

Then I want to add my index. which in this case is three. Probably just. Right that’s my index. So if I D reference my address, and in an athlete what would what would I get here this if I was if I was to do it this way. I think you would get an error. I will. I will not get an error but i’m not going to get the address that I want. Now, why do you say that i’m going to get in there. I think you’re not supposed to have that D reference operator. That the director Okay, so you want me to say P, plus three you saying that will give me my address. Yes, yeah that will actually give me 1003 now, what about wanted to change about so it’s great now, I have the I have the address, but now I need to go and change the value at that address, so what how do I, how do I use that what do I do.

I think you would put all of that P, plus three in parentheses, and then use that handy dandy D reference operator. you’re absolutely right right if this is going to give us an address. Then we wanted to in order to change that value, we need to reference that address and then go and change the value so if I want to change this to 10 then that’s the way I would do it so remember this is going to be my address. i’m going to D reference that address right so i’m going to go and find the value of p. I put P, but let’s just pretend that P is representing PT are i’m going to find the value of P, which is 1000 i’m going to add three to it, so that I can get 1003.

So now inside the parentheses, it says 1003 now I can be referenced that address, meaning that i’m going to now go to that address in the memory and then I can go and change that to 10. So that is basically the way arrays work, and that is the way dynamic raise work so, then why use dynamic arrays was what’s so special about them. Probably the ability to resize. The ability to resize okay. And so, if I want to resize my array my my dynamic array to let’s say size six rather than to.

Can I just do this. Oh i’m sorry, can I just say. PT are six without without make it bigger. know. It goes out of bounds. it’s absolutely correct it sounds, this is a, this is not something that we’re able to do so, then, how would I make my. array grove in how can I make it resize if I can’t do that do I have to do something like this do I read declare it. i’m not quiet, but you would have to create a temporary pointer. Okay, whatever sides that you would. want it to be. Okay, so this isn’t gonna work either a lot why doesn’t this work.

Because it’s going to eliminate all the data that you already have inside of a pointer or it’s just going to give you a bunch of errors. In the air I can’t read eclair another very low, at the same name but I can’t do that. yeah. So yeah he said that yeah absolutely correct, we will need to create a new. A new temporary. array so i’m going to go ahead and do that. OK, so now, I have a new temporary so that resigned as I resizing my pointer. Well, you would.

You would first have to create the temporary Ray, as you have done. Okay, and then take. find a way to copy all the values from your original array original dynamic array and shove them into that temporary right. Okay, so how, how can I do it, so can I, so if I want to just copy everything from from pointer into camp back, can I just do it. Without with our. know.

Why would this, what is it is doing. it’s basically having two pointers pointing at the same dynamic right which is a big, no, no. Big, no, no, we just now last where this pointer right we lost all this memory that just got allocated by doing something like that, so we can’t do that so then, how do I copy everything from pointer over to 10 if I can just say San Francisco pointed. would need a. What I call a Walker I guess. A Walker. Again, so how and how do I so So what do you want me to. create a. pointer Walker. And set that equal to.

That original pointer. Okay. yeah and then you would probably want to iterate. So. yeah so for into I. Know for. I haven’t done so well. What do you mean you haven’t done any value just not learning this Alexis. I think I might need your help on this one. yeah so that will you’re absolutely right, we probably don’t actually even really need to do this Walker we probably don’t need the Walker but yeah what we want to do is say it is equal to zero is less than the original size of our old one, which was fine.

And then I plus plus and then we simply just want to copy it over right and we’re going to do our ugly way of doing it we’re going to say star right P tr plus I. wouldn’t give 10. Oh you’re absolutely correct, yes, Tim. Class I. is equal to. Star and then pointer plus. pointed plus I. Right, so what that’s doing is saying let’s go ahead and find the address that temp is pointing to which is the first index and his new dynamically allocated array. And then we’re going to start from zero and we’re going to find the address plus zero D reference it and then said it equal to.

The address that point is pointing to plus zero and the reference that so the value at this address is going to be set to the value at this address and we’re going to keep looping that through until we get all the values from this pointer copied over to this new pointer. Right, and so this is equivalent right, we could just say temp. I. is equal to PT are. These are actually doing the same thing, but until you understand. This notation I do not want you to use this notation right until you can look at this and it doesn’t look like madness then we’re going to go ahead and use that, so this is going to be off limits. For now.

Alright, so now that we copied everything over well I don’t understand now, I have two pointers I have two arrays that now are have the same values and how does that make out of that make my array resize. Right, because now TIM has everything that pointer had in it, but that that’s not resizing my Ray that just gives me to raise. So. So now what you would do is you would delete every delete pointer. But in. While deleting it since it’s an array you would have to use the brackets after the delete the square brackets.

Okay, so I do square bracket. And then, whatever. pointer pointed well why don’t Why did I need to square bracket. Because it’s an array. that’s where that will have to put a number in that square bracket that’s weird well what’s going on with that. It just deletes the whole entire array. yeah that’s absolutely correct the compiler doesn’t know when to do the D allocation, but when you use the bracket and at least knows how much memory was allocated by this pointer and so, if you want to delete every memory address that lays out this point are allocated.

As far as when it actually did the. array size way up here when we first allocated memory, the compiler will know that this pointer allocated five memory addresses. And if we want to delete all by memory address is that this pointer use when it causes new keyword, we have to use the brackets what what happened it by didn’t use a bracket. It will only delete the first memory address.

And it would. It would leave all the other ones within. I think the free store. yeah absolutely we’ve don’t use the bracket is going to basically say hey all right, let me go to pointer let me see what address it is pointing to and then i’m going to go ahead and free up that address only and it frees up this address and it leaves the remaining addresses here. But if we use the bracket right we go down and we say let’s use the bracket then it’s going to actually.

Come to pointer and it says hey point is pointing at add just 1000 but it allocated five. memory addresses so we’re going to start at 1000 and we’re going to delete all five of them and so that’s what the bracket is. awesome, so now we know how to delete our. pointer well whoa wait a minute that’s not resizing now we have a temporary pointer and we don’t have our old pointer anymore well that’s not resizing. Well, I mean the last step would be to set your original point or equal to the temporary pointer.

Alright, so if I said, the new point equal to the temporary what does that mean. It basically means that your original pointer is now pointing to the new array of size six that was allocated. Absolutely, so now there’s an extra space memory and we’re using the same as when we continue inside of our code we don’t have to now change our. code to a new our variable to a new name right, we can still go on using our identify appointed to to access available so that gives us the illusion that our array grew inside. yeah. doesn’t that kind of remind us something that we learned in CS to. What does that remind you of that we use a csv.

We live, it was this one kind of array that we like to use better because we didn’t have to declare size and we can just start adding things, and it would just grow inside as we add what was the name of that data structure.

A vector. yeah yeah that’s right. that’s right a vector that’s exactly how vectors work vectors look like they just go in in size, but what they really doing is they’re they’re using dynamic arrays and a read they’re pointing. pointers to different places in a memory as needed. yeah good I didn’t know that either I just made that up no. i’m just looking exactly how I vectors work. So any other questions about dynamic arrays.

No, I think he answered them. All right, well, thank you and i’ll see you later, when we talk about multi dimensional arrays unless of course you are fed up with me. To be continued.

Ayush Chaurasiya

Read The Latest Information Or Stories.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button