• lazyneet@programming.dev
    link
    fedilink
    arrow-up
    20
    arrow-down
    5
    ·
    5 months ago

    I’ve been using C++ almost daily for the past 7 years and I haven’t found a use for shared_ptr, unique_ptr, etc. At what point does one stop being a noob?

    • AngryPancake@sh.itjust.works
      link
      fedilink
      arrow-up
      26
      arrow-down
      1
      ·
      5 months ago

      Given that you probably are using pointers, and occasionally you are allocating memory, smart pointers handle deallocation for you. And yes, you can do it yourself but it is prone to errors and maybe sometimes you forget a case and memory doesn’t get deallocated and suddenly there is a leak in the program.

      When you’re there, shared_ptr is used when you want to store the pointer in multiple locations, unique_ptr when you only want to have one instance of the pointer (you can move it around though).

      Smart pointers are really really nice, I do recommend getting used to them (and all other features from c++11 forward).

      • arendjr@programming.dev
        link
        fedilink
        arrow-up
        7
        ·
        5 months ago

        Smart pointers are really really nice, I do recommend getting used to them (and all other features from c++11 forward).

        You’re recommending him to give up his sanity and/or life?

        • porgamrer@programming.dev
          link
          fedilink
          arrow-up
          2
          ·
          5 months ago

          I would have said the same thing a few years ago, but after writing C++ professionally for a while I have to grudgingly admit that most of the new features are very useful for writing simpler code.

          A few are still infuriating though, and I still consider the language an abomination. It has too many awful legacy problems that can never be fixed.

    • riodoro1@lemmy.world
      link
      fedilink
      arrow-up
      11
      arrow-down
      2
      ·
      5 months ago

      This guy probably still uses a char*.

      What have you been using it daily for? arduino development? I’m hoping no company still lives in pre C++17 middle ages.

    • MajorHavoc@programming.dev
      link
      fedilink
      arrow-up
      8
      ·
      5 months ago

      At what point does one stop being a noob?

      I recognize that trick question. For C++, the answer is always “soon”.

    • Fushuan [he/him]@lemm.ee
      link
      fedilink
      English
      arrow-up
      3
      ·
      5 months ago

      Shared poibters are used while multithreading, imagine that you have a process controller that starts and manages several threads which then run their own processes.

      Some workflows might demand that an object is instantiated from the controller and then shared with one or several processes, or one of the processes might create the object and then send it back via callback, which then might get sent to several other processes.

      If you do this with a race pointer, you might end in in a race condition of when to free that pointer and you will end up creating some sort of controller or wrapper around the pointer to manage which process is us8ng the object and when is time to free it. That’s a shared pointer, they made the wrapper for you. It manages an internal counter for every instance of the pointer and when that instance goes out of scope the counter goes down, when it reaches zero it gets deleted.

      A unique pointer is for when, for whatever reason, you want processes to have exclusive access to the object. You might be interested in having the security that only a single process is interacting with the object because it doesn’t process well being manipulated from several processes at once. With a raw pointer you would need to code a wrapper that ensures ownership of the pointer and ways to transfer it so that you know which process has access to it at every moment.

      In the example project I mentioned we used both shared and unique pointers, and that was in the first year of the job where I worked with c++. How was your job for you not to see the point of smart pointers after 7 years? All single threaded programs? Maybe you use some framework that makes the abstractions for you like Qt?

      I hope these examples and explanations helped you see valid use cases.

      • lazyneet@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        5 months ago

        When you bring threads into it, these exotic features make more sense. I have been doing single-threaded stuff for the most part.