frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Understanding C++ Ownership System

https://blog.aiono.dev/posts/understanding-c++-ownership-system.html
23•todsacerdoti•1h ago

Comments

Dwedit•52m ago
C++: Where you can accidentally deallocate an object that's still in the call stack. (true story)
einpoklum•35m ago
Well, you can also write:

   int x = 123;
   delete &x;
and that would compile. But it's not a very good idea and you should be able to, well, not do that.

In modern C++, we avoid allocating and deallocating ourselves, as much as possible. But of course, if you jump to arbitrary code, or overwrite something that's due as input for deallocation with the wrong address, or similar shenanigans, then - it could happen.

kccqzy•28m ago
You can also do that intentionally and correctly. After all `delete this;` is a valid statement that can occasionally be useful. That said, I’ve only seen this in old pre-C++11 code that does not adhere to the RAII best practice.
HarHarVeryFunny•3m ago
The trouble with C++ is that it maintains backwards compatibility with C, so every error-prone thing you could do in C, you can still do in C++, even though C++ may have a better way.

The modern, safest, way to use C++, is to use smart pointers rather than raw pointers, which guarantee that nothing gets deleted until there are no more references to it, and that at that point it will get deleted.

Of course raw pointers and new/delete, even malloc/free, all have their uses, and without these low level facilities you wouldn't be able to create better alternatives like smart pointers, but use these at your own peril, and don't blame the language if you mess up, when you could have just done it the safe way!

jesse__•47m ago
Does anyone reading this have links to people who have written specifically about a C++ ownership model that rejects the smart_ptr/RAII/friends model in favor of bulk allocations, arenas, freelists, etc? I know there are groups of highly productive programmers that feel the traditional C++ ownership model is hot garbage, and I'd love a resource that puts down specific arguments against it, but I've never come across one myself.
rubymamis•28m ago
I'm interested in the same! There are plenty of resources for C[1][2]. I just looked into my old notes and found a post for C++[3].

[1] https://btmc.substack.com/p/memory-unsafety-is-an-attitude-p...

[2] https://www.gingerbill.org/series/memory-allocation-strategi...

[3] https://dmitrysoshnikov.com/compilers/writing-a-pool-allocat...

jesse__•4m ago
Nice, thanks. I haven't read those gingerbill ones, I'll take a look :D
otherjason•28m ago
What makes you think that RAII- and arena-based strategies are in tension with one another? RAII and smart pointers are more related to the ownership and resource management model. Allocating items in bulk or from arenas is more about where the underlying resources and/or memory come from. These concepts can certainly be used in tandem. What is the substance of the argument that RAII, etc. are "hot garbage?"
einpoklum•18m ago
In my library [1], wrapping the CUDA APIs in modern C++, I do allocations which are not exactly from an arena, but something in that neighborhood - memory spaces on context on GPU devices.

Unlike the GP suggests, and like you suggest, I have indeed embraced RAII in the library - generally, not just w.r.t. memory allocation. I have not, however, replicated that idioms of the standard library. So, for example:

* My allocations are never typed.

* The allocation 'primitives' return a memory_region type - essentially a pointer and a size; I discourage the user from manipulating raw pointers.

* Instead of unique_ptr's, I encourage the use of unique_span's: owning, typed, lightweight-ish containers - like a fusion of std::span<T> and std::unique_ptr<T[]> .

I wonder if that might seem less annoying to GP.

---

[1] : https://github.com/eyalroz/cuda-api-wrappers/

jesse__•7m ago
In reverse order they were asked ..

The best argument I've ever come across against using RAII is that you end up with these nests of objects pointing to one another, and if something fails, the cleanup code can really only do one thing, which is unwind and deallocate (or whatever the cleanup path is). This structure, generally, precludes the possibility of context dependent resource re-usage on initialization failure, or on deallocation, because you kind of have to have only one deallocation path. Obviously, you could imagine supporting in an RAII context, but, the point is that you probably have to put a fair bit of conscious effort into doing that, whereas if you have a less .. rigid.. ownership model, it becomes completely trivial.

I agree that the allocation model and ownership model are independent concepts. I mentioned arena allocation because the people I know that reject the traditional C++ ownership model generally tend to favor arenas, scratch space, freelists, etc. I'm specifically interested in an ownership model that works with arenas, and tracks ownership of the group of allocations, as opposed to the typical case we think about with RAII where we track ownership of individual allocations.

aw1621107•18m ago
> explicitly rejects the smart_ptr/RAII/friends model in favor of bulk allocations, arenas, freelists, etc?

These aren't mutually exclusive; you can use the former to manage the latter, after all.

> I know there are groups of highly productive programmers that feel the traditional C++ ownership model is hot garbage

I'm not aware of links off the top of my head, but I can try to summarize the argument.

From my understanding, the argument against RAII/etc. has more to do with the mindset it supposedly encourages more than the concept itself - that RAII and friends makes it easy to think more in terms of individual objects/elements/etc. instead of batches/groups, and as a result programmers tend to follow the easy path which results in less performant/more complex code. By not providing such a feature, so the argument goes, programmers no longer have access to a feature which makes less-efficient programming patterns easy and so batched/grouped management of resources becomes more visible as an alternative.

jesse__•2m ago
Agreed. I guess I'm interested in anyone that's specifically written about ownership strategies that lean into the group allocation thing.
nwlieb•17m ago
Yes: https://m.youtube.com/watch?v=xt1KNDmOYqA

Title: “ Casey Muratori | Smart-Pointers, RAII, ZII? Becoming an N+2 programmer”

jesse__•6m ago
Good one. I was blessed to have the opportunity to watch that one live, on stream. It's always stuck with me.
verall•17m ago
If you have requirements for high performance then the traditional C++ "ownership model" (I would say a better description is "ownership strategy") is definitely "slow". It's pretty "safe" in that you usually aren't going to leak a bunch with it but bull allocations, arenas, and freelists are all potentially faster. And you wouldn't use them if they were slower since they're (usually) more to deal with.

But even in software using these strategies, they probably will be using different ownership strategies in different parts of the code. Once you're writing high performance code, you will use specific strategies that give you the best results. But it's completey domain specific.

GrowingSideways•10m ago
Such a model likely would not be referred to as "ownership". This is a relatively recent metaphor for memory management that came well after the concepts you mentioned. The fact that such a metaphor is core to rust's memory model is no coincidence.
einpoklum•31m ago
The title reminds of this:

https://youtu.be/TGfQu0bQTKc?si=7TiDRic6LaWI1Xpc&t=70

"In Rust you need to worry about borrowing. In C++ you don't have to worry about borrowing; in C++ you have to worry about ownership, which is an old concept..." :-P

jurschreuder•6m ago
I don't know why people use 'new' and 'delete' in all the examples how memory in C++ works because you never normally use them during coding only if you want to make your own container which you might do once to learn about the internals.

C++ by default creates objects by value (opposed to any other language) and when the variable goes out of scope the variable is cleaned up.

'new' you use when you want to make a global raw pointer outside of the normal memory system is how I would see it. You really never use it normally at least I don't.

A good rule of thumb is not to use 'new'.

Origami Programming [pdf]

https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/origami.pdf
1•andsoitis•41s ago•0 comments

Do AI models reason or regurgitate? Why AI is not merely a "stochastic parrot"

https://bigthink.com/the-present/do-ai-models-reason-or-regurgitate/
1•ryan_j_naughton•1m ago•0 comments

Package Manager Glossary

https://nesbitt.io/2026/01/13/package-manager-glossary.html
1•7777777phil•3m ago•0 comments

The industrial waste site that glitters like a glacier

https://www.nationalgeographic.com/environment/article/kishangarh-dumping-yard-marble-india
1•noleary•3m ago•0 comments

Nanolang: A tiny experimental language designed to be targeted by coding LLMs

https://github.com/jordanhubbard/nanolang
1•Scramblejams•4m ago•0 comments

Minnesota wants to win a war of attrition

https://www.theverge.com/policy/863632/minnesota-walz-trump-sousveillance-ice
3•andrewstetsenko•8m ago•0 comments

Tesla, BYD, and Xiaomi Are Playing Different Games

https://gilpignol.substack.com/p/tesla-byd-and-xiaomi-are-playing
1•light_triad•8m ago•0 comments

Nebra Sky Disc: the oldest depiction of astronomical phenomena

https://www.livescience.com/archaeology/nebra-sky-disc-the-worlds-oldest-depiction-of-astronomica...
1•janandonly•9m ago•0 comments

I Improved Claude's MCP-CLI Experimental MCP Fix – 18x speedup on 50 calls

1•AIntelligentTec•10m ago•0 comments

Of donkeys, mules, and horses: data structures for network prefixes in Rust

https://blog.nlnetlabs.nl/donkeys-mules-horses/
1•fanf2•10m ago•0 comments

Gravity from Information Geometry: A Lean 4 Formalization of Emergent Spacetime

https://www.academia.edu/146192044/Gravity_from_Information_Geometry_A_Lean_4_Formalization_of_Em...
1•kristintynski•12m ago•1 comments

Software Is Fine

https://shomik.substack.com/p/software-is-fine
1•mooreds•12m ago•0 comments

Do not give up your brain

https://cassidoo.co/post/good-brain/
1•mooreds•12m ago•0 comments

Use Social Media Mindfully

https://danielleheberling.xyz/blog/mindful-social-media/
2•mooreds•13m ago•0 comments

Tell HN: Deskflow is getting spammed with AI-slop PRs

1•acheong08•13m ago•0 comments

Google Mandiant Delivers the Coup de Grâce to Microsoft's NTLM

https://www.heise.de/en/news/Windows-Networks-Google-Mandiant-Delivers-the-Coup-de-Grace-to-Micro...
2•croes•13m ago•0 comments

Apple's "Protect Mail Activity" Doesn't Work

https://www.grepular.com/Apples_Protect_Mail_Activity_Doesnt_Work
2•mike-cardwell•15m ago•0 comments

Targeted Bets: An alternative approach to the job hunt

https://www.seanmuirhead.com/blog/targeted-bets
1•seany62•17m ago•1 comments

Jazz – The Database That Syncs

https://jazz.tools/
1•aleksjess•18m ago•1 comments

Targeted Bets: An alternative approach to the job hunt

1•seany62•18m ago•0 comments

Nanotimetamps: Time-Stamped Data on Nano Block Lattice

https://github.com/SerJaimeLannister/nanotimestamp/wiki
2•Imustaskforhelp•19m ago•0 comments

Starlink users must opt out of all browsing data being used to train xAI models

https://twitter.com/cryps1s/status/2013345999826153943
6•pizza•19m ago•1 comments

Nobody knows how large software products work

https://www.seangoedecke.com/nobody-knows-how-software-products-work/
2•7777777phil•23m ago•0 comments

Moving from Java to Python (2014)

https://yusufaytas.com/moving-from-java-to-python/
3•jatwork•23m ago•0 comments

19 hacks to get your startup's first customers (I made $150k+)

https://twitter.com/hustle_fred/status/2013200865956426130
2•Farid_Sukurov•24m ago•0 comments

Lava Lamps in Cloudflare's Encryption

https://www.cloudflare.com/en-gb/learning/ssl/lava-lamp-encryption/
1•cl3misch•25m ago•0 comments

The assistant axis: situating and stabilizing the character of LLMs

https://www.anthropic.com/research/assistant-axis
3•mfiguiere•27m ago•0 comments

Multi-tenant SaaS is dead for mid-market (and why K8s namespaces are the future)

1•nthypes•29m ago•0 comments

Show HN: Shebe, a fast, simple and tiny code-search tool

https://github.com/rhobimd-oss/shebe
1•marwamc•31m ago•1 comments

Looking Back at the Best Inventions of 2001

https://shkspr.mobi/blog/2026/01/looking-back-at-the-best-inventions-of-2001/
3•7777777phil•31m ago•0 comments