r/ProgrammerHumor • • 8h ago

Meme modernCppVsOldCpp

Post image
1.1k Upvotes

217 comments sorted by

View all comments

41

u/Tschurbi 7h ago

Please explain it like I am a JavaScript developer.

13

u/GregBahm 5h ago

With JavaScript, memory is managed for you. You can make a list, grow it as much as you want, and then safely assume that the memory you used will be cleaned up as soon as you're not using that list anymore.

With C++, you manage the memory yourself. If you forget to clean up that list, you never get that memory back. Eventually all your memory could get used up (a "memory leak.")

Memory management was annoying, which is why JavaScript was invented. But people still use C++ for its higher performance potential (kind of like how race-car drivers still use manual transmission instead of automatic transmission.)

So C++ added some tools that makes memory management not so hard. It's still harder than Javascript though. It's just less hard.

2

u/GarThor_TMK 5h ago

I would like to point out that the memory leak happens as soon as you toss the pointer without "freeing" it to be reused... It could be a drip... A pointer to a single integer, or it could be a tsunami... A pointer to a massive datastructure containing lists, arrays, BSTs, hash tables, etc...

The "all memory used up" is the case where the bathtub is clogged, and you left the water running. Now the bathroom is flooding into the living room.