3 ms·
In modern C++, using new or delete in your code is wrong. It's not done. Nobody writes code like that anymore in C++. It's been quite a while since I wrote any
by emeraldd 13y ago
In modern C++, using new or delete in your code is wrong. It's not done. Nobody writes code like that anymore in C++.
It's been quite a while since I wrote any significant C++ code, but this statement seems wrong. Is this really "state of the art" for C++?
- pcwalton 13y agoIn practice, no. People still write new and delete all the time in large C++ codebases (LLVM, Gecko, WebKit, etc. etc.) unique_ptr is gaining traction, of course.
- epx 13y agoIt depends a lot on the framework, but you can avoid almost 100% of new and delete on code you have control.
- banachtarski 13y agoProfessional C++ coder here. Seeing new and delete has very heavy code smell and can be avoided 99% of the time.
- mcphage 13y agoWhat have you done to your language that the native syntax for creating and deleting objects is a code smell?!
- AnimalMuppet 13y agoRecognized that the average programmer can't reliably handle the tool?
- kd0amg 13y agoIncluded an alternative to new/delete's form of allocation that covers a good range of its use cases and is less prone to screwup.
- banachtarski 13y agoThey are the primitive functions for making and destroying objects on the heap which requires more care than construction and destruction on the stack.
- naiyt 13y agoI don't develop C++ professionally or anything, but it's my understanding that a lot of people agree that unique_ptr's and shared_ptr's are the way to go these days.
- rian 13y agoNaked New/delete have been taboo since auto_ptr hit the scene.
- emeraldd 13y agoInteresting, I hadn't seen that before. I guess I really need to go back and relearn C++ ...
- to3m 13y agoYou can get surprisingly far without using new or delete. std::vector, std::list and std::string are good replacements for many uses, you can of course create objects on the stack, and if your objects are stateless then they can maybe be const globals - and that can often save you a big pile of bother. Though while new and delete are best avoided where possible, it's hardly the end of the world if you do use them. And my experience has always been that you're better off just using them, if the alternative is heavy use of smart pointers. As member variables, smart pointers are tolerable, if copied to an ordinary pointer before heavy use; as locals or parameters, they're almost always best avoided. Over the years I've have had far more annoyance from painful single stepping, spitefully poor unoptimised performance and inconvenient watch window behaviour than I have ever had from just picking out all the memory leaks after the fact. (Which, if the code is written relatively tastefully, is usually pretty straightforward. And unless your colleagues are actively being dicks, I've always found there aren't too many to get rid of anyway.)
- letzjuc 13y agoWhy use new/delete when you have make_shared and make_unique? Like 99.99999999% of the time you can use those. Like the only situation where I think they might be not enough is for some weird uses of placement new.