Bjarne Stroustrup: How do I deal with memory leaks? By writing code that doesn't have any.
(www.stroustrup.com)
from sanitation@lemmy.radio to programming@programming.dev on 09 May 20:18
https://lemmy.radio/post/13235812
from sanitation@lemmy.radio to programming@programming.dev on 09 May 20:18
https://lemmy.radio/post/13235812
aka “bros: in order to be rich, just don’t be poor.”
#programming
threaded - newest
lol “jUst wRiTe bETteR cOde”
<img alt="" src="https://lemmy.world/pictrs/image/11842987-3e8a-4b1b-ba83-afda3d572710.jpeg">
we’re all freestyling assembler with ai
catch up luddites
A reminder that Bjarne doesn’t actually code. He is a bureaucrat.
Perfect username.
all these newfangled languages with their “memory safety” and “helpful tooling”, pah. all a real programmer needs is a hole punch and a roll of paper.
No, this is kinda right. Memory leaks =/= memory safety. Memory leaks are just when you keep allocating more and more memory, and can be done in any language. If I make accidentally make a list that infinitely grows in python, that’s a memory leak.
There are techniques to write code that is mostly free of leaks, which is what he is referring to.
Memory safety, on the other hand, doesn’t seem to be mentioned on that page…
A “true” memory leak isn’t just memory that is never freed, it’s memory that CAN’T be freed. Usually because the handle / pointer to it was thrown away.
The distinction isn’t really all the important though, it all looks the same as far as how the program functions.
The memory leaks section just feels like an introduction to smart pointers as though they’re some complex concept. Also, the page is showing its age by mentioning the now-removed
auto_ptrinstead of something likeunique_ptr.Anyway, scrolling down a little more:
This actually comes up in C# with arrays. Copying their example here:
It may have been a design mistake not to make C#'s arrays invariant, though I don’t know the state of that debate today.
Invariant?
Neither covariant nor contravariant.
supertype[] is not a supertype of subtype[] if supertype and subtype alone are in that relationship, because the mutability of arrays means that the Liskov substitution principle doesn’t hold.
(These are all something you’ll probably find good explanations of on Wikipedia.)
Thank you for your reply. Sadly I don’t know terms like covariant or supertype either 😅 so I’ll have to look into all that when/if it comes up. I write in GDScript for fun (a game engine’s high-level scripting language said to have syntax similar to Python).
To be clear, he’s not saying “just code better”, as the title implies. It’s a really poor choice of title.
He goes on to say, effectively, “Don’t use new/delete/malloc/free if you can help it. Use smarter mechanisms.”