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++)
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.
View all comments
Show parent comment
8
u/WriterPlastic9350 Oct 10 '26 edited Oct 10 '26
double-free vulnerability
C++ actually makes this easier with smart pointers. You ought not to be using
newanddeletein C++ these days. You probably wantstd::shared_ptrorstd::unique_ptrinstead:edit: really, you could get quite far in C++ by just writing C and using smart pointers (and no other feature of C++)