No video

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

  Рет қаралды 9,783

Mike Shah

Mike Shah

Күн бұрын

►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►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.
►KZfaq Channel: / mikeshah
►Please like and subscribe to help the channel!

Пікірлер: 42
@yb9737
@yb9737 2 жыл бұрын
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
@MikeShah
@MikeShah 2 жыл бұрын
Thank you for the kind words :) More to come, spread the word!
@k0185123
@k0185123 6 ай бұрын
Totally agreed!!!
@edoardogribaldo2870
@edoardogribaldo2870 Жыл бұрын
Astonishing explanation !
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@dhanushs1802
@dhanushs1802 2 жыл бұрын
Very well explained as always. Thank you.
@MikeShah
@MikeShah 2 жыл бұрын
Cheers!
@menachemlevi
@menachemlevi 6 ай бұрын
best teacher ever ,subscribe guys lets make him big
@MikeShah
@MikeShah 6 ай бұрын
Cheers -- very kind of you! :)
@syed6044
@syed6044 2 жыл бұрын
Thank you very much Mike...
@MikeShah
@MikeShah 2 жыл бұрын
You're most welcome!
@wika96
@wika96 Жыл бұрын
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.
@MikeShah
@MikeShah Жыл бұрын
If those threads are only reading the value without any synchronization, then you can assume thread-safe. If some threads are reading/writing, then unique_ptr is not immune to thread-safety issues. For example, a thread could call unique_ptr::get and then essentially break the invariant around the 'uniqueness' of the unique_ptr.
@wika96
@wika96 Жыл бұрын
@@MikeShah Thank you
@k0185123
@k0185123 6 ай бұрын
Goooooooooood!!!!!!!!!!!!!!!!
@QWin-ir6yq
@QWin-ir6yq 3 ай бұрын
Which of the smart pointers best resemble a raw pointer?
@MikeShah
@MikeShah 3 ай бұрын
None of them really -- each smart pointer is putting some restriction to help you write safer code. A quick summary: shared_ptr is the most flexible, and still saves you from double-frees -- so perhaps this is the closest in some sense. weak_ptr may or may not be valid memory (but you can check, and still allows you to share memory), and unique_ptr is exactly a pointer, except you can only have ownership of one piece of memory (limiting sharing).
@mav474
@mav474 Жыл бұрын
Thanks Mike
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@7guitarlover
@7guitarlover Жыл бұрын
@ Dr mike is there a difference between move and release function in unique_ptr. Is unique_ptr p1 = make_unique(10); unique_ptr q1 = std::move(p1) ; vs unique_ptr p1 = make_unique(10); unique_ptr q1 = p1.release() ; fundamentally the same ?
@peerajak
@peerajak 7 ай бұрын
Why don't we just use stack variable, then? One name for one memory (on stack). Destroy together with stack memory?
@MikeShah
@MikeShah 7 ай бұрын
Might need to allocate on run-time either something that is variable size, or otherwise if we have a large allocation, we need a pointer. That said, we could just make one very large allocation at run-time and then manage the memory ourselves with a local-arena, which is in fact what some applications do.
@wpfbeginner
@wpfbeginner 6 ай бұрын
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?
@MikeShah
@MikeShah 6 ай бұрын
Going to be caught at runtime unless your compiler or tooling provides a warning on later access. It depends on the datatype, but most likely the 'move' operation will make 'mike' a nullptr and then give you a segfault.
@joebosah2727
@joebosah2727 Жыл бұрын
Keep coming back to this just to sink in. Can we replace( line 21, 14.48mins time elapse) “std::unique_ptr mike_array = std::make_unique(10);” with “auto mike_array{make_unique(10)};” It seems to be working on MVS 2022 with either ISO 14,17, 20 as default. What’s the difference?
@MikeShah
@MikeShah Жыл бұрын
Yup, you could use auto there for sure, auto will deduce the type automatically.
@joebosah2727
@joebosah2727 Жыл бұрын
@@MikeShah thank you. It’s 3:55am here
@yea-yea
@yea-yea Жыл бұрын
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!
@MikeShah
@MikeShah Жыл бұрын
Anywhere you would use a raw pointer, you can use a std::unique_ptr. So any time you heap allocate with new, consider substituting with a unique_ptr if exactly 1 thing is going to have a reference to that piece of memory. So for example, if you are loading a bunch of text data from a file into a buffer, you might heap allocate a large collection of bytes that are read into that buffer. If multiple objects need to look at that buffer, then use a shared_ptr, otherwise a unique_ptr.
@klutch4198
@klutch4198 2 жыл бұрын
I see a Cambridge shirt! Are you from Mass?!
@MikeShah
@MikeShah 2 жыл бұрын
Not originally, but yes spend time in Mass nowadays! New England is wonderful!
@klutch4198
@klutch4198 2 жыл бұрын
@@MikeShah Glad you enjoy it!! I'm Western Mass native myself thats why I had to ask haha
@MikeShah
@MikeShah 2 жыл бұрын
@@klutch4198 Awesome!
@klutch4198
@klutch4198 2 жыл бұрын
@@MikeShah Just curious - do you have any ideas for some program ideas I should build for practice? I've built hundreds of apps of all kinds (I am a developer) but when it comes to CPP all I have so far is my magic eight ball haha. It's just so powerful and fast that program ideas for langs like C, Rust, etc. can be a little tricky because of said power and speed. Also, I'm currently writing cpp for the course on a raspberry PI instead of my mac mini just for it's simplicity with linux (so I dont mess with my current dev environment on mac)
@MikeShah
@MikeShah 2 жыл бұрын
@@klutch4198 very cool! As for projects, there's a nice summary here of some ideas: austinhenley.com/blog/challengingprojects.html I think any of these are great for cpp practice. If you do anything gaming related, my SDL2 or SFML series can help get you setup.
@Kirfx
@Kirfx 8 ай бұрын
Wow! Not even a warning for wrong delete! 🤯 And it runs everywhere on everything 😅
@MikeShah
@MikeShah 8 ай бұрын
Timestamp?
@Kirfx
@Kirfx 8 ай бұрын
@@MikeShah 4:40. I mean program compiles with wrong delete call, doesn't throw a warning, doesn't crash just silently leak memory. I bet this behaviour is sitting there for many years and for some reason wasn't fixed. Instead it's recomended to do Coding Standards and Reviews, Smart Pointers, Custom Allocators, Static Analysis Tools, Modern C++ Features. These recommendations are all great, but what about fixing obviously dangerous behaviour or at least to warn the programmer with message from compiler? I'm just trying to estimate how dangerous C++ programming actually is.
@MikeShah
@MikeShah 7 ай бұрын
I would think most static analysis would catch this type of bug -- but you are correct that you should be a delete[] :) @@Kirfx
How I Did The SELF BENDING Spoon 😱🥄 #shorts
00:19
Wian
Рет қаралды 36 МЛН
7 Days Stranded In A Cave
17:59
MrBeast
Рет қаралды 83 МЛН
26. decltype, CTAD. Умные указатели unique_ptr, shared_ptr
1:30:27
Мещерин Илья
Рет қаралды 2,9 М.
Unique Pointer In C++
13:55
CppNuts
Рет қаралды 44 М.
SMART POINTERS in C++ (for beginners in 20 minutes)
24:32
CodeBeauty
Рет қаралды 98 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 353 М.
Introduction to constexpr | Modern Cpp Series Ep. 86
10:56
Mike Shah
Рет қаралды 11 М.
How I Did The SELF BENDING Spoon 😱🥄 #shorts
00:19
Wian
Рет қаралды 36 МЛН