unique_ptr is NOT just for heap allocations | custom deleters

Поделиться
HTML-код
  • Опубликовано: 8 май 2023
  • 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 Год назад +60

    Pleasure to be mentioned in your video π.

    • @heroe1486
      @heroe1486 Год назад +4

      Video uploaded 29 minutes ago, Pi Launchpad · 11h ago

    • @joedartonthefenderbass
      @joedartonthefenderbass Год назад +5

      @@heroe1486 people can be givern early access to the video

    • @fuexfollets5755
      @fuexfollets5755 Год назад +1

      Its the pi man again!

    • @heroe1486
      @heroe1486 Год назад +1

      @@joedartonthefenderbass Even without RUclips 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 RUclips so yeah

  • @delta3244
    @delta3244 Год назад +30

    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 Год назад +30

    > 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 Год назад +5

    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 Год назад +19

    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 Год назад +3

      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.)

  • @KaruniaSentosa
    @KaruniaSentosa Год назад +4

    Thank you mCoding for your insight! The custom deleter (4:23) for closing open files gives me a whole new meaning about the Deleter.

  • @Bittboy
    @Bittboy Год назад +9

    Definitely do a video on Empty Base Object Optimization, and maybe even delve into Curiously Recurring Template Patterns as well!

  • @Hobo10000000000
    @Hobo10000000000 Год назад +6

    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 11 месяцев назад

    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.

  • @cn-ml
    @cn-ml Год назад +5

    "rust users are typing" - more like "rust users are already rewriting"

  • @tommclean9208
    @tommclean9208 Год назад +3

    Im on C++ 17 and I want to hear how to do it! :)

  • @catcatcatcatcatcatcatcatcatca
    @catcatcatcatcatcatcatcatcatca Год назад +2

    But sometimes I just like leaking bit of memory here or there.

  • @lerdi
    @lerdi 5 месяцев назад

    How's there not something like std::free_deleter functor in standard library, implemented like your FreeDeleter?

  • @yoavmor9002
    @yoavmor9002 7 месяцев назад

    Can't you use C style unions to get both the deallocator and the pointer to share memory?

  • @48_subhambanerjee22
    @48_subhambanerjee22 2 месяца назад +1

    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

  • @gooble1
    @gooble1 Год назад

    good fucking stuff.

  • @quintrankid8045
    @quintrankid8045 Год назад +2

    I'm commenting because I'd like to find out how to use less memory.

  • @rastaarmando7058
    @rastaarmando7058 Год назад +1

    I am pretty sure [[no_unique_address]] does not allow members to overlap.

    • @mCoding
      @mCoding  Год назад +1

      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.

  • @raidensama1511
    @raidensama1511 Год назад +1

    🥂Rust

  • @AntonioNoack
    @AntonioNoack Год назад +1

    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 Год назад +5

      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 Год назад +1

      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.

  • @gubigm
    @gubigm Год назад +4

    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 Год назад +1

    Okay but how unique_ptr is not just for heap allocations

    • @isodoubIet
      @isodoubIet Год назад +8

      He literally just explained that

    • @DebashishGhoshOfficial
      @DebashishGhoshOfficial Год назад +6

      You can use it for any resourse which needs to be cleaned up. Not every resource is a heap allocated memory.