The NEW Keyword in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is HMO and welcome back to my state plus plus series today we'll be told me all about the new keyword it's a boss boss and man man does this meet talking about if you guys missed my video yesterday about how to create objects in C++ and the correct way to accentuate objects and definitely check that out cod that link there but otherwise let's talk about new so the new keyword is interesting because it's it's actually quite deep a lot of people write new and they don't really think about it but there's a lot that goes on and it's a really important that you understand that especially since you're programming in C++ the fact that your prototype makes a bus bus means that you should be caring about things like memory and performance and optimization all and all that because really if you don't why are you writing C++ there are so many other languages out there that you can do especially now in 2017 why would you be writing to a bus bus card unless you specifically need performance or you specifically care about everything that goes on and understanding new is very very important especially if you're coming from a managed language like Java or C sharp where memory is first of all cleaned up automatically for you but also you don't get anywhere near as many choices when it comes to memory as you do in C++ so for those who do know Java or C sharp you're used to using new all the time and when you're going to see bus bus you'll probably think I yeah I can see simple spots not that hard is the same I mean there's a new key way I'll just create a new class know check out my video on how this and share classes if you haven't already so the main purpose of new is to allocate memory on the heap specifically you write new and then you write your data say whether that be a class or a primitive type or an array based on what you've written it determines the necessary size of the allocation in bytes for example if I write a new int that is going to have to request four bytes of memory allocate 4 bytes of memory once it has that number it goes and it asks the operating system what the I should say C standard library I need 4 bytes of memory please give it to me and that's where the fun begins now we need to find a contiguous block of 4 bytes of memory now of course 4 bytes of memory is very very easy to find so there'll be Radek quick allocation but it still needs to find an address in memory where you have full bytes in a row once it does that it returns a pointer to that memory address so that you can begin using your data and storing data there and read me in access of reading and writing and doing all that fun stuff so you see how I just like list it off like five steps or something like that yeah that's the primary takeaway from this when you call new it takes time now I did say that we had to literally look for four bytes of contiguous memory it's not really like it's not like it it literally searches our memory just in a row like a laser being like a cater we have four bytes free no I'll look on the next slot there's something called a free list which actually maintains addresses that have bytes free not a story for another video but it's not like it might not be as slow as you think it's obviously written to be rather intelligent but it is still quite slow but that's the primary takeaway knew basically finds a block of memory that is big enough to accommodate our needs and then gives us a pointer to that block of memory let's take a look at some cards what I've got over here is a very basic class has just got a string name that's it I'm going to scroll down over here and we'll take a look at the new keyword first and foremost just like we create integers normally by doing this we can also choose to actually dynamically allocate that memory and credit on the heap by using the new keyword so what we can write is int pointer B because remember as I said new returns a pointer to the memory that you've allocated equals new int and that's it that is a single for byte integer allocated on the heap and this B is storing its memory address if I wanted to allocate an array instead then I would ask where brackets and just type in how many elements I wanted so in this case 50 which means we need 200 bytes of memory 50 times 4 for being the size of each integer if we wanted to allocate our NC class on the heap by the new key where we were write code like this or this alternatively we don't need to use parentheses for this the default constructor but we can and I usually do if we wanted an array of entries instead week do that by just using square brackets and there we go that's basically new right that's how you use the new keyword let's talk about it a little bit more with classes the nuclear what does two things it doesn't just look at entity see how big an she is in this case it's a string so that's I think like 28 bytes or something like that don't quote me on that I'm just guessing from memory it multiplies that by 50 and it asks for a block of that many bytes in the case of an array though you'll actually get a block of 50 entities just continuously in memory so this is actually kind of like allocating 50 entities on the stack just in a row it's a little bit different because you're still allocated on the heap but each entity in this example won't really be in another memory breast you have your block of memory which is 50 entity it's just just in a row if we go back to two this just being a heap allocated single entity object then by writing this new keyword we not only allocate enough memory on the heap to store this anta see we also call the constructor that's the other important thing that the nuclear work does it not only allocates the memory it also calls the constructor now behind the scenes all new really is and you can see what I've done here is I've just if you right-click on you you can go to definition and you'll see what this dot operator new actually is first of all you'll see that it's an operator new is just an operator just like plus or minus or equals it's an operator which means that you can actually overload the operator and changes behavior we'll talk about overloading operators very very soon link in the description card on the screen all that jazz but second of all you can see that is literally just function and this is the size that it takes that's how much it allocates it returns a void pointer we might talk about void pointers in a separate video but basically a void pointer is just it's a pointer with no time a pointer is just a memory address so of course it's like pointers really need a type it needs a type for you to be able to manipulate it probably the way that you want to but at its core a pointer is just a memory address it's just a number so why would it need a specific type like int or double or entity but anyway you can see that we return a void pointer so it returns a pointer it so takes in a size and it returns a point to that allocate block of memory but what new actually does behind the scenes and strategies speaking this is actually dependent on the state plus plus library so of course Bureau your own save us plus compiler with your honesty of us loss library because theoretically make it do anything you wanted but usually usually pulling you will call the underlying state function malloc which that's for memory allocate and what this will actually do you'll note is taking a size of how many fights we want and return a void pointer so that's really all it does so that being said this code is actually kind of equivalent to if we just written malloc sizeof entity like that and then of course cost this back into an end to see something we wouldn't have had to do in C but we do in C++ but the difference between these two lines of code the only difference between these two lines of code is the fact that this will actually call the entity constructor whereas what this will do is purely allocate the memory and then give us a pointer to that memory not calling the constructor you should not be allocating memory tables like this there are some situations in which you might want to do that we might talk about them later but for you right now if you're watching this video because you don't know what I pretty new is using you because this of course won't call the constructor and it's also way poor code and it's just harder to read and this is really the way that you should be doing it the last thing that I'm going to mention about new for today is that when you do use the new keyword you have to remember that you must use delete so once we allocate all these variables like B and E we have to use the delete keyword which is also an operator if you go to the definition for that X is also an operator text in a block of memory size it's just a regular function which calls the C function free and actually frees the block of memory that was malakut this is important because when we use the new keyword the memory is not released it's not marked as free and it's not put back into that free list so that we can call new and allocate it again until we call delete we have to do that manually there are of course a lot of strategies in simple spots to automate this in some form and there are simple strategies like kind of scope based pointers and advanced strategies like reference counting we'll get into all of our stuff in the future but just keep in mind if you use me like this in a wrong way you have to use delete and I do plan on talking about the intricacies of all this memory management in the future once we actually start working on a project and writing code which is actually going to be very soon so get excited everything will be revealed I'm sure and you'll see many examples in the case of B because this was allocated by using the array of credit keep in mind by the way that this new that takes in an array of peretta is actually a slightly different function if we allocate using new with square brackets we should be calling delete with square brackets like this because there is a delete operator with square brackets like that okay so that's another rule if you allocate using new with square brackets because you've allocated an array delete using the square brackets if you don't then just use whatever square brackets one more thing that new actually supports is something called a placement in U and that is where you actually get to decide kind of where the memory comes from so you're not really allocating memory in this case you're just calling the constructor and initializing your entity in a specific memory address and the way you do that is just by writing parenthesis like this and then specifying a memory address such as well B in this case I mean it would theoretically work because I'm assuming that entity is gonna be less than 200 bytes definitely well because it's just a string but yeah this it's gonna confuse like all this code and really really we'll talk about placement new in the future in more detail on how you can actually use at optimizing a code quite a bit but for now I just want to show you what the syntax kind of looks like in however anyway I hope you guys enjoyed this video if you did you can have that like button you can also help support this series and make sure more awesome episodes are made by going to patreon account or search the channel I've got a discord server there'll be a link in description below we can talk about all this stuff patrons get special like server roles and also a special channel where we actually talk about these videos and plan the next it's kind of like right behind the scenes and very details very cool so definitely check that out and become a pic home and help support this series I will see you guys in the next video goodbye [Music] [Music]
Info
Channel: The Cherno
Views: 143,200
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, c++ new, new keyword, new operator, c++ how to use new, heap vs stack, memory, memory allocation, create objects
Id: NUZdUSqsCs4
Channel Id: undefined
Length: 10min 52sec (652 seconds)
Published: Fri Sep 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.