r/ProgrammerHumor • • Oct 10 '26

Meme theyAreLovingIt

Post image
2.1k Upvotes

83 comments sorted by

View all comments

Show parent comment

8

u/WriterPlastic9350 Oct 10 '26 edited Oct 10 '26

double pointer deletion

double-free vulnerability

and everything C++ makes difficult.

C++ actually makes this easier with smart pointers. You ought not to be using new and delete in C++ these days. You probably want std::shared_ptr or std::unique_ptr instead:

std::unique_ptr<T> t = std::make_unique<T>(...);

edit: really, you could get quite far in C++ by just writing C and using smart pointers (and no other feature of C++)

1

u/conundorum Oct 11 '26

As of... C++17, I believe, unique_ptr can actually handle new just fine now, and (IIRC) it turns out that there are a few cases where this is sometimes better than make_unique():

std::unique_ptr<T> t(new T(...));

You still don't need to worry about the delete because of the smart pointer, and I remember reading that there are a couple uncommon quirks that can sometimes get sidestepped this way.