News for hackers. Stuff that matters.
Re:Garbage Collection is Wrong by emeraldd on Thursday February 27, 2014 @04:12PM (#7315097)
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++?

Re:Garbage Collection is Wrong by banachtarski on Thursday February 27, 2014 @04:19PM (#7315157)
Professional C++ coder here.

Seeing new and delete has very heavy code smell and can be avoided 99% of the time.

Re:Garbage Collection is Wrong by mcphage on Thursday February 27, 2014 @05:39PM (#7315752)
What have you done to your language that the native syntax for creating and deleting objects is a code smell?!
Re:Garbage Collection is Wrong by kd0amg on Thursday February 27, 2014 @07:48PM (#7316525)
Included an alternative to new/delete's form of allocation that covers a good range of its use cases and is less prone to screwup.
Re:Garbage Collection is Wrong by AnimalMuppet on Thursday February 27, 2014 @05:54PM (#7315849)
Recognized that the average programmer can't reliably handle the tool?
Re:Garbage Collection is Wrong by banachtarski on Saturday March 01, 2014 @01:59AM (#7323460)
They are the primitive functions for making and destroying objects on the heap which requires more care than construction and destruction on the stack.
Re:Garbage Collection is Wrong by pcwalton on Thursday February 27, 2014 @04:12PM (#7315099)
In 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.

Re:Garbage Collection is Wrong by to3m on Thursday February 27, 2014 @04:46PM (#7315350)
You 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.)

Re:Garbage Collection is Wrong by letzjuc on Thursday February 27, 2014 @05:29PM (#7315697)
Why 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.

Re:Garbage Collection is Wrong by rian on Thursday February 27, 2014 @04:21PM (#7315169)
Naked New/delete have been taboo since auto_ptr hit the scene.
Re:Garbage Collection is Wrong by emeraldd on Thursday February 27, 2014 @06:19PM (#7316000)
Interesting, I hadn't seen that before. I guess I really need to go back and relearn C++ ...
Re:Garbage Collection is Wrong by naiyt on Thursday February 27, 2014 @04:19PM (#7315159)
I 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.
Re:Garbage Collection is Wrong by epx on Thursday February 27, 2014 @04:14PM (#7315116)
It depends a lot on the framework, but you can avoid almost 100% of new and delete on code you have control.