C++ 11 Library: Shared Pointer - I

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone we all know that pointers are very important feature in tsipras pass they are very powerful they enable you to do very low-level operations which are not possible in other languages however pointers are often troublemakers let's look at our example we have a dog and the dog has a constructor and destructor and the bug function now let's say we have a function foo and inside the function we create a dog let's call it a corner and then we did a bunch of different things and then we delete the dog and then we did a bunch of other things and then when you want to use the dog again so we call P Park this is silly right because the dog gunner has been deleted how can you use the P again so P is a dangling pointer and if you're using a lob jecht that's deleted the result is undefined behavior if we don't delete P then the bug function will be executed okay but by the end of this function the dog gunner is never deleted so the storage of the dog is never the allocated so the result is a memory leak it may seem silly in our simplistic example here but these two kind of bugs happen a lot in synchronous programming if you think about it what is the main trouble here the main trouble is with the delete we were not able to delete the dog at the dub propria time if we deleted a dog too early we have a dangling pointer if we forgot to delete at all then we have a memory leak but the problem is keeping track of when to delete every object is not only hard it is a tedious work a programmer should never do tedious work tedious work is meant to be done by the computer this is why we need smart pointers if you include the memory header you can use the different flavor of smart pointers today will mainly talk about shared pointer now let's look at an example of using shared pointer let's delete all these the way to use a shared pointer is very simple all we need to do is wrap up the law pointer with a shared pointer shared put dog P and P is constructed with a new object of dog gunner the way the shared pointer works is it keep track of how many pointers or how many shared pointers are pointing to the object and when that number becomes zero that object will be deleted automatically so at this point the count equal to one because we have one shared pointer P that is pointing to gunner and by the end of the full function because P will go out of scope so there will be no pointer or no shared pointer pointing to the gunner so the count becomes zero and at this point the dark gunner will be destroyed in this case the pointer is not really shared because we only have one pointer that's pointing to gunner suppose we have another shared pointer dog P 2 and P 2 is also pointing to gunner the dog and now we have a situation that the pointer is shared and suppose P 2 parked so at this point the count become two and at this point the count become one again because P 2 goes out of scope so there's only one pointer that's pointing to Ghana and by the end of the full function again the count becomes zero and the dog is destroyed and if we call the full function in the main function and then run the program it brings out dub is created dr. Lewis dr. Lewis and dr. is destroyed and that the shared pointer actually has a member function which can report how many shared pointers are pointing to the object so if I print out P dot use count this actually will print out to notice that I use arrow to access the objects member and I use thought to access the shared point itself member there is a very important fit for that you need to be careful suppose I want to use the shared pointer in the main function also I'll create a dog D equal to new dog let's call it tank and then I create a shared pointer dog P which is constructed from the dog T and then I create another shared pointer ptoo also created from the dark tank so now I would think that P and P 2 are two shared pointers pointing to a tank and when both P and P two goes out of scope the tank will be destroyed actually this is a very bad way of using the shared pointer so what has happened is when P is constructed from the docs pointer D P maintains account value of 1 so P get use count equal to 1 and when P 2 is constructed from the doc pointer dp2 also maintains a count of 1 P 2 dot to use count equal to 1 so when P goes out for scope the dog tank will be destroyed and when P 2 goes out to scope the dog tank will be destroyed again which is undefined at the behavior so the key thing about using shared pointer that you should remember is an object should be assigned to a smart pointer as soon as it s created law Poynter should not be used again and in this case since we create an object but we haven't immediately assigned it to a shared pointer and later on we use the wrong pointer again so that causes the trouble the correct way of using it is like this once the object is created immediately assigned to a shared pointer and then later on that the object is only accessed through p2 or P which are shared pointers so this is a bad idea and because this issue is so important C++ has provided a shortcut way of wrapping an object with shared pointer so the shortcut is like this shared pointer dog P equal to make shared dog and this function will take the parameter that you use to construct a doll tank this is the preferred way of creating a shared pointer it is even better than this one because this guy is not only faster but also safer if you look at this code what has happened are at least two steps step one gunner is created and the step two P is created with the dog gunner so it has two steps while the make sure tunc ssin combines these two steps into one single step so make shared is faster and in addition to that if you look at what happened here what if the gunner is created successfully but the shared pointer P has failed to be created maybe because of memory allocation failure then the dog gunner will end up not being managed by a shared pointer therefore it will not be deleted and its memory will be leaked so this code is not exception safe but this code is exception safe so as I said once you have started using shared pointers you should always use shared pointers to access those dynamically created objects and as you've seen a shared pointer can be used pretty much like a regular pointer you can use the arrow operator because it is over low overloaded for shared pointer you can even use the deliverance operator mark there's one more thing with a row pointer we can cast it into a different type of pointer can we do that with jet pointer yes we can with shared pointer there are a couple of special function just for that purpose there's a static pointer cast there's a dynamic pointer cast and the Const pointer cast so you can use these functions to cast a shared pointer just like you're casting a law pointer thank you for watching feel free to subscribe to my channel and check out the other videos I have see you next time
Info
Channel: undefined
Views: 80,209
Rating: 4.9539413 out of 5
Keywords: C++, programming, coding, computer, free, class, lecture, skill, language
Id: qUDAkDvoLas
Channel Id: undefined
Length: 11min 57sec (717 seconds)
Published: Fri Oct 18 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.