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.
Retiring a favourite C++ joke
51–60 of 151 posts
Re: Retiring a favourite C++ joke
#52I 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.
Re: Retiring a favourite C++ joke
#53Re: Retiring a favourite C++ joke
#54My 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.
(Just like >> is overloaded to do IO, instead of shifting bits.)
Re: Retiring a favourite C++ joke
#55It's an OK C++ joke, but not great. I could certainly give the author a few pointers...
Re: Retiring a favourite C++ joke
#56I 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.
Re: Retiring a favourite C++ joke
#57My 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
void trex(volatile short left_arm, volatile short right_arm); // deprecated
https://eel.is/c++draft/depr.volatile.type#example-3Re: Retiring a favourite C++ joke
#58Hey, 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 :)
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.
Re: Retiring a favourite C++ joke
#59Re: Retiring a favourite C++ joke
#60Hey, 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 :)
Undefined behaviour is no joke.