unique_ptr is NOT just for heap allocations | custom deleters

  Рет қаралды 11,959

mCoding

mCoding

Жыл бұрын

Define your own deleter.
The smart pointer type unique_ptr is NOT just for automating deletes of heap-allocated pointers. By specifying a custom deleter type, you can use them to clean up any resource that doesn't clean up after itself. Yes, you can delete a new, but you can also free a malloc, fclose an fopen, or deallocate an allocate for a custom allocator type (feel free to use a stack allocator!). This can be useful for interoperating with C code or code from other languages, as well as to clean up after classes or incidental data structures that don't otherwise clean up after themselves.
― mCoding with James Murphy (mcoding.io)
Source code: github.com/mCodingLLC/VideosS...
unique_ptr intro video: • unique_ptr: C++'s simp...
SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!
/ mcoding
Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
www.paypal.com/donate/?hosted...
Want to donate crypto? Check out the rest of my supported donations on my website!
mcoding.io/donate
Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information, Pi
BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: / discord
Github: github.com/mCodingLLC/
Reddit: / mcoding
Facebook: / james.mcoding

Пікірлер: 36
@PiLaunchpad
@PiLaunchpad Жыл бұрын
Pleasure to be mentioned in your video π.
@heroe1486
@heroe1486 Жыл бұрын
Video uploaded 29 minutes ago, Pi Launchpad · 11h ago
@joedartonthefenderbass
@joedartonthefenderbass Жыл бұрын
@@heroe1486 people can be givern early access to the video
@fuexfollets5755
@fuexfollets5755 Жыл бұрын
Its the pi man again!
@heroe1486
@heroe1486 Жыл бұрын
@@joedartonthefenderbass Even without KZfaq membership ? I initially thought maybe he uploaded it by error (or whatever) and then revert it to private. Responding to my own question : Even if it's not explicitly possible you can still have a video only accessible via the link and not listed on KZfaq so yeah
@delta3244
@delta3244 Жыл бұрын
I'm not quite stuck on C++ 17, but I'm still rather interested in learning about the empty base object optimization. Thank you for this video!
@canyon_online
@canyon_online Жыл бұрын
> as much as we love C++ there are times where an existing library that is already written in C is a better choice than writing it in C++ * eyes ffmpeg nervously *
@Finkelfunk
@Finkelfunk Жыл бұрын
This is easily one of the best C++ channels out there. These videos help so much in getting to know the language better. If you ask me, I'm all for going over the most basic STL functions and how to make custom versions of them in some form.
@Quasar6X
@Quasar6X Жыл бұрын
The [[no_unique_address]] attribute is cool, but you forgot to mention that, taking the address of such member is UB. Quoted from the standard "Taking the address of such member results in an address that may equal the address of some other member of the same object." That means the getters for the deleter may actually return the owned ptr.
@AJMansfield1
@AJMansfield1 Жыл бұрын
Calling the function from that pointer would be UB, but taking the address isn't itself UB. If it were, the compiler would happily apply the fact that Undefined Behavior Does Not Occur to optimize away the existence of the entire function, if you just took the address of the member and did some arithmetic on it. (This is the real purpose of UB btw: not only can the compiler do whatever it wants for the UB itself, it can assume that any program state or code path that would lead to UB is unreachable or does whatever is most convenient/optimal.)
@Bittboy
@Bittboy Жыл бұрын
Definitely do a video on Empty Base Object Optimization, and maybe even delve into Curiously Recurring Template Patterns as well!
@KaruniaSentosa
@KaruniaSentosa Жыл бұрын
Thank you mCoding for your insight! The custom deleter (4:23) for closing open files gives me a whole new meaning about the Deleter.
@Hobo10000000000
@Hobo10000000000 Жыл бұрын
My company is mandating c++17. I'm envious of everyone getting to use 20 in production. Have you covered writing Python-esque generators in C++? I think that'd be an interesting topic to cover.
@MaxSzalinski
@MaxSzalinski 10 ай бұрын
Thanks for clarifying that mystic "custom deleter" topic! I'd love to hear about Empty Base Object Optimization!
@DebashishGhoshOfficial
@DebashishGhoshOfficial Жыл бұрын
Hats off to the library implementors. And as usual, nice video.
@yoavmor9002
@yoavmor9002 6 ай бұрын
Can't you use C style unions to get both the deallocator and the pointer to share memory?
@cn-ml
@cn-ml Жыл бұрын
"rust users are typing" - more like "rust users are already rewriting"
@48_subhambanerjee22
@48_subhambanerjee22 Ай бұрын
Do you have all c++ topics covered... I am learning c++... I know rust cant replace c++ in the near future... As its deeply rooted in every legacy systems
@lerdi
@lerdi 4 ай бұрын
How's there not something like std::free_deleter functor in standard library, implemented like your FreeDeleter?
@tommclean9208
@tommclean9208 Жыл бұрын
Im on C++ 17 and I want to hear how to do it! :)
@catcatcatcatcatcatcatcatcatca
@catcatcatcatcatcatcatcatcatca Жыл бұрын
But sometimes I just like leaking bit of memory here or there.
@quintrankid8045
@quintrankid8045 Жыл бұрын
I'm commenting because I'd like to find out how to use less memory.
@gooble1
@gooble1 Жыл бұрын
good fucking stuff.
@rastaarmando7058
@rastaarmando7058 Жыл бұрын
I am pretty sure [[no_unique_address]] does not allow members to overlap.
@mCoding
@mCoding Жыл бұрын
On the contrary, *all* it does is allow members to overlap. en.cppreference.com/w/cpp/language/attributes/no_unique_address Obviously, it cannot allow overlap in real data, but in particular it can allow overlap in padding.
@anon_y_mousse
@anon_y_mousse Жыл бұрын
@@mCoding I think some people might need more explanation for that one. Possibly they didn't understand what it means that the standard requires an object take up at least one byte even though it doesn't actually exist which allows the overlap to not cause problems. Which is actually kind of a cool space optimization for something which may be used millions of times over in a single program at a given point in its life.
@AntonioNoack
@AntonioNoack Жыл бұрын
As Gabriel said, the title was not well explained in this video. What other allocation would that be? Stack? Why would you need unique_ptr for that?
@Finkelfunk
@Finkelfunk Жыл бұрын
It actually is exactly what it sounds like. He even mentioned things like opening a file and having a custom deleter for closing a file. Not every resource is a heap allocation
@claytongearhart7671
@claytongearhart7671 Жыл бұрын
4:14
@AntonioNoack
@AntonioNoack Жыл бұрын
@@Finkelfunk Ah, the title has two possible meanings :) with heap allocations, and heap-allocations. I wanted to know other allocations, but it's about completely different resources.
@raidensama1511
@raidensama1511 Жыл бұрын
🥂Rust
@gubigm
@gubigm Жыл бұрын
Great video. But. But guys, don't you honestly think this stuff is horrible?
@anon_y_mousse
@anon_y_mousse Жыл бұрын
Yes, but as horrible as it is, it's still also really cool and I wish C had some of these features. This is why I'm writing my own language, C++ sucks and C is incomplete.
@gabrielpar3519
@gabrielpar3519 Жыл бұрын
Okay but how unique_ptr is not just for heap allocations
@isodoubIet
@isodoubIet Жыл бұрын
He literally just explained that
@DebashishGhoshOfficial
@DebashishGhoshOfficial Жыл бұрын
You can use it for any resourse which needs to be cleaned up. Not every resource is a heap allocated memory.
unique_ptr: C++'s simplest smart pointer
11:54
mCoding
Рет қаралды 39 М.
Return Value Optimization and Copy Elision in C++
9:54
mCoding
Рет қаралды 36 М.
КАКОЙ ВАШ ЛЮБИМЫЙ ЦВЕТ?😍 #game #shorts
00:17
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 4,3 МЛН
FOOTBALL WITH PLAY BUTTONS ▶️ #roadto100m
00:29
Celine Dept
Рет қаралды 73 МЛН
Intro to async Python | Writing a Web Crawler
14:23
mCoding
Рет қаралды 73 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 97 М.
Python's collections.abc | InvertibleDict
14:00
mCoding
Рет қаралды 44 М.
Python's most DISLIKED __dunder__ (and what to use instead)
9:59
how NASA writes space-proof code
6:03
Low Level Learning
Рет қаралды 2 МЛН
Every Python dev falls for this (name mangling)
14:11
mCoding
Рет қаралды 135 М.
C++ Weekly - Ep 404 - How (and Why) To Write Code That Avoids std::move
8:50
C++ Weekly With Jason Turner
Рет қаралды 24 М.
C++ cache locality and branch predictability
10:43
mCoding
Рет қаралды 76 М.
🤔Почему Samsung ПОМОГАЕТ Apple?
0:48
Technodeus
Рет қаралды 459 М.
Samsung or iPhone
0:19
rishton vines😇
Рет қаралды 8 МЛН
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 9 МЛН