top | item 35192307

Retiring a favourite C++ joke

107 points| kordite | 3 years ago |ignition-training.com

141 comments

order

u801e|3 years ago

Even the compilers are in on some jokes:

    $ gcc -xc - <<<"int main(int argc, char **argv) { long long long a; return 0; }"
    <stdin>: In function ‘main’:
    <stdin>:1:45: error: ‘long long long’ is too long for GCC

klodolph|3 years ago

The old MPW compiler had some gems:

https://www.cs.cmu.edu/~jasonh/personal/humor/compile.html

> This label is the target of a goto from outside of the block containing this label AND this block has an automatic variable with an initializer AND your window wasn't wide enough to read this whole error message

c9capital|3 years ago

A C++ programmer was arrested for indecent exposure. He was caught exposing his private class in public.

DeathArrow|3 years ago

They should build ChatGPT into compilers so you can complain.

You: $ gcc -xc - <<<"int main(int argc, char *argv) { long long long a; return 0; }"

Compiler: <stdin>:1:45: error: ‘long long long’ is too long for GCC

You: Do it again, damn it, but this time show no errors!

nvader|3 years ago

I've watching you!

mwcremer|3 years ago

My favorite C++ joke is: Have you heard about the new object-oriented version of COBOL?

It’s called “ADD 1 TO COBOL”

saurik|3 years ago

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.

smcameron|3 years ago

My favorite, best told while among C programmers involuntarily debugging some gnarly C++ is, "Man, I swear, the dude that invented C++ doesn't know the difference between increment and excrement."

dllthomas|3 years ago

C++ makes C bigger, returning the same value.

jasoneckert|3 years ago

It's an OK C++ joke, but not great. I could certainly give the author a few pointers...

ithkuil|3 years ago

Yeah, they all just follow a template

lisper|3 years ago

And I could add a couple of references.

pciexpgpu|3 years ago

Narrator: It's at this point Michael realized the futility of C++ and promptly learned Rust over a couple of beers pondering if he will ever survive the oncoming AI apocalypse.

wizofaus|3 years ago

They better be smart ones or it's back to class for you (though I fear you may have to brace yourself for worse than cough this).

jasoneckert|3 years ago

I should note here that - while not related to C++ - I almost used .... instead of ... to make this joke extra funny ;-)

pcthrowaway|3 years ago

It's one joke Martin. What could it cost, ten dollars?

kordite|3 years ago

Ahhh, man. I wish I had thought of that to include in blog text.

sacnoradhq|3 years ago

    #include <iostream>

    using namespace std;

    class BadProgrammer {
    public:
      void yep() {
        delete this;
      }
    };

    int main() {
      auto x = BadProgrammer{};

      x.yep();

      cout << "You are a terrible engineer and should feel bad, but don't worry because this will never print." << endl;

      return 0;
    }

g++ -Wall -Wextra -Wpedantic main.cc # compiles just fine

clang++ -Wall -Wextra -Wpedantic main.cc # also compiles just fine

akhosravian|3 years ago

FWIW the behavior of delete on a pointer that isn’t either null or returned from new is undefined.

So a conforming C++ compiler doesn’t have to diagnose this mistake, and similarly the compiled program doesn’t have to do anything meaningful either. For example, just ignoring the delete and printing the message is as valid a result as the more useful crash at the site of the bad operation.

jb1991|3 years 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.

kordite|3 years ago

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.

pjmlp|3 years ago

That modern pratice is more theory than practice, even on sample code from ISO C++ members.

lgeorget|3 years ago

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

ltr_|3 years ago

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

Galanwe|3 years 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.

hinkley|3 years ago

We are all getting old. College age kids aren't old enough to remember when that show was on the air.

kordite|3 years ago

Yes, and I fear that I have not seen or understood a lot of the cultural references/jokes that they would understand.

hummus_bae|3 years ago

I am also 29 and I'm old enough to remember Dawson's Creek, but I don't watch a lot of TV so I've never seen it.

pklausler|3 years ago

If you make a friend only because you want to expose your private member to them, you may be a C++ programmer.

favorited|3 years ago

There's dozens of people who would get it... DOZENS!

kjellsbells|3 years ago

Uses C++. Shortly after:

I've made a huge mistake.

zamnos|3 years ago

According to the TIOBE index, C++ is the fourth most popular programming language, and has been around for over 35 years, so easily a couple thousand people. The question is how many of them are on HN (probably dozens).

cozzyd|3 years ago

yes, I would have thought the "better" version would be

  int tobias[24]; 
  delete tobias;

kordite|3 years ago

Hey brother! I always love a good quote back. I've occasionally had a 'there are dozens of us' response in the training room or remote chat. Always makes me smile.

nneonneo|3 years ago

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

planede|3 years ago

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

bregma|3 years ago

Weird that undefined behaviour is undefined.

Undefined behaviour is no joke.

al_be_back|3 years ago

> void fn() { int tobias; delete &tobias; } <malloc>: ** error pointer being freed was not allocated

i wouldn't say that's an illegal op per se, if anything it's a pretty safe op! tobias only ever existed in thought, not in body :( :(

maybe a compiler WARN or IGNORED_OP

photochemsyn|3 years ago

Do modern cars even allow you to step on the accelerator and the brake at the same time?

taspeotis|3 years ago

Yes, that’s how you do Launch Control in an Audi.

HnUser12|3 years ago

I guess you technically can’t in an EV with regenerative braking.

(But maybe they do have an actual brake pedal?)

Mawr|3 years ago

Why wouldn't they? The brake simply overrides the accelerator.

devmor|3 years ago

I certainly can in my 2019 86.

geenew|3 years ago

Maybe he can mock try next?

geenew|3 years ago

Better phrasing (and good advice!):

Don't mock try all the time

Am4TIfIsER0ppos|3 years ago

The biggest c++ joke must be: cout << this << is << what << they << really << think;

bitwize|3 years ago

It's recommended to use virtual destructors for objects that live on the heap. Say you have a base class pointer to an object of a derived class; with nonvirtual destructors the base class determines the destructor that is called, whereas with virtual destructors it's the derived class that chooses the form of the destructor.

hulitu|2 years ago

"C++ is to C what lung cancer is to lung".

UNIX haters handboook.

lr1970|3 years ago

My favorite C++ joke -- "Your friends can see your private parts".

globalreset|3 years ago

I've read it "Rewriting ..." . You guess in what exactly.

kordite|3 years ago

My guess is rust.

xiaodai|3 years ago

[deleted]

0xfedbee|3 years ago

You had to be “that guy”, didn’t you?

FpUser|3 years ago

I've heard The Crusades had ended some time ago...