6 ms·
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
by jb1991 4y 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.
- pjmlp 4y agoThat modern pratice is more theory than practice, even on sample code from ISO C++ members.
- jb1991 4y agoNo, 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.
- pjmlp 4y agoI 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.
- jb1991 4y agoIf 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.
- pjmlp 4y agoA niche in the C++ world.
- kps 4y agoHmm, 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%20case:yes%20-file:third_party%2F%20-file:%2Fgen%2F%20-file:test.*%20-comment:new&sq=&ss=chromium%2Fchromium%2Fsrc https://source.chromium.org/search?q=\bnew\b%20lang:c%2B%2B%... https://source.chromium.org/search?q=\bmake_unique\b%20lang:c%2B%2B%20case:yes%20-file:third_party%2F%20-file:%2Fgen%2F%20-file:test.*%20-comment:new&sq=&ss=chromium%2Fchromium%2Fsrc https://source.chromium.org/search?q=\bmake_unique\b%20lang:...
- FpUser 4y agoI 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.
- lgeorget 4y agoIt'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).
- ltr_ 4y agoI thought the same, modern c++ (>=11) felt like a new start for the language. it's a complete different beast nowdays.
- kordite 4y agoI 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.
- jb1991 4y agoThat's perfectly reasonable. I have fortunately worked on greenfield projects, so don't have to contend with legacy paradigms.
- salawat 4y agoAh, 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?
- jb1991 4y ago> Ah, so you've been doing programming-lite. I hope you can understand why that's offensive.
- kordite 4y agoAbsolutely. 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.
- Galanwe 4y ago> 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.
- unnah 4y agoYou 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.
- halayli 4y agoWhy 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.
- Joker_vD 4y agoAh, so reference counting is not considered a form of garbage collection any more? Ingenious but somewhat of dishonest.
- spacechild1 4y agoSmart 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.
- kps 4y agostd::unique_ptr is reference counted, it's just that the count only goes up to BOOL_MAX instead of LONG_MAX.
- jb1991 4y agoI 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.)