frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Bjarne Stroustrup: How do I deal with memory leaks? (2022)

https://www.stroustrup.com/bs_faq2.html#memory-leaks
32•theanonymousone•1h ago

Comments

adampunk•57m ago
Remarkably similar to dykstra on debugging.
wxw•45m ago
> As early as 1981, I pointed out that by reducing the number of objects that I had to keep track of explicitly from many tens of thousands to a few dozens, I had reduced the intellectual effort needed to get the program right from a Herculean task to something manageable, or even easy.

Fast forward a few decades, and we're still very much on this journey of finding the right abstractions/interfaces/libraries/languages. I feel like there must be a complexity equivalent to Parkinson's law: complexity expands to fill the space left in between abstractions.

Panzerschrek•44m ago
Good arguments. But for some reason there are still strange people, who prefer calling malloc/free (or new/delete or something similar) manually. Some of them even invent languages with manual-only memory management (like Zig or Odin).
cv5005•36m ago
One reason is that c++ still hasn't gotten 'trivial relocatability' right - i.e being able to memcpy/memmove and not have to call constructors/destructors when growing your vector class.
Panzerschrek•19m ago
C++ compilers optimize-out empty destructor calls and sometimes even replace calls to move constructors/move assignment operators via memcpy. But it's unfortunately not guaranteed in all cases due to constrains of the C++ object/memory model designed initially without proper move semantics.
Panzerschrek•14m ago
Actually, issues with non-trivial moves and relocations are specific only for C++. Some other languages (notably Rust and Swift) don't have such issues, but still have nice automatic memory management via destructors and containers atop of it.
eska•35m ago
No need to insult people just because you don’t understand other strategies for reducing the amount of lifetimes to track and consolidating deallocations by using memory arenas.
Panzerschrek•22m ago
You can use some arena implementation in C++ too. But only when you need such an approach. If you don't care - just use std::string, std::vector or something similar.
eska•17m ago
The C++ standard library interface is broken regarding its abstraction of allocation (according to its authors). Therefore you in fact can’t just use arenas in C++ without giving up on large parts of its standard library and becoming incompatible with other code. The languages whose users you call strange don’t have this issue.
tialaramex•14m ago
I'm no fan of Odin especially, but I'd expect that one obvious defence they'd offer is that this code potentially wastes a lot of resources and if you were writing in their language you'd more likely go "Wait, that seems like a bad idea..." and produce better code.

    cat += *p+"+";
Feels very cheap because it was so few keystrokes, but what it's actually doing is:

1. Making a brand new std::string with the same text inside it as `p` but one longer so as to contain an extra plus symbol. Let's call this temporary string `tmp`

2. Paste that whole string on the end of the string named `cat`

3. Destroy `tmp` freeing the associated allocation if there is one

Now, C++ isn't a complete trash fire, the `cat` std::string is† an amortized constant time allocated growable array under the hood. Not to the same extent as Rust's String (which literally is Vec<u8> inside) but morally that's what is going on, so the appends to `cat` aren't a big performance disaster. But we are making a new string, which means potentially allocating, each time around the loop, and that's the exact sort of costly perf leak that a Zig or Odin programmer would notice here.

† All modern C++ std::string implementations use a crap short string optimisation, the most important thing this is doing - which is the big win for C++ is they can store their empty value, a single zero byte, without allocating but they can all store a few bytes of actual text before allocating. This might matter for your input strings if they are fairly short like "Bjarne" "Stroustrup" and "Fool" but it can't do "Disestablishmentarianism".

Panzerschrek•6m ago
I agree, having nice containers with automatic memory management allows such problems. But this code still works as intended, but it has suboptimal performance. But I think, that it's still better to use an approach allowing such performance issues, rather then bugs specific for manual memory management (memory leaks, use-after-free errors, spatial access errors).

And it's still possible to improve performance here without returning to manual memory management. Just replace it with something like this:

    cat += *p;
    cat += "+";
Now no temporary string is created and thrown away, only cat performs memory allocations under the hood.
tialaramex•35m ago
> Modified February 26, 2022

So while a much older date is probably appropriate, maybe 20-30 years ago, we can at least mark this (2022) until somebody justifies a particular previous date.

theanonymousone•30m ago
Thanks. I modified the title.
eska•13m ago
Strange to see std::sort(), auto_ptr and RAII on the same page, when that combination was always broken.
tialaramex•4m ago
I actually don't know (at least I don't think I do) the full story here, can you elaborate?

Google Cloud Fraud Defence is just WEI repackaged

https://privatecaptcha.com/blog/google-cloud-fraud-defence-wei/
429•ribtoks•5h ago•206 comments

Cartoon Network Flash Games

https://www.webdesignmuseum.org/flash-game-exhibitions/cartoon-network-flash-games
140•willmeyers•2h ago•48 comments

AI Is Breaking Two Vulnerability Cultures

https://www.jefftk.com/p/ai-is-breaking-two-vulnerability-cultures
31•speckx•1h ago•1 comments

Serving a website on a Raspberry Pi Zero running in RAM

https://btxx.org/posts/memory/
130•xngbuilds•3h ago•50 comments

An Introduction to Meshtastic

https://meshtastic.org/docs/introduction/
283•ColinWright•7h ago•111 comments

A web page that shows you everything the browser told it without asking

https://sinceyouarrived.world/taken
340•mwheelz•6h ago•175 comments

PC Engine CPU

https://jsgroth.dev/blog/posts/pc-engine-cpu/
88•ibobev•4h ago•32 comments

Bjarne Stroustrup: How do I deal with memory leaks? (2022)

https://www.stroustrup.com/bs_faq2.html#memory-leaks
34•theanonymousone•1h ago•15 comments

Apple, Intel have reached preliminary chip-making deal

https://www.reuters.com/business/apple-intel-have-reached-preliminary-chip-making-deal-wsj-report...
85•scrlk•1h ago•35 comments

Poland is now among the 20 largest economies

https://apnews.com/article/poland-economy-growth-g20-gdp-26fe06e120398410f8d773ba5661e7aa
743•surprisetalk•6h ago•642 comments

Rumors of my death are slightly exaggerated

1138•CliffStoll•2d ago•163 comments

Mojo 1.0 Beta

https://mojolang.org/
175•sbt567•16h ago•126 comments

Cloudflare to cut about 20% of its workforce

https://www.reuters.com/business/world-at-work/cloudflare-cut-over-1100-jobs-2026-05-07/
1194•PriorityLeft•22h ago•829 comments

Show HN: Git for AI Agents

https://github.com/regent-vcs/re_gent
64•doshay•4h ago•38 comments

David Attenborough's 100th Birthday

https://www.bbc.com/news/articles/cp3pww9g0p5o
114•defrost•7h ago•12 comments

Canvas online again as ShinyHunters threatens to leak schools’ data

https://www.theverge.com/tech/926458/canvas-shinyhunters-breach
876•stefanpie•20h ago•582 comments

US Government releases first batch of UAP documents and videos

https://www.war.gov/UFO/
139•david-gpu•6h ago•239 comments

Maybe you shouldn't install new software for a bit

https://xeiaso.net/blog/2026/abstain-from-install/
770•psxuaw•20h ago•407 comments

Podman rootless containers and the Copy Fail exploit

https://garrido.io/notes/podman-rootless-containers-copy-fail/
83•ggpsv•5h ago•14 comments

GeoJSON

https://geojson.org/
123•tosh•9h ago•61 comments

Ask HN: We just had an actual UUID v4 collision...

174•mittermayr•11h ago•171 comments

Dirtyfrag: Universal Linux LPE

https://www.openwall.com/lists/oss-security/2026/05/07/8
758•flipped•23h ago•305 comments

Google Broke reCAPTCHA for De-Googled Android Users

https://reclaimthenet.org/google-broke-recaptcha-for-de-googled-android-users
9•anonymousiam•17m ago•0 comments

ClojureScript Gets Async/Await

https://clojurescript.org/news/2026-05-07-release
248•Borkdude•11h ago•59 comments

Defeating Works by Design's Unpickable Lock [video]

https://www.youtube.com/watch?v=rMi1dIqMwNw
10•zdw•2d ago•1 comments

The surprisingly complex journey to text-selectable client-side generated PDFs

https://sdocs.dev/blogs/journey-to-pdf-generation
60•FailMore•1d ago•51 comments

The map that keeps Burning Man honest

https://www.not-ship.com/burning-man-moop/
741•speckx•1d ago•341 comments

The Disappearance of the Public Bench

https://placesjournal.org/article/the-disappearance-of-the-public-bench/
105•cainxinth•1d ago•122 comments

Pinocchio is weirder than you remembered

https://storica.club/blog/pinocchio-in-italian/
273•cemsakarya•2d ago•111 comments

Inventing Cyrillic (2024)

https://www.historytoday.com/archive/history-matters/inventing-cyrillic
38•lermontov•2d ago•79 comments