Hacker News new | ask | show | jobs
by jb1991 1193 days ago
I was surprised to see it not mentioned in the post -- the most obvious reason this joke should be retired is not the reference to a television show but rather the modern practice of never using new or delete at all in C++ code. Modern C++ avoids these pitfalls and I think it's been 10 years since I've written the words the new or delete.
5 comments

I frequently work with companies that have old, large codebases. A key requirement of the training in that environment is that attendees are able to read existing production code. In some cases that's over twenty years old and millions of lines of code. new/delete is just part of that reality. We teach them without encouraging their use.
That's perfectly reasonable. I have fortunately worked on greenfield projects, so don't have to contend with legacy paradigms.
Ah, so you've been doing programming-lite.

You haven't programmed til you've come to the realization that billions of dollars worth of man hours are about to go down the toilet because of an assumption made 20 years ago, in a 4th order dependency, that you can't get rid of, and to rewrite is going to cost another couple million dollars, and spent a night with a bottle of whiskey wondering whether it's really all worth it.

Where my maintainers/InfoSec bros at?

> Ah, so you've been doing programming-lite.

I hope you can understand why that's offensive.

Absolutely. I would also not use new/delete in that situation. It would depend on the audience whether they were still taught on a course, sometimes there is, and sometimes there isn't an educational purpose.
That modern pratice is more theory than practice, even on sample code from ISO C++ members.
No, it’s not just theory. Like anything in C++, codebases and their styles widely vary. But my comment holds true for the vast majority of developers I have worked with over the last decade. With the exception of the special “placement new“ operator, it is nearly obsolete to use new and delete.
I suggest browsing around code from Microsoft, Google, Apple,...

Plus there are those tons of enteprise code, hardly the "vast majority".

The only place I see modern C++ as advocated, it at C++ conference talks, and my own hobby coding.

Hmm, I was at G until the recent decimation, and would have been surprised to see `new`, especially in new (heh) code, because it immediately raises the question of ownership, which means it costs time for every future reader.

Taking chromium as a proxy (though the style isn't identical), there are currently 5 or 6 times as many `make_unique` as `new`. (Some false positives on the word ‘new’ in strings, because I don't remember how to exclude them.)

https://source.chromium.org/search?q=\bnew\b%20lang:c%2B%2B%...

https://source.chromium.org/search?q=\bmake_unique\b%20lang:...

Now search for C arrays and strings, old style enums, str...() and mem...() functions, C style casts, preprocessor macros,..
That’s called whataboutism.
If you are going to work with a legacy codebase, then sure. My comment was more about fresh codebases, which are fortunately the only ones I've worked on.
A niche in the C++ world.
I would argue it is not for me at least. In my 2 current C++ commercial products of fair size one has single explicit allocation, the other does not have any.

If I was writing for example libraries of custom high performance containers the situation would have been different but still highly localized. And it is sort of very specialized type of development anyways.

It's still useful to teach them in my experience. It's hard to introduce smart pointers without explaining the problem they solve (i.e. wrong or missing uses of delete).
I thought the same, modern c++ (>=11) felt like a new start for the language. it's a complete different beast nowdays.
> the modern practice of never using new or delete at all in C++ code

What? How do you (de)allocate memory without new and delete?

If the answer is "smart pointers" then why bother use C++ in the first place, just use Go or something.

Go is a garbage collector. Smart pointers are not.

It's fairly simple:

instead of new --> make_unique(); this returns a pointer, but it will automatically delete its memory when it goes out of scope.

instead of delete --> do nothing; the memory is deleted when you don't need it any more.

There are additional API methods on the unique pointer that allow you to do other things for flexibility, but while continuing that memory safety.

This is nothing at all like a GC language like Go.

You allocate either on the stack, or on the heap with make_unique and make_shared. Deletion occurs automatically once the object is out of scope.
Why would you respond to a comment about a topic you don't understand and spread false information? You clearly cannot tell the difference between garbage collected language and smart pointers.
Ah, so reference counting is not considered a form of garbage collection any more? Ingenious but somewhat of dishonest.
Smart pointers do not necessarily use reference counting. std::shared_ptr is reference counted, but std::unique_ptr is not. Needless to say that std::unique_ptr should always be the default choice and std::shared_ptr only used when actually needed.
std::unique_ptr is reference counted, it's just that the count only goes up to BOOL_MAX instead of LONG_MAX.
I think you’re making a joke, but reference counting is different than what a unique pointer does. (a destructor is not the same thing as a reference count, even if that reference count is limited as you suggest.)
like the other comment said, std::unique_ptr is not ref counted. Ref counting is one of the ways to implement garbage collection but it is not garbage collection by itself. C++ does not have garbage collection.
C++ does not have garbage collection in the standard library, just as shared_ptr was not part of the standard library in the past. You can currently have optional garbage collection in C++: https://github.com/pebal/sgcl
C++ can do pretty much anything, but that doesn't mean that it’s typical to do so. In practice, you will never see a garbage collector in C++.