Live data from Hacker News

Retiring a favourite C++ joke

ignition-training.com

51–60 of 151 posts

Re: Retiring a favourite C++ joke

#51
post #46
post #38

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.

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.

Re: Retiring a favourite C++ joke

#52
post #38

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.

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).

Re: Retiring a favourite C++ joke

#54
post #25

My favorite C++ joke is: Have you heard about the new object-oriented version of COBOL? It’s called “ADD 1 TO COBOL”

That's more like ++C. You need "ADD 1 TO COBOL RETURNING COBOL"... which elicits another C++ joke: that it would have been nice to get to use the better version, but someone used the wrong operator, so what we have is certainly complicated but probably just more of the same.

The ++ is probably overloaded, and actually formats your hard disk, instead of incrementing a number.

(Just like >> is overloaded to do IO, instead of shifting bits.)

Re: Retiring a favourite C++ joke

#56
post #38

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.

I thought the same, modern c++ (>=11) felt like a new start for the language. it's a complete different beast nowdays.

Re: Retiring a favourite C++ joke

#57

My favorite C++ joke is this limerick that has been in the standard for a while: When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation. https://eel.is/c++draft/temp.expl.spec#8

An other one from the standard:

  void trex(volatile short left_arm, volatile short right_arm);   // deprecated
https://eel.is/c++draft/depr.volatile.type#example-3

Re: Retiring a favourite C++ joke

#58

Hey, it actually works! int main() { int tobias[24] = {0,0,33}; delete(tobias+4); } Compile and run on Ubuntu 20.04 (may work on other Linuxes), no errors, no warnings, runs perfectly fine. Bonus fun: try printing out the address of `tobias` and `new int[6]` afterwards :)

I assume you have it on x86_64. I'm trying to make sense of that according to glibc implementation details[1] and Sys V/Itanium ABI.

Default new and delete just use malloc/free.

First 4 ints are interpreted as prev_size and size. prev_size is 0. 33 is 0b10001, size is 32 (bytes, so 8 ints), AMP is 0b001, so not in arena (default sbrk heap, I assume), not mmap'd, prev is used.

I didn't follow how the internal bookkeeping will be updated, but I assume 8 size chunk will be immediately reused on a following `new int[6]`.

Obviously don't write code like this.

[1] https://sourceware.org/glibc/wiki/MallocInternals

Re: Retiring a favourite C++ joke

#60

Hey, it actually works! int main() { int tobias[24] = {0,0,33}; delete(tobias+4); } Compile and run on Ubuntu 20.04 (may work on other Linuxes), no errors, no warnings, runs perfectly fine. Bonus fun: try printing out the address of `tobias` and `new int[6]` afterwards :)

Weird that undefined behaviour is undefined.

Undefined behaviour is no joke.

Post reply on HN