Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

do you NEVER allow dynamically allocated memory? or am I misunderstanding you?


There 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.


In 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.


Please 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.


I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: