Safety in modern C++ and how to teach it

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is Jenna welcome back to my safe possible series today we're gonna be talking about what it means to be safe in C++ and just in general the whole debate about modern C++ and safety because I feel like this is an issue that has been circulating for a really long time and I feel like it's kind of the more complex C++ we actually get to in on this channel with all the various projects and this series I feel like more and more people are kind of a little bit confused about what the correct way to write C++ is and like just there's been so much debate on my discord on reddit everywhere about just in general like how to write C++ in the modern day because there's all these people of course who are saying that modern C++ is all you should be using you shouldn't be touching things like pointers like roll pointers at all ever you don't even have to teach that because you just don't even worth knowing because it's very bad but then there's a flip side of things people say but people saying smart point is a terrible don't use them they like performance overhead whatever like just roll pointers be a man and use a roll pointer that's for you all you need in fact you know be a man and manage your own memory is kind of a thing as well so I just want to kind of talk a little bit about what all of that means so that people who are kind of new to C++ and in general people on my channel have just a bit of a greater awareness of what things like that mean and where you should be concerned and what the right choice kind of is so we're just gonna kind of talk today and I might write some code released so show some concepts but really this is just going to be a little bit of a casual open discussion please participate by leaving a comment as well with your thoughts I really want to know what this community that watches my videos the watch my videos actually thinks furthermore I do have a discord the channel comes like this Court is the link to that discord so it'll be somewhere on the screen and in the description below as well so definitely check that out because that's a whole like I think there's like over three and a half thousand members on that discord and we talked about C++ and graphics and game ends and everything all the time and lately I've been kind of more active on that as well so anyway what is what does it mean to be safe in C++ so a safe programming or the concept of kind of programming in a way that does that hopefully reduces things like crashes or memory leaks or access violations is something that's kind of the hue like the human race has kind of been improving on as time goes on so with kind of the introduction of C what was 11 we started to I would say move towards concepts such as smart pointers in favor of rural pointers primarily because of the whole issue of having memory leaks as well as not knowing what memory has actually been allocated or free because really all this can be summarized into a simple problem I need to allocate memory on the heap that is a huge operation in itself if you guys haven't seen my heap as a stack video check that out in the top right corner because that's really useful to know and actually other videos that I recommend you watch before watching this one is definitely the wrong pointers video which has saved us what what pointers in C++ and also the smart pointers video because that's kind of what we're gonna be talking about today and specifically again today I want to focus on pointers and memory rather than like other things like exceptions or you know the other type types of safe kind of programming or things like you know error checking whatever we don't care about that but today we're just gonna be talking about pointers so when we started kind of gravitating towards smart pointers and all that it can really all be summed up into the fact that I want to allocate here memory right that in itself seems to be such a complicated part of programming that we've constructed all of these kind of constructs and systems and ways of dealing with that in a way to automate it and hopefully reduce human error and all of this can be tired really into human error right smart pointers and kind of automatic memory management systems exist to make to make programmers lives easier and not just easier but more robust meaning that you don't you know it's not about the fact that you don't have to deal with certain things anymore because they're automated it's also when you forget to deal with with certain things you are fine because it's handled for you automatically so the concept of memory allocation itself is really simple right I want to allocate my memory allocation of course on am I am talking about heap allocation I want to allocate a block of memory on the heap right what happens in that case is you get a pointer back if the allocation is successful you get a valid pointer back to the beginning of that block of memory and it will exist until you explicitly delete it that is the whole concept of what I'm talking about really and the problem comes from I guess a few aspects there's the problem of what happens if I forget to free my memory that's an issue because that will cause a memory leak which could be completely harmless and you wouldn't even notice or it could be catastrophic and it could be the result of your program crashing because you run out of memory then there's also the problem of ownership right so who owns the memory that has been allocated if I if I have a roll pointer just a pointer to that block of memory and I start passing it from function to function from class to class who is responsible for managing and as potentially cleaning up that memory right that's the ownership problem because if you start just having a roll pointer around then what you know you know there will be kind of programming scenarios in which you have function a that manages that function B that manages it you're not sure which of those functions is going to finish with it last but you need both of those functions to actually have access to that pointer so how do you say that okay at the end of those two functions completing their job that's what I want to Freight out memory that's not possible unless you can explicitly say I'm gonna run function a I'm gonna run function B and then at the end of that I'm gonna have like a third function or something that is guaranteed to run after those two functions are finished and that is what's gonna before my cleanup step and that obviously massively complicates the whole programming just just programming really and that's something that we definitely want to avoid so that's another kind of ownership issue but really there's just forgetting about the allocating memory which is kind of more of a simple problem because the solution to that really is just you know to be able ittle bit more attentive when you're programming which again isn't something when I'm saying like that's not it that's that's hard to say right oh just be a better programmer you know that what does that mean like that's so arbitrary that's so you can't just say okay well yeah I guess I should pay more attention to my code when I'm programming that's not a solution right that's just I'm just saying that that's something that would be a potential hypothetical fake solution to the problem is yes to be a better programmer and then you would need to worry about more like kind of more complicated constructs in order to delete memory that has been allocated by yourself explicitly right but then there's also the ownership problem which is I have a bunch of different functions that require access to the starter we're all in the finished I want to clean up after that data like I want to be able to deallocate that data but I don't want to have to explicitly build something that like manages that ownership or transfers ownership because that's just going to massively complicate things and I'm gonna have to kind of keep track of that manually and I know that when another programmer comes up with the team and decides to write something else that many place that memory they're gonna probably screw things up okay so those are kind of the two big issues and the reason why we kind of want to automate the deletion of memory but really when we talk about safety and C++ and specifically all this smart pointers stuff we are specifically just automating one simple line of code and that is the deletion of memory the freeing of memory that is the only thing we're dealing with okay into it primarily in two ways and again the smart pointers video covers all of this so this isn't really new information in this series but it's just as we can go on with this series and with this channel we're gonna be actually building applications so in building frameworks saying building actual production code soon so because of that I want to just kind of be on the same page with all of you about that automating that single line of code is can be very important okay and I firmly believe that yes in 2018 doesn't matter about in fact I almost regret mentioning in 2018 because it doesn't matter at any time right in any kind of realm it is good to automate a lot of things in programming all right specifically the kind of freeing of memory because it seemed to be such a fragile and pedantic kind of operation that people just screw up all the time right so yes smart pointers are really good you should 100% use smart pointers never in my life would I say you know roll pointers only that's stupid okay I think I think everyone agrees our that's stupid not using the simple course standard kind of libraries smart pointers you know that's fine some people may choose to write their own right whether that just be a scope based pointer which is the equivalent of like a CV unique pointer or whether that be like a reference counting system which is basically the equivalent of an STD shared pointer that's fine right and then you can introduce concepts like awake references that's also fine right will wake pointers that's fine right you can make your own constructs I have nothing against that sometimes prefer to do that both for kind of the ability to customize them to their own needs or maybe try and squeeze out specific platform you know platform specific kind of performance constraints and concerns by writing their own system which they have a hundred percent control of totally fine right but if you choose to go with a one hundred percent rule pointers kind of strategy for yourself whether you're building it's not that you're a bad programmer it's just like why write like why do I do that it doesn't make any sense and the thing is I I'm somewhat guilty of this as well like if I'm running code really quickly I'll probably just default to using world pointers the biggest reason why I do that is because in the scope of things they are still for me personally easier to write and easier to read because there's no thought that goes into them you just put a little star and you're done right and then if I want to I can choose to delete memory but it probably doesn't even matter because again I'm talking about the constant the context of a simple sandbox application which probably won't be more than 100 lines of code because I just want to test out one little thing or whatever right and of course then in that case it's like well you know am I really gonna start dealing with it smart pointers like why that's gonna kind of turn my code into such a heavyweight burden right whereas if I just had rawrr pointers it would just be kind of cleaner easier to read and i don't care i don't care if i never free my memory I don't care about ownership right because all I'm writing is a little app and there are a lot of people who say well yes but even in a little sandbox application you should be using smart pointers because people might see your code and might learn from it and might be able then might do that everywhere but that's stupid I mean think about it really right why on earth should I not be able to write a rule pointer for a little sandbox application like how like pedantic and ridiculous do you have to be to decide that I'm never going to type out a roll pointer in my life like that's I just don't understand why like something a lot of people I have met in real life are actually like that which is a bit annoying to me and people some people just completely freak out when they see real point as well but ever seen me write real pointers which to me is very confusing because like hey man it's not like I killed someone I just you know I just decided to kind of manage memory myself a little bit like oh no like it's so kind of silly it sounds so silly to me and maybe that's just me being like not really taking responsibility for the fact that I do have like over 100,000 subscribers and all the people who watch my videos and learn from them and maybe I should be more responsible and then you know it's almost like you know if a child saves me crossing the road on a red light you know because I don't want to wait for the light to turn green before I run across the road like then a child sees that like that's kind of a bad thing a little bit of a bad influence on that child even though what I'm doing is fine right I know it's fine because they're gonna cause and you know I want to wait for the light to turn because I really need to hurry up but the child sees that and I'm a bad influence on the child is it kind of like that where I should be running we're all pointers I shouldn't be running role plays at all because some people wish for my videos might get the wrong impression and might not have the full context of why I'm doing that like that's certainly a please don't restart my computer in the middle of recording thank you because if this video fails I will be very upset anyway I've got a talk before Windows 10 restarts because I'm recording my audio through the computer and anyway doesn't matter the point is I'm probably dragging this on and I just really wanted to kind of talk about this so that's what this video is but anyway my point being that I think that people should a little bit stop about the whole roll point is very smart points thing in the context of a real framework or real application that will be used by people and might be production code yes use smart pointers it is a very very stupid move to not do that both for maintainability and kind of reliability of code as well as really just just being able to write code freely right without having to worry about when will my memory get frayed oh no I forgot to delete oh no what happens if I start passing this pointer around I'm gonna lose ownership of it all of that is solved with smart pointers mostly right maybe threading issues and other kind of things my bizarre things of course shared pointer is not thread safe and there are a bunch of other constraints that come with using smart points like oh my goat the solution to memory in general but in but of course just speaking typically but the typical case is that they do help you a lot so yes you should use them and we will definitely be using smart pointers as we get into more serious code don't get me wrong it's just that at the stage of learning C++ I firmly believe that you need to you need to know what a broad pointer is you need to know how memory actually works why because smart pointers are just a wrapper on top of a rule pointer that's all they are there additional auxilary code around a roll pointer in order to automate things which essentially are just deletion right and freeing of memory that is why they exist and if you don't know what a roll pointer is then you're not you do you're not aware of what's happening behind the scenes and since I'm trying to teach all of you to be good C++ program it's not just I'm capable of writing C++ code but more like I'm capable of writing real time kind of performance critical production C++ code you have to know how everything works that's why I make videos on how to compiler the linker work and that's why I like going in-depth into my videos because I'm trying to teach you guys how to actually be good programmers in C++ and in order to do that you need to and how things work so if a child pointer scares you then there's an issue here in what I'm trying to do and obviously your goals aren't meeting my goals for what I kind of want to teach so that's something that I think as a community we need to talk about a lot more because yeah it just ends up kind of you know people end up saying that I'll you know I'm teaching or obviously for class or whatever because I'm using old kind of things but the fact of the matter is in my opinion and this is just my opinion there is no factual information about you should never use smart pointers or you should always use smart pointers those are two opinions in the real world what you use will matter a lot on what you what it is you're actually doing right this channel at the end of the day is just my opinion and in my strong opinion you need to know all these old things you need to know how simple sauce works and how old these things work and say buzz-buzz if you want to actually be a good supercross programmer right that's it that's my strong opinion if you don't like that then you probably you know there's plenty of other good save response resources out there I by no means I'm the only one or the best one or anything like that I'm just one of the many signals wealth resources out there on the Internet you can buy books you can do whatever you want but that's just what I think which is why on my channel that's what I'm going to teach anyway I hope you guys enjoy this video if you did you can hit the like button the comments section below is made for discussion so please let me know what you think and about your opinions about you know programming kind of stable as well safely I guess but also what things you want to kind of like if you think I'm focusing on the right things if you think if you think I'm focusing on the wrong things and your opinion about the whole smart points role bonus thing I really want to read and reply to hopefully much those comments and this port is the other really good place to check out kind of further discussion on single cells in general and also all of my videos and all that stuff anyway I'm really excited for what's coming in the future for this channel cuz I mean I think it's been a month since my last video and I've been both kind of dealing with kind of a bunch of stuff in my personal life as well as on the down-low kind of planning you know the the next year or so really of this channel so I'm really excited to to get into that and I'm sure you guys just all the cool stuff is fun so I'll see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 84,682
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, safety, modern c++
Id: CWglkNBUmD4
Channel Id: undefined
Length: 17min 51sec (1071 seconds)
Published: Sun Sep 16 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.