4 ms·
do you NEVER allow dynamically allocated memory? or am I misunderstanding you?
by TheSoftwareGuy 12y ago
do you NEVER allow dynamically allocated memory? or am I misunderstanding you?
- Nursie 12y agoIn modern c++ you can get away without it by using a combination of RAII and the various shared pointer and container features. It's an odd mental leap for this old C hack, but it works quite well when you get into it. Bonus - no 'free' or 'delete' necessary.
- TheSoftwareGuy 12y agoPlease correct me if I am wrong, but RAII isn't flexible enough to allow for lazy initialization. the various STL pointer types (std::shared_ptr etc..) are great, but they do incur overhead, and sometimes that is not acceptable in e.g. embedded systems.
- cottonseed 12y agoI think he means it should read something like: auto x = std::make_unique<Foo>(); Unless you're implementing an allocation policy, you shouldn't be calling new and delete directly.
- EpicEng 12y agoThere are very few instances in which manually managing memory in C++ is justified. When you have `unique_ptr`, `shared_ptr`, and all sorts of container classes, it simply doesn't come up in most cases. If you see operator new being used you should be suspicious.