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

holy mother of god! I used C++ around 2000 for some old projects. When new and delete where the object oriented "equivalent" for malloc() and free(). So, if you don't use that, what do you use in 2022 C++?


`std::make_unique()`, `std::make_shared()`, or another function that wraps `new` and `delete` into an RAII type (commonly called a smart pointer) so that you, the developer, worry less about explicit memory management.

And the standard smart pointers are perfectly extensible enough to wrap things allocated from C libraries (I like to pick on opengl's glalloc() and glfree(), though malloc() and free() are acceptable to pick on too), C-style `FILE` pointers, or even memory-mapped things from `mmap()`

I would also point out that even in 2000, `std::auto_ptr` existed. So even in 2000 you probably should not have been using `new` and `delete`.


std::auto_ptr was broken and didn't work with STL. It wasn't until C++11 introduced "move" semantics that you could really use smart pointers.

You can't really blame him for not using a part of the language that was unusable, right?


By the GP's opinion, you use the stl classes that implement RAII for you.

What is, obviously, only one way to do it, with its up and downsides. Granted that the upsides are much more numerous than the downsides, but there is a reason it's not the only option available.




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

Search: