SMART POINTERS in C++ (std::unique_ptr, std::shared_ptr, std::weak_ptr)

preview_player
Показать описание


Thank you to the following Patreon supporters:
- Samuel Egger

Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

This is one of the best series about C++. It's clear, it's fast and it has a charming and confident tutor.

cpdorli
Автор

A bit late but after listening to several experts, it seems that the authors of the standard and the guidelines are VERY clear about when to use raw or smart pointers. I'll try to summarize them.

1. NEVER use new or delete. It has to be very clear wether a pointer owns the object or doesn't. If it owns the object (for example, it is creating the object), use a smart pointer and make_unique or make_shared.

2. By default, use unique_ptr because it has almost no overhead. If you know it will need to have several owners, use shared_ptr.


3. When the pointer will not own the object, use raw pointers. For example, when passing an object to a function, the pointer that receives it will not own the object (it will still be alive once the function returns), so don't pass the smart pointer, pass a raw pointer.
To pass a unique_ptr as a raw pointer to a function, the best way is to dereference it and pass it by reference. So the function is just for example "void foo(const Class& myObject)", and you call it with "foo(*myPointer)".
Another way would be to pass the adress itself, with the method "unique_ptr.get()".

4. Again, never use delete on a raw pointer and assume it does not own the object.

5. When you create an object inside a function and want to return it, do it as a unique_ptr. The receiver will be able to do whatever they want with it. Keep in mind that now, passing a local object from a function by copy will not actually copy it, but move it as an r-value. So it will move into the new unique_ptr, without a compiler error. Again, the receiver can do whatever they want, so they can move it into a shared_ptr or even a raw pointer.

6. When you want to transfer ownership of a unique_ptr, you can do it as a r-value using std::move(). This is basically what I just explained that C++ does when returning from a function. You can do it to transfer the ownership to a function.


These are the basic guidelines with which you will probably never have memory leaks or loose pointers, they helped me a lot.

Iamthehe
Автор

These are no mere tutorials ... no, my good friends, these are timeless works of art! Your work is much appreciated!

everettmthunzi
Автор

I've never watched a better quality tutorial series than this. Thank you so much for making these, cheers!

anicsr
Автор

*My takeaways:*
1. Smart pointers are used to automatically assign and delete heap memory 0:25
2. Unique pointer 1:12
3. Shared pointer 5:00
4. Weak pointer 8:20
5. When should we use them 9:30

leixun
Автор

I love how the your code has a large enough text size that I can read it, even without full screening the video
thanks for this. the garbage collector is one of the biggest reasons I stooped using c++ 10 years ago and switched to c#

kylefillingim
Автор

Hey Cherno, I love this series.
I think that a topic that would be good to cover since many places don't have a good explanation would be rvalue and lvalue references. This was a major pain point for me when learning c++ but is so integral to the language.

DaRealPielover
Автор

Im 5 years late to this video 😢.
You changed my whole perspective on them thank you.

naveensenapati
Автор

I used to be of the mindset that smart pointers were just for people who couldn't be bothered to manually manage memory properly, as if they were a cheap alternative, and I'm glad I'm over that phase now! On top of the advantages you mentioned in this video, I really like them for semantics. Seeing that a dynamically allocated member is a unique_ptr tells me that the parent class will own/be responsible for the object. I still use raw pointers to indicate that a class does not care about owning/managing the pointed-to object, it just wants a brief reference to it (provided that the lifetime of the parent class exceeds that of the object obviously). AFAIK, C++17 is bringing with it the "std::observer_pointer" which is literally just a raw pointer with a more semantically-useful name (whether or not it comes under 'smart' pointer is debatable). It has exactly the same purpose as the raw pointers I use currently and hints that the parent is simply observing the object. Anyway awesome video as usual Cherno, keep up the amazing content! :)

lewisb
Автор

Hey Cherno, re-watching your series as they're always a good refresher. Will, you ever do a video about 3:23 talking about exceptions and why you don't like them?

Xxpr
Автор

OMG! Cherno! You make these things so clear that takes other teachers forever to explain.

elgs
Автор

Thanks for the video.
All your teachings are top notch.
There's a reason why you show up first when researching a c++ topic.

SteveAdamRocks
Автор

Thanks for the video. Didn't get smart pointers in any of my C++ classes since colleges still teach C++98 :\

LucidStew
Автор

Currently taking a distance course at uni on C++, working with smart pointers, and this video gives a much neater explanation tham the course material (which for me is in a language I'm not that good at anyways). This really helped clear up some confusions I had about smart pointers based on the course material, thanks

Croccifixo
Автор

I was lucky to have Professor in data structures who taught as hobby and was a VP of a start up. He taught us modern c++ practiced today. Incorporated C++11 to C++17 features. He is a gem because I hear a lot of people learning C in college, with little to no strong OOP fundamentals

jimba
Автор

Very informative video. I am C# unity game developer but when I YouTube search Stack vs Heap I found one of video on it. It was so informative so I decided to watch full playlist and now I am on 43 video.

faizydeveloper
Автор

you've explained the essence of the topic in literally several short sentences, this is immediate subscription + notifications on + lifetime respect from me

postdisc
Автор

I love that idea of implementing stuff in the STL our selves. Great way to learn.

marcusk
Автор

10:46 what you need is like some kind of smart pointer so that you can dynamically point to where youtube moves the like button

jonf
Автор

You can make a shared_ptr from a unique_ptr. Using assignment AKA the = operator or std::move(). This make it convent to return a unique_ptr and let the caller determine if they want it to be a shared_ptr or not.

shadowwalker
join shbcf.ru