3 ms·
Educate me, pkease: why no new or delete? Does C++ now discourage heap allocation or is the common practice to use smart pointers, etc.?
by memling 4y ago
Educate me, pkease: why no new or delete? Does C++ now discourage heap allocation or is the common practice to use smart pointers, etc.?
- thrwyoilarticle 4y agoConstructing into a unique_ptr[1] will mean you don't have to risk forgetting to delete it or risk managing something you don't own. You'll also sometimes get compiler spew that you'll learn is telling you you're trying to do something nonsensical e.g. use its copy ctor. In contrast, if you try to copy a raw pointer that is intended to be unique then you get a copy of a pointer and a Heisenbug. Also a lot can be done with things that are on the heap but act as local variables. [1] You probably don't actually need a shared_ptr and if you do then it won't stop you from creating memory leaks.
- underdeserver 4y agoYep. In new code you should use smart pointers for allocation as much as you can.