Move Semantics in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This guy has taught me more than my professors did....Thank you

👍︎︎ 55 👤︎︎ u/rekosin1 📅︎︎ May 29 2020 🗫︎ replies

Him and Jason Turner are the ones I go for when I need to check or want to know more about C++, might not be as useful for someone with years of experience.

👍︎︎ 31 👤︎︎ u/Tjccs 📅︎︎ May 29 2020 🗫︎ replies

There are several mistakes, which are already noted in youtube comments, so if you are new and learning, check it.

👍︎︎ 18 👤︎︎ u/ElijahQuoro 📅︎︎ May 29 2020 🗫︎ replies

cherno is amazing

👍︎︎ 17 👤︎︎ u/Ipotrick 📅︎︎ May 29 2020 🗫︎ replies

At first when he did the cast to string&& I almost had an seizure, but then luckily he changed it to std::move

Pheew :)

👍︎︎ 7 👤︎︎ u/le_manalishi_verde 📅︎︎ May 29 2020 🗫︎ replies

Can anyone clear my doubt please :

at first he had created the Entity object as :
Entity entity(String("Cherno"));
But at https://youtu.be/ehMg6zvXuMY?t=429 he changed it to :
Entity entity("Cherno");

And it works fine. Is there a concept I am missing here ?
Entity's constructor expects String type object, we aren't providing that. Does it happen because String class has a constructor that expects const char* ?

👍︎︎ 2 👤︎︎ u/ticket2020 📅︎︎ May 29 2020 🗫︎ replies

Very nicely explained. Thank you!

👍︎︎ 1 👤︎︎ u/foifaifei 📅︎︎ May 29 2020 🗫︎ replies
👍︎︎ 1 👤︎︎ u/darthsahab 📅︎︎ May 29 2020 🗫︎ replies
Captions
hello guys my name is China welcome back to my say plus plus series so today we're going to be talking about move semantics this is probably like the single most requested topic so I'm happy that I'm finally getting to and today we're gonna be more or less taking a look at an introduction to what move semantics is because it's a very complex topic the basics is really simple but then what you can do with it and how it works in practice we'll get to that stuff don't worry so first of all make sure that you've seen my Elle values and our values video because this is more or less going to be a continuation of that video now that we've learnt what our values actually are and what our value references actually are we can take a look at their single biggest use case move semantics to keep things simple move semantics essentially just allow us to move objects around and this wasn't possible before C++ 11 because C++ 11 introduced our value references which are necessary for move semantics the basic idea is that when we're writing a C++ code there are a lot of cases in which we don't really need to or want to necessarily copy an object from one place to another but that's really the only place that we can get it from one place to another so for example if I'm passing an object into a function that then is going to take ownership of that object I have no choice but to copy it the same goes for when I want to return an object from a function I still have to create that object inside that actual function and then actually return it which means that again I'm copying that data now I don't really like to use return values as an example for this because there is of course something called return value optimization which can kind of optimize that part of it and make it less of a problem but with the first example if I need to pass in an object a new object that I'm constructing into some kind of function that takes ownership of it or whatever I still need to unfortunately construct like essentially a throwaway object in the current stack frame wherever I am and then copy it into the function that I'm calling which which is not ideal because I don't actually need it here I need it there but I can't construct it there because I need to first construct it here and then pass it in it becomes a huge mess and sure if your object just consists of like a couple of integers or something like that then copying it is no big deal but what if your object needs to like heap allocate memory or something like that what if it's a string and if you need to copy it well you need to create a completely new heap allocation that's not good that becomes a heavy object to copy and that is exactly where move semantics comes in if we can just move the object instead of copying it then our performance will be higher and I promise you once we dive in and take a look in a minute you'll see that this is not nearly as difficult as it might seem but first this video is sponsored by Skillshare for those of you who don't know what Skillshare Skillshare is an online learning community where millions of creative and curious people can come together to take the next step in their creative journey they've got thousands of amazing classes for you guys to check out and the best thing is they're so small and bite-sized and efficient that even if you're super busy like I am you'll still find the time to learn something new which of course is always important some of my personal favorite classes on Skillshare are the illustration and art related classes but recently I found that they also have a number of excellent productivity related classes and I think that boosting your productivity is both one of the hardest and most rewarding things to do so definitely check them out and starting at less than $10 per month for an annual subscription I think it's really great value but if you guys want to check out a two month free trial then you can by clicking a link in description below huge thank you again to Skillshare for sponsoring this video sir move semantics let's dive in and take a look at what all the fuss is about let's write a class that's going to serve as an example to demonstrate why exactly we might want to move something so I'm just going to write a very basic string class that might look something like this this is not by any means going to be like a proper way to write a string class in fact I might make a video on that sometime in the future but let's just say that we have a class which has a char pointer of data as well as some kind of size this is our actual string data so what we want to do here when we take in a Const our pointer like this is essentially set the size to be the string length so whatever the length of this string is that's how many characters we might want we probably want a null termination character as well but we'll just skip that for the purposes of this example because again this is about move semantics and not necessarily about how to write a proper string class and then into here I'll just do a basic mem copy to move all of the data in from the source string here into our data buffer so now that we have that and I'll actually just add a printf here we just says created just so that we can later visualize our kind of code flow so we have a basic constructor here that takes in a string and allocates memory and copies everything into that memory buffer now inside the destructor we might have something fairly simple to just simply deletes the data and that's that's about it that's our basic string class now let's write some other class for example maybe I'll call it entity that's basically just going to consume our string so inside the entity class I might just have like a standard kind of con string reference here maybe this will be like the name of our entity and then obviously we will store this so I'll just write string m name here and then I will simply just assign it like this so at the moment for this to actually work properly we need to write a copy constructor because if we just simply create a string over here or in fact let's go the easy whale to say entity entity and then maybe call it churner like this and we're using the implicit constructor here so really what's happening is that and I might leave it like that for clarity but basically if we're if we just have some code like this we still need some way of copying this string into this string and so we need a copy constructor so what I'll do is I'll write a copy constructor here and this copy constructor is going to be very similar to this constructor I'll copy it in fact I'll just change this to copy it except obviously the size is just going to be other dot size and then everything else is pretty much the same except of course we just write other dot M data so now we're just copying the string and we have a valid way to actually get it in here if I run this code in fact let's do one more thing let's just write a function called print name here and then that needs some way of printing the name which we can't actually do at the moment so let's write a very basic function here called print now because our string class is special and doesn't have a null termination character what I'll actually do is loop through the size of the string and then print each character one by one sefa char and then we'll do M data I and then at the very end I will just print a new line character so now this will just call m named print and we should have a very basic program now that will just basically just print shown and if I run this code you can see that we have created copied and Cherno so everything is in fact working however this is really the problem that addressing today we have this copied line being printed which of course means that our data has been copied now why is this a problem well the reason it's a problem is because you can see that to copy a string we need to allocate memory on the heap we have to call new char that's not that's not a good thing it's definitely not terrible and especially if you have just one copy like that let me just quickly add this thing in here as well it's not necessarily like the worst thing in the world but remember the fact that we have to copy this is a little bit ridiculous because I mean look at look at what we're doing we're essentially creating an entity called Cherno here and I mean we just need some way of getting the string into here and what does that do well that that has to allocate memory twice because we need to create it first in the scope of this function inside our main function which is what happens over here we're actually creating it inside the main function and then we're passing it into this entity constructor which then on this line over here has to actually copy it into here why can't we just move it into here why can't we just allocate the memory right here and just have it there or I mean worst case scenario because we can't really do that without getting access to the string and then doing stuff like that manually why can't we just still allocate it here in the main function but then just move it into this space because now we wanted to occupy this space and the answer is of course we can and that's where move semantics comes in so what we need to do in this case is write a move constructor so this is very similar to the copy constructor except this takes in our value reference meaning a temporary so this right here is a temporary it's not an L value is not assigned to anything it's just used up as a parameter for this actual entity constructor so now by specifying this constructor which by the way we should specify with no except I mean even visual studio is telling us because it's not really supposed to be throwing exceptions not that we really use them anyway but just to be ultra correct we'll do that and by specifying this constructor the hope is that eventually when we actually perform this copy instead of copying it will actually move now in order for that to actually work we need to also make sure that entity has a constructor that takes in a temporary so instead of kind of doing that what I'll do is I'll actually copy and paste this constructor so I'll just add a new constructor that takes in a temporary like this now hope course is that when we actually call this because now that now that this is a temporary and we're providing an R value reference constructor since this is an R value it will actually call this constructor now instead of this constructor now they are both the same they do the same thing that is assigned name the same way so nothing will change but that's kind of the first step so now we need to actually implement our move constructor so what I'll do is I'll copy in fact I'll just copy everything and I'll show you how to do this so we'll say moved over here size still gets copied the same way and what we'll do is we'll actually copy data but what we're doing here is instead of actually allocating a new buffer of data and copying everything individually copying copying that entire block of data into this new block of data what we're doing instead is we're simply assigning the pointer so we're taking the pointer to M data which is the pointer to the existing buffer that we had in our original string and now we're saying that this pointer that we have here in this new string instance that we've constructed it actually points to the same data so instead of allocating an entire new block of data and copying everything into it we're just essentially pointing to the same block of data as the old string now this immediately presents a problem because we do still have to string instances so what happens to the old one when it gets deleted well it's going to take the data with us so what should we do and that's exactly why I move constructor cannot end right here you have to also take care of the other string the other string instance who you're basically taking control over you're stealing all of that data so to do that we need to also assign m data to null pointer that's kind of the the major thing that we need to do but also what we should do is assign size to be 0 so by doing so we've basically created something called a hollow object this old string is now essentially just in an kind of like an empty state and what will happen is when the old string instance gets destroyed this delete m data will actually be deleting null pointer and obviously deleting null pointer just doesn't do anything because there's nothing to delete that pointer is pointing to null so we've effectively just taken over that old string instead of doing a deep copy by copying all the data and allocating new memory we've essentially done as a shallow copy we've just kind of rewired the pointers so now everything should be good right let's run this and see what we get and you can see that interestingly we have created copied destroyed and Cherno so interestingly the Cherno is being printed after destroyed because that's actually our temporary getting destroy so that seems to a function this didn't crash or anything however we still have a copied which is a little bit odd because you know I mean we provided this constructor what's going on and the final answer is that what's actually happening is the copy constructor is still being called over here when we actually assign name like this it's actually still using this constructor to get it to use the move constructor you have to actually explicitly cast it to a temporary like this and if we run this code you'll see that we have a moved now instead of copies so we've successfully only allocated memory once and managed to move the string into the entity class now in practice you wouldn't really cast it to an r-value reference like this instead you would use something called STD move which essentially does the same thing but we'll have an entire video on STD move and all the stuff that's used for in the future so let's run this code and you can see that we still have the same result created moved destroyed and then we've managed to print our string successfully everything it seems to run so the the major point of this is that we've saved using this copy constructor to actually allocate a new block of memory and copy it we've simply managed to move that instead so we still only have a single allocation which is great okay so I hope that that was somewhat of a good introduction to move semantics of course there are so many more things we could cover such as SCE move and don't worry we will in future videos but I hope you guys enjoyed this video if you did please hit that like button don't forget that you can try our Skillshare free for two months by clicking the link in the description below and I will see you guys later good bye [Music]
Info
Channel: The Cherno
Views: 128,416
Rating: 4.9427304 out of 5
Keywords: cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, move semantics, lvalue, rvalue, lvalues and rvalues, move constructor, move operator, move, move c++
Id: ehMg6zvXuMY
Channel Id: undefined
Length: 13min 10sec (790 seconds)
Published: Fri May 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.