What is the deal with smart pointers, unique pointers and shared pointers?

What is the deal with smart pointers, unique pointers and shared pointers? Why do we need these instead of regular pointers for C++?

POSIWID: The Purpose Of A System Is What It Does Shirt $21.68

Ape Out Shirt $21.68

POSIWID: The Purpose Of A System Is What It Does Shirt $21.68

  1. 4 weeks ago
    Anonymous

    dumb frogpointer

  2. 4 weeks ago
    Anonymous

    "We" don't. They're for people who should be writing Java.

    • 4 weeks ago
      Anonymous

      What the frick is wrong with Java?

    • 4 weeks ago
      Anonymous

      "We" don't.
      >buffer overflows
      They're for...
      >iterator invalidates
      people who should...
      >memory leaks
      be writing Java.
      >segmentation fault (core dumped)

  3. 4 weeks ago
    Anonymous

    They are used by bad developers to manage memory and because normies who dont have a good concept of raii, and stack memory allocators work will create leaks in there code.

    Basically if you know what your doing and a good developer you dont need to worry about them

    • 4 weeks ago
      Anonymous

      But learncpp literally says smart pointers are the embodiment of RAII

    • 4 weeks ago
      Anonymous

      This kind of egotistical thinking causes all sorts of frick ups and is partly the reason for the recent sensation over managed or safe languages. If Pascal or Delphi had won in the 90s we wouldn't be dealing with hatchet wounds peddling their shite and winning. FRICK this Earth.

  4. 4 weeks ago
    Anonymous

    Good reference.

  5. 4 weeks ago
    Anonymous

    It's when you write code that deals with exceptions, don't worry, there is 95% chance it doesn't concern you

  6. 4 weeks ago
    Anonymous

    Because it's a big fat ugly language.

  7. 4 weeks ago
    Anonymous

    Smart pointers, including unique pointers and shared pointers, are a feature introduced in modern C++ to manage memory more safely and efficiently compared to raw pointers.

    Here's a breakdown of each:

    Raw Pointers: In traditional C++, developers often used raw pointers to manage memory allocation manually. While raw pointers provide flexibility, they also come with risks such as memory leaks, dangling pointers, and resource mismanagement. It's easy to forget to deallocate memory, leading to memory leaks. Dangling pointers can occur when a pointer points to memory that has been deallocated, leading to undefined behavior.

    Unique Pointers (std::unique_ptr): Unique pointers are a type of smart pointer that ensures exclusive ownership of the dynamically allocated object. It guarantees that at any point in time, only one unique pointer can point to a particular memory address. When a unique pointer goes out of scope or is explicitly reset, it automatically deallocates the memory it owns. This helps prevent memory leaks and dangling pointers, as the ownership is transferred when the unique pointer is moved or copied.

    Shared Pointers (std::shared_ptr): Shared pointers allow multiple pointers to refer to the same dynamically allocated object. They maintain a reference count internally, incrementing it when a new shared pointer is created and decrementing it when a shared pointer goes out of scope or is reset. The dynamically allocated memory is deallocated only when the last shared pointer pointing to it goes out of scope or is reset, ensuring that the memory is released when it's no longer needed. Shared pointers are particularly useful in scenarios where multiple objects need access to the same resource and the ownership needs to be shared.

    • 4 weeks ago
      Anonymous

      Smart pointers offer several advantages over raw pointers:

      Memory Management: Smart pointers automatically manage memory allocation and deallocation, reducing the risk of memory leaks and dangling pointers.

      Ownership Management: Unique pointers enforce exclusive ownership, while shared pointers allow shared ownership, providing clarity on resource ownership and lifetime.

      Safer Code: Smart pointers help write safer and more maintainable code by reducing the likelihood of common memory-related errors.

      Readability and Expressiveness: Smart pointers make the code more expressive by clearly indicating ownership semantics and reducing the need for manual memory management boilerplate.

      Overall, smart pointers improve code safety, reduce bugs related to memory management, and enhance code readability and maintainability, making them a preferred choice over raw pointers in modern C++ development.

    • 4 weeks ago
      Anonymous

      You are such a loser relying on chatGPT.

      • 4 weeks ago
        Anonymous

        oh no 🙁

    • 4 weeks ago
      Anonymous

      Holy shit, no wonder C++ sucks so much more than C.
      t. C-nile who was wondering about these """"pointers"""" but never cared enough to look into it and now feels vindicated

      Get in your heads, C-suite, you cannot dumb it down anymore, you need to make people smarter, not software more idiot-proof.
      Yes, suck our dicks, there are maybe only 1000 people on the entire planet earth with the skillset you're looking for.

      • 4 weeks ago
        Anonymous

        Yep, this bullshit about safety is an excuse for reta
        Segmentation fault (core dumped)

  8. 4 weeks ago
    Anonymous

    Why do you need pointers when u can just pass by reference?

    • 4 weeks ago
      Anonymous

      Because your mom is too fat to fit in the registers.

      • 4 weeks ago
        Anonymous

        We're not in 1980s anymore. WTF

        • 4 weeks ago
          Anonymous

          True, but your mom ain't what she used to be either.

          Yep, this bullshit about safety is an excuse for reta
          Segmentation fault (core dumped)

          >Segmentation fault (core dumped)
          You seem to suffer from a critical case of having been badly programmed.
          Tip: Consult an experienced programmer who knows what he's doing the next time you work on your code.

          • 4 weeks ago
            Anonymous

            >fifty years of C
            >zero working programs
            "Next time we'll definitely get it right!"

          • 4 weeks ago
            Anonymous

            C++ is a C program
            Rust is a C++ program

          • 4 weeks ago
            Anonymous
  9. 4 weeks ago
    Anonymous

    p = malloc(9000);

    if (!p) {return some kind of error;}

    __try {
    use(p);
    } __finally {
    free(p);
    }

    problem solved no need for shart pointers

    • 4 weeks ago
      Anonymous

      >Microsista
      >unironically uses exceptions in C
      Every time I see you post you get more brain damaged. It's quite amazing.

      • 4 weeks ago
        Anonymous

        >MS solves the problem of requiring a bunch of goto's for cleanup after an error in C
        >Freetards complain
        Kek
        You must be jealous that you can't use this functionality in Linshit OS

        • 4 weeks ago
          Anonymous

          [[cleanup]] does that just fine without introducing le ebin transversal control flow

          • 4 weeks ago
            Anonymous

            >jealous of brain damage so insane that MS has been trying to obviate it for decades
            lol. lmao

            >making up reasons why seh is LE BAD
            mald and seethe

        • 4 weeks ago
          Anonymous

          >jealous of brain damage so insane that MS has been trying to obviate it for decades
          lol. lmao

  10. 4 weeks ago
    Anonymous

    Smart pointers are for stupid people.

  11. 4 weeks ago
    Anonymous

    They're just various attempts to make it easier to reason about resource ownership/lifecycle and therefore hopefully make various types of error (memory leaks, use after free, etc) less likely.
    You don't need to use them in every (or any) project, but they're worth looking into as many people find them helpful.

  12. 4 weeks ago
    Anonymous

    Because Sepplers can't handle manual memory and need garbage collection.

  13. 4 weeks ago
    Anonymous

    They're all cope for the sad fact that C++ can't link with libgc

  14. 4 weeks ago
    Anonymous

    Hey should I make a unique pointer to a normal array? Does that make sense?
    I have a type that can't be copied or moved, and I don't know how many I'll need at compile time. I tried a std::vector and resized it at runtime but apparently resize uses the copy constructor which is fricking stupid.
    Anyway I think I can just have a unique pointer to a normal array then at run time std::make_unique the array. But that feels stupid too.

    Please respond.

    • 4 weeks ago
      Anonymous

      >apparently resize uses the copy constructor which is fricking stupid.
      Well Black person what did you think would happen? It has to move it somewhere if you don't have enough memory left in the current spot.
      >Anyway I think I can just have a unique pointer to a normal array then at run time std::make_unique the array. But that feels stupid too
      I don't understand what you're trying to do.
      I think you're looking for deque.

    • 4 weeks ago
      Anonymous

      Or I didn't understand your problem and you're struggling with just initializing it?
      You should be able to
      std::vector<my_t> my_vec;
      vec.reserve(n);

      and then emplace your stuff. I think.

    • 4 weeks ago
      Anonymous

      Convenience thing so you don’t have to do the ”new… free” dance and write code in two different places every time you allocate something.

      Create a vector of a dummy POD struct of the same size as your actual data type, reinterpret cast to vector of your type and placement new each element. Or just make it moveable like a normal person, whatever smart thing you think you’re doing is probably not that smart

      • 4 weeks ago
        Anonymous

        I don't think he needs to do that, constructing the initial vector with
        std::vector<T> vec(n);

        Should be fine and construct everything in place.
        Obviously std:vector<T> vec(n, m) won't work because it does involve copying/moving.

        Hey should I make a unique pointer to a normal array? Does that make sense?
        I have a type that can't be copied or moved, and I don't know how many I'll need at compile time. I tried a std::vector and resized it at runtime but apparently resize uses the copy constructor which is fricking stupid.
        Anyway I think I can just have a unique pointer to a normal array then at run time std::make_unique the array. But that feels stupid too.

        Please respond.

        If you need to initialize, there is one constructor of std::vector that doesn't do any copy or move, and that's the one with iterators.
        std::vector<ctor_args_t> args = something
        std::vector<my_type> my_vec = {args.begin(), args.end()};

    • 4 weeks ago
      Anonymous

      Make a vector of unique ptr. Then you can add new elements and the new elements will be allocated on the head as needed, never being copied

      • 4 weeks ago
        Anonymous

        Heap*
        By the way, everyone else that has replied to this question has no idea what they're talking about. Just listen to me. You could also use shared ptr but that's for weak homosexuals.
        There's also nothing too wrong with unique ptr to vector or array, but that's a more niche use case you probably don't need. Note that the size of array needs to be known at compile time so it didn't really apply to you, and unique ptr of vector doesn't solve your problem, either.

      • 4 weeks ago
        Anonymous

        >making a vector of unique_ptr pointing all over the place just because you're trying to get not-invalidated ptrs instead of just using a deque
        You're the one who has no idea what they are talking about.

  15. 4 weeks ago
    Anonymous

    We don't need them, we're not Rust developers. We can use them if we need something developed fast and don't care about managing memory the old fashion way.

Your email address will not be published. Required fields are marked *