Let's Learn C++ ~ #25 ~ Dynamic Arrays Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm TP and welcome to let's learn C++ today's topic is dynamic arrays this is Winnie hallucinate in multiple dimensions every remember things as beams of light wrapped around the fifth dimension if you're completely lost know that I didn't know where this joke was going when I wrote it all right in the previous tutorial we covered how to dynamically allocate memory for individual variables many tutorials before that we covered how to create arrays and multi-dimensional arrays we will now cover how to create dynamic arrays the old-fashioned way meaning professionals readily use the method I'm about to show you it's kind of why I've waited so long to show you it all right to create a dynamic array create a pointer as you normally would and then have it set to a new instance of the data type you desire followed by square brackets for the size of the array notice that previously we would use parentheses to enclose a singular instance value but for an array we use square brackets to designate the size of the array the size must be declared when the array is created and cannot be changed just like regular arrays so let's look at an example of this alright so up top we have our standard sub code of importing the iostream the string and using namespace standard because I'm lazy then up top for line seven through nine we have previously used ways of instantiating arrays that are non dynamic that are just regular arrays and we have a being created with empty square brackets and then squiggly brackets to enclose the values that we want it to be defaulted to so one two and three then we have creation of a B array that just has a size of four and after that if we wanted to like initialize all the values of that array to be zero we could just click include some open and close curly braces right there and it would be done all right and then we have a simple output there to show that our B array is not actually initializing the values and we'll hat will in fact have some garbage value defaulted then after that we have our dynamic memory array example so first we have pointer one which is just p1 and it's being set to a new instance of an int array of size five and this will work just fine if we wanted to initialize the values to zero we could simply open close regular parentheses with no values inside and it'll default to zero now we have pointer to set equal to an empty set of square braces so the array size is not being declared and instead what we're trying to pass in is curly braces it enclosed with one two and three this will cause an error this will not actually work I wanted to show you this because up above we use it to create a regular array but it will not work for dynamic arrays so if we go ahead and set a breakpoint here and try to run our code it will not actually work in cause build errors okay so we can just go ahead and delete the curly brace code here and add in some value if we wanted it to work and then run it and now it'll start to work just fine so if we continue on through we notice that our first value being output be zero has a garbage value inside of it and then we have p1 being set to a value of four right here and then we have it being output as well as a random garbage value for p2 being output as well all right so let's go ahead and close that out now something to note is that after you instantiate any dynamic memory it's important super super important that you clean up that memory afterwards so basically any time you use this new keyword somewhere later in the program you should have a delete open close square brackets for the deletion of a dynamic array if it's just a regular new value then you don't need the score traces but because we're working with arrays this makes sense and all we're doing it cleaning up the memory perfect and then finally we just have our get line to stop the program before it closes alright so a question you may be asking yourself at this point is what if I want a variable the user inputs to determine the size of an array well this is a very particular issue and I have an example for just that so up top we have the creation of a user input into variable and then we're just going to pass in using a enter number as the prompt and then the value entered afterwards is going to be stored in user input then we try to create an array a simple normal array using the user input now notice I get a compiler error right here at user input this little red squiggly line mouse over it and says expression must have a constant value maybe may be thinking to yourself well we could just use a cost as we did before so we can convert this user input to a constants right here and that should work as well right so let's go ahead and copy in the value and now if we mess over that no it still is not satisfied that is because regular arrays size must be determined at compile time it cannot be determined at run time like we're asking it run time being when the user inputs the value and the program is running so this expression does not work at all however what we could do instead is use dynamic memory to solve this problem all right so I'm just going to go ahead and delete this top two lines and now look at what we have here we have a pointer P being set to a new int array whose size is determined by the user input and then after that we're just going to go ahead and loop through all the values of that array and set them to a value of four wire for I don't know and then after that we just output a line saying ending and finally use our gate line here now something weird is going to happen here watch what happens when I run the program I'm going to input a value five right here enter and says ending and the program closes out now ordinarily what would happen is our string Y and then get line would actually stop the program from closing out so what's going on why is it closing out now instead of stopping well because we have a call to see in somewhere else in the program it's now not properly handling the get line function as it did before so instead what we could do is we'll just replace this get line which is a regular CN with a variable Y and it'll work just as before now we can enter in a value of five as before and then it will say ending and the prompt will not close and it said we can just press ENTER again or we have to actually enter a value and and then it'll close out so one of the weird quirks of CN is they actually have to put in some value for it to be satisfied you can't just press the Enter key alright so that's it for part 1 of dynamic marae's in the next tutorial i will cover how to incorporate dynamic arrays into functions and creating multi-dimensional arrays alright you are doing fantastic keeping up I highly recommend challenging your skills at hacker ENCOM please support me on patreon if this helped you and thank you to my patreon supporters Marcus G JK and as always like subscribe and keep the dream alive [Music]
Info
Channel: Trevor Payne
Views: 8,626
Rating: 4.8297873 out of 5
Keywords: TPayne, Experience, Let's Learn, Cpp, C++, Programming, Computer Science, Tutorial, Intermediate, Dynamicly Allocated Memory, Dynamic Arrays, Part 1, runtime array size, new, delete[]
Id: YNPUt7PIDI4
Channel Id: undefined
Length: 7min 23sec (443 seconds)
Published: Fri Aug 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.