std::unique_ptr - A scoped smart pointer | Modern Cpp Series Ep. 33

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

►Lesson Description: In this lesson I show you how to use a std::unique_ptr. I will show you why we might want to use this smart pointer versus a raw pointer, and how to create this pointer. While we haven't covered classes yet in this series, it's important to realize that this smart pointer is essentially a 'wrapper' around a raw pointer that calls the destructor to free memory when the pointer leaves scope.

Note: Something not covered in this lesson is the idea of a 'custom deleter' which can be useful.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

I can't believe I'm getting world class content for free from one of the best c++ influencer/programmer out there. This channel is underrated by a huge margin. Thanks Mike

yb
Автор

Question. At the end of the video, talking about using move semantics with unique pointers. After transferring ownership of the UDT class in your example from `mike` to `joe`, does `mike` become a "dangling unique pointer" until the end of the local scope?

shanemcleod
Автор

Very well explained as always. Thank you.

dhanushs
Автор

Good explanation. On line 20, after you move mike to joe, if you access mike what happens. Is it caught at the compilation time or runtime?

wpfbeginner
Автор

Which of the smart pointers best resemble a raw pointer?

QWin-iryq
Автор

Keep coming back to this just to sink in.
Can we replace( line 21, 14.48mins time elapse) “std::unique_ptr<UDT[]> mike_array = std::make_unique<UDT[]>(10);” with
“auto
It seems to be working on MVS 2022 with either ISO 14, 17, 20 as default.
What’s the difference?

joebosah
Автор

best teacher ever, subscribe guys lets make him big

menachemlevi
Автор

Great video Mike. Could you clarify this for me? Can we pass a unique pointer through threads? What would be the behavior? Thank you very much.

wika
Автор

@ Dr mike is there a difference between move and release function in unique_ptr.
Is unique_ptr<int> p1 = make_unique<int>(10);
unique_ptr<int> q1 = std::move(p1) ;
vs
unique_ptr<int> p1 = make_unique<int>(10);
unique_ptr<int> q1 = p1.release() ;
fundamentally the same ?

guitarlover
Автор

Hi Mike! Great explanations as always, thank you very much for the excellent content!

I would have a question tho: I understand what unique_ptr is or that it can't be copied but moved, its resource (pointee) can be pointed by only that particular uniue_ptr etc. However, I still don't really get "when" do we need it? Can you give a real life example where we have single ownership of a resource so that we need a unique pointer there?

P.S. I am actually planning to ask the same question for shared_ptr as well on shared_ptr video. So, if you'd like to answer both here, I would appreciate. If not, I would still appreciate your unique_ptr real life example here :)

Thanks a lot again!

iea-iea
Автор

Why don't we just use stack variable, then? One name for one memory (on stack). Destroy together with stack memory?

peerajak
Автор

I see a Cambridge shirt! Are you from Mass?!

klutch
Автор

Because you cannot be trusted to remember "delete mike;" or "delete [] mike;", the solution is to remember :
std::unique_ptr<UDT> mike = std::unique_ptr<UDT>(new UDT);

safatkhan
Автор

Wow! Not even a warning for wrong delete! 🤯 And it runs everywhere on everything 😅

Kirfx