What are smart pointers and when should they be used?
Hello colleagues! I haven't been writing in C++ for very long, but I've already encountered the problem of memory leaks. I'm still using regular raw pointers with `new` and `delete`, but I constantly forget to call `delete`, especially when the code gets cluttered with exceptions and early returns. For example:\n\n```cpp\nvoid process() {\n Resource* res = new Resource();\n // ... some code that may throw an exception ...\n delete res; // if there was an exception, we won't reach here — leak!\n}\n```\n\nI've heard about "smart pointers" in C++11, but I don't fully understand what they are. There's a lot of information on the internet, but I'd like a clear explanation: what kind of beasts are they, how do they differ from regular pointers, and in what situations should they actually be used? I'm especially interested in `std::unique_ptr`, `std::s
No answers yet.
Log in to leave an answer