C++ Programming Tutorials - 30 - C++ Dynamic Arrays - Eric Liang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is up guys my name is Erick and in today's episode we're going to cover the topic on how to create a dynamic array in C++ let's do this now a while back I taught you how to create arrays in C++ you're probably wondering if we already learn how to create an array why are we looking at this topic again because what we're going to be learning today is dynamic arrays and those are completely different from the arrays that we've learned a few episodes back now what makes these arrays different from the ones we've learned back then is that the rates that we've learned before are actually called static arrays now what that means is sanik arrays are basically arrays to have a fixed size what do you mean by size well by static arrays I mean there's a mixed number of elements that your array can store so for instance if we were to create a static array that can only store three elements meaning inside the square brackets it would have a number three inside then that's the maximum number of elements it can store it cannot store four five or six elements or even more because the moment we created the array size of three that's what we're going to get that's what we're going to stick with we can't go beyond it however what if we wanted to create an array that can readjust itself in size what if we wanted to create an array that can let's say add more items and that is where dynamic arrays will come into play so the topics we're going to cover in this video are what is the dynamic array what is the difference between a static and the dynamic array and how to make a dynamic array in C++ first off what is a dynamic array a dynamic array is in a rate that can adjust its size during runtime so what are the differences there soft static arrays are fixed in size like I said before static arrays allocate or reserve memory space in your computer's memory during compile time now what compile time means is when the program is created not only that static arrays are located in the stack memory space meaning if we were to divide the computer's memory into chunks or sections there's a certain section that is called the stack memory and that's where static arrays are located as for dynamic arrays they're dynamic in size meaning they could readjust its own size how well that's due to the second ball point and that is dynamic arrays allocate memory space during runtime meaning when the program is running and not only that dynamic arrays are located in the heap memory space of your computer's memory again it's just another section of your computer's memory so how do we make a dynamic array in fact what do we need in order to make one first off we have to know how to create pointer variables second we need to remember the concept of how to create arrays and then finally we want to learn to new things and that is the new operator and the delete operator you're probably wondering what is the new operator good question in fact the new operator is what will allow us to allocate memory space or our dynamic array during runtime as for the delete operator that is what's going to do the opposite of the new operators functionality and that is the lead operator will clear the space that the dynamic array was using why would we want to do that well computers as we know have a finite amount of space and we don't have to always use all of it all the time right so as soon as you no longer need that data stored in the dynamic array we delete them and by the way it's not optional for you to put it or not put it you have to put it somewhere down the line in your program you have to put it there ok so let's hop on over to visual studio and start coding a dynamic array shall we so here I've went ahead and commented out the syntax structure for a dynamic array to create a dynamic array you first have to create a pointer variable now the name of your pointer variable will end up becoming the name of the dynamic array so make sure your name is what you want your dynamic array to be so as we learned a pointer variable starts off with a data type followed by the asterisk followed by the name of the pointer variable in this case it will become the name of the dynamic array next the second line you would have to take the name of your pointer variable and set it equal to new and then the data type that you gave your pointer variable followed by a pair of square brackets and then a value a whole number value which will represent the number of elements inside your dynamic array and that's basically it and then the third line will basically represent the lines of code you would mess around with your dynamic array such as store removing and all that good stuff and then finally on your very last line you're going to have the delete operator line and make sure when you do create this line you include the empty square brackets followed by the name of the dynamic array you are going to clear the space for note that once this line is called all the data stored in your dynamic array will be gone okay so you're probably wondering wait a minute wait a minute wait a minute I thought this was a dynamic array well it is and you're probably wondering okay if that's the case why is there a number of elements value inside the square brackets wouldn't it be empty or shouldn't it have something more generalized so it could reassign well that's where the fact that new comes into play and not only that this is where also the fact that dynamic arrays allocate memory space during runtime comes into play as well it's because of the fact that it allocates memory space during runtime that makes it dynamic so let me show you so if we were to create a let's say int pointer variable and let's say my dynamic array and then let's say my dynamic array equals new and then int because that's the datatype of our pointer variable that we went with let me call in and inside the square bracket we would give it a value let's say three just to keep things simple and then let's say we are going to assign the value so my dynamic array 0 equals let's say 100 and in my dynamic array 1 equals 200 and then my dynamic array 2 equals 300 and then if we're to see out actually let's see let's do a for loop so 4 into I equal to 0 I less than 3 I plus plus this will allow us to loop through the dynamic array and display each value on to the console window so see out let's see my dynamic array and then I and then Enlai ok so if we're to run and commodities program we should see 100 200 300 listed or vertically and as you can see it lists 100 200 300 vertically perfect now time to prove to you why this is there and why this is dynamic so so let's say we wanted to increase to five elements okay so in that case we're going to have to run the delete operator so delete empty brackets and then name your dynamic array my dynamic array semicolon okay so at this point if we were to try and see out this array what do you think we will get do you think we'll even compile this successfully let's see as you can see the program stopped working okay so that means as soon as you run a delete operator you can no longer use the dynamic array however we can actually use it and not only that we can readjust the size at this point so like I said let's say we want to resize it so it can store five elements so my dynamic array equals new int five okay and then let's say we want to copy and paste this down and let's say when let's say 10 20 30 and then let's say my dynamic array 3 equals to 40 and then my dynamic array 4 equals to 50 okay that's five values so with the compile and run this program what do you think we will get no errors right but as you can see it shows okay I forgot to use the loop here my bad so if we're to copy and paste that on the bottom change the 3 to up 5 and let's let's do a C out in between so C out here and dynamic 3 just so we know where it ends ok so I've read the compile and run this program again we should see 5 values after 3 and as you can see it shows 1 300 300 which is the first few parts of our code and followed by the C out that marks the end of that and then the 5 values which readjusted the size to 5 elements and as you can see it shows 10 20 30 40 50 without any problem at all ok so you're probably wondering at this point okay but what if we don't know what the is going to be what if we want the user or maybe we want an external API to determine the size for us in that case we're going to have to delete the value inside the bracket and give it a variable name so let's call it count and then outside let's create int count and then let's see out how many values to store in the dynamic array and then see in count so that way it will allow our user to determine the size of our dynamic array so where to delete this bottom chunk right here remember keep the delete and then run and come out this program okay well actually before we do that let's update our loop to count okay and then we were to compile and run this program again okay as you can see it shows how many values to you two do you want to store in the dynamic array let's say 10 and as you can see shows the first three values that was a sign however the last few values last seven values are just a bunch of gibberish values right well that's because we didn't store anything there and these are just default values that the computer's memory build up with now like I said let's say we add a few more lines of code okay so let's say three four and five and then debug and then let's say we give it a size of 10 again as you can see it shows 100 200 300 300 300 300 and then some random values perfect because I didn't update the values when I copied and pasted it that's why I showed 300 still but what if we want to ask the user to enter some values in so in that case we would have to use a for loop so or int I equal to let's say into k equal to 0 k Lessing count k plus less so this i want to ask the user to input values so see out input value see in my dynamic array and then k silicon and then let's say see out and line just to make a space so if you're to compile and run this program but see what would happen so let's see how many values do you want to store in the dynamic array let's do ten again okay input value let's say 100 200 300 400 500 600 700 800 900 and 1000 enter as you can see our program allowed us to enter the 10 values as we stated before and it will stop once all 10 values are stored and it will display all 10 values and as you can see shows 100 all the way to 1000 which is exactly what we've inputted up here and then the program ends perfect so with this you have learn how to create and use a dynamic array in c plus plus thank you for watching this tutorial and if you have any questions about this topic or anything at all about dynamic arrays be sure to leave it in the comments below like the video if you found it helpful and subscribe if you haven't already and i'll see you next time [Music] you
Info
Channel: Eric Liang
Views: 24,962
Rating: 4.8876033 out of 5
Keywords: C++, C++ programming, c++ programming tutorials, how to program in c++, C++ for beginners, C++ programming for beginners, eric liang, how to code in c++, C++ dynamic arrays, dynamic arrays in c++, C Dynamic Arrays, C++ Pointers, C++ new, C++ delete, C++ programming tutorials for beginners, learn to code in c++, C++ for noobs, c tutorial, c tutorial for beginners, c tutorial arrays, c and c++ tutorial for beginners, introduction to c++, visual studio, c++ tutorial
Id: MwwbgqG6bSk
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Tue Aug 22 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.