News for hackers. Stuff that matters.
Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @04:05PM (#46684500)
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.
Re:Understanding C++ Ownership System by rubymamis on Monday January 19, 2026 @04:24PM (#46684695)
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...

Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @04:48PM (#46684960)
Nice, thanks. I haven't read those gingerbill ones, I'll take a look :D
Re:Understanding C++ Ownership System by otherjason on Monday January 19, 2026 @04:24PM (#46684701)
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?"
Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @04:45PM (#46684921)
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.

Re:Understanding C++ Ownership System by HarHarVeryFunny on Monday January 19, 2026 @06:37PM (#46686100)
That "nest of objects point to each other" makes no sense ... RAII is just a technique where you choose to tie resource management to the lifetime of an object (i.e. acquire in constructor, release in destructor).

If an exception gets thrown, causing your RAII object scope to be exited, then no problem - the object destructor gets called and the resource gets released (this is the entire point of RAII - to make resource allocation and deallocation automatic and bullet-proof).

If you are creating spaghetti-like cross-referencing data structures, then that is either poor design or something you are doing deliberately because you need it. In either case, it has nothing to do with RAII, and RAII will work as normal and release resources when the managing object is destroyed (e.g. by going out of scope).

RAII could obviously be used to allocate/free a resource (e.g. temp buffer) from a free list, but is not really relevant to arena allocation unless you are talking about managing the allocation/release of the entire arena. The whole point of an arena allocator is the exact opposite of RAII - you are deliberately disconnecting individual item allocation from release so that you can instead do a more efficient bulk (entire arena) release.

Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @08:18PM (#46686726)
Another commentor succinctly pointed out one argument against RAII+friends is that it encourages thinking about single objects, as opposed to bulk processing.

In many contexts, the common case is in fact bulk processing, and programming things with the assumption that everything is a single, discrete element creates several problems, mostly wrt. performance, but also maintainability. [1][2]

> The whole point of an arena allocator is the exact opposite of RAII

Yes, agreed. And the internet is rife with people yelling about just how great RAII is, but comparatively few people have written specifically about it's failings, and alternatives, which is what I'm asking about today.

[1] https://www.youtube.com/watch?v=tD5NrevFtbU

[2] https://www.youtube.com/watch?v=rX0ItVEVjHc&t=2252

Re:Understanding C++ Ownership System by vacuity on Monday January 19, 2026 @09:13PM (#46687078)
I don't think the "complex web of objects to be deallocated" scenario is usually a problem, but I generally agree with your points. As always, careful design and control of the software is important. Abstractions are limited; spend them carefully.
Re:Understanding C++ Ownership System by HarHarVeryFunny on Monday January 19, 2026 @10:13PM (#46687496)
> one argument against RAII+friends is that it encourages thinking about single objects, as opposed to bulk processing.

RAII is just a way to guarantee correctness by tying a resource's lifetime to that of an object, with resource release guaranteed to happen. It is literally just saying that you will manage your resource (a completely abstract concept - doesn't have to be memory) by initializing it in an objects constructor and release it in the destructor.

Use of RAII as a technique is completely orthogonal to what your program is doing. Maybe you have a use for it in a few places, maybe you don't. It's got nothing to do with whether you are doing "bulk procesing" or not, and everything to do with whether you have resources whose usage you want to align to the lifetime of an object (e.g this function will use this file/mutex/buffer, then must release it before it exits).

Re:Understanding C++ Ownership System by HarHarVeryFunny on Tuesday January 20, 2026 @09:22AM (#46692107)
The alternative to RAII is simply do-it-yourself ! For example, if you are writing multi-threaded code and need a mutex to protect some data structure, then you'd need an explicit mutex_lock() before the access, and an explicit mutex_unlock() afterwards... if your code might throw exceptions or branch due to errors, then make sure that you have mutex_unlock() calls everywhere necessary!

Automating this paired lock and unlock, to avoid any programmer error in missing an unlock in one of the error paths, just makes more sense, and this is all that RAII is doing - it's not some mysterious religion or design philosophy that needs to pervade your program (other than to extent that it would make sense to remove all such potential programmer errors, not just some!).

In this mutex example, RAII would just mean having a class "MutexLocker" (C++ provides std::lock_guard) that does the mutex_lock() in it's constructor and mutex_unlock() in it's destructor.

Without RAII, your code might have looked something like this:

try {

  mutex_lock(mut);

  // access data structure

  mutex_unlock(mut);
} catch (...) { // make sure we unlock the mutex in the error case too !

  mutex_unlock(mut);
}

With the RAII approach the mutex unlocks are automatic since they'll happen as soon as the mutex locker goes out of scope and its destructor is called, so the equivalent code would now look like this:

try {

  MutexLocker locker(mut);

  // access data structure
} catch (...) {

}

That's it - no big deal, but note that now there was no need to add multiple mutex unlock calls in every (normal + error) exit path, and no chance of forgetting to do it.

You can do the same thing anywhere where you want to guarantee that some paired "resource" cleanup activity take place, whether that is unlocking a mutex, or closing a file, or releasing a memory buffer, or whatever.

You may not think of it as RAII, but this approach of automatic cleanup is being used anywhere you have classes that own something then release it when they are done with it, for example things like std::string (and all the C++ container classes) is doing this, as are C++ <stream> file objects, C++ smart pointers, C++ lock_guard for mutexes, etc, etc.

The name "RAII" (resource acquisition is initialization) is IMO a poor name for the technique, since the value is more in the guaranteed, paired, resource release than the acquisition, which was never a problem. Scope-based resource management, or Lifetime-based resource management, would better describe it!

Of course RAII isn't always directly applicable because maybe you need to acquire a resource in one place and release it someplace else entirely, not in same scope, although you could choose to use a smart pointer together with a RAII resource manager to achieve that. For example create a resource with std::make_shared<ResourceManager>() in one place, and resource release will still happen automatically whenever reference counting indicates that all uses of the resource are done.

Re:Understanding C++ Ownership System by mgaunard on Monday January 19, 2026 @10:39PM (#46687657)
There are plenty of people who use RAII with arenas for nested group of objects.

Bloomberg for example had a strong focus on that, and they enhanced the allocator model quite significantly to be able to standardize this. This was the reason for stateful allocators, scoped allocators, uses-allocator construction and polymorphic memory resources.

Re:Understanding C++ Ownership System by einpoklum on Monday January 19, 2026 @04:34PM (#46684803)
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/

Re:Understanding C++ Ownership System by aw1621107 on Monday January 19, 2026 @04:34PM (#46684801)
> 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.

Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @04:50PM (#46684983)
Agreed. I guess I'm interested in anyone that's specifically written about ownership strategies that lean into the group allocation thing.
Re:Understanding C++ Ownership System by nwlieb on Monday January 19, 2026 @04:35PM (#46684813)
Yes: https://m.youtube.com/watch?v=xt1KNDmOYqA

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

Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @04:46PM (#46684940)
Good one. I was blessed to have the opportunity to watch that one live, on stream. It's always stuck with me and, now that I think about it, is the best resource I know of that puts those ideas into words/writing.
Re:Understanding C++ Ownership System by verall on Monday January 19, 2026 @04:35PM (#46684822)
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.

Re:Understanding C++ Ownership System by GrowingSideways on Monday January 19, 2026 @04:42PM (#46684890)
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.
Re:Understanding C++ Ownership System by HarHarVeryFunny on Monday January 19, 2026 @05:29PM (#46685390)
Those types of allocation technique were common back in the day for efficiency reasons, maybe still relevant for things like embedded programming where you need to be more careful about memory usage and timing, but I would say that nowadays for normal application usage you are better off using smart pointers.

It's not a matter of one being strictly better than the other, but rather about using the right tool for the job.

Re:Understanding C++ Ownership System by dundarious on Monday January 19, 2026 @06:38PM (#46686111)
I disagree, as group lifetimes are conceptually and architecturally often easier and simpler than having lots of individual lifetimes managed by smart pointers. And sure, you can often slap shared_ptr around the place, or hopefully a less lazy smart ptr choice, but it makes the code harder to understand by obscuring ownership rather than eliminating it as a concern.
Re:Understanding C++ Ownership System by HarHarVeryFunny on Monday January 19, 2026 @06:46PM (#46686178)
I don't understand why you see it that way... Whether using an arena allocator or smart pointer, in both cases you need to allocate the individual object (p = arena_allocate(arena, ...), or p = std::make_unique<T>()). In the case of a smart pointer that is all you have to do - deallocation will be automatic. In the case of the arena allocator you will also need to deallocate the entire arena at the appropriate time.

How are you conceptualizing this that the arena allocator is simpler?

How are you conceptualizing smart pointers as "obscuring" ownership, when the entire point of them is to make ownership explicit! The smart pointer IS the owner!

Re:Understanding C++ Ownership System by jesse__ on Monday January 19, 2026 @05:45PM (#46685593)
Many soft-realtime systems make use of these techniques, specifically 3D graphics and game engines.
Re:Understanding C++ Ownership System by edflsafoiewq on Tuesday January 20, 2026 @02:51AM (#46689068)
Re:Understanding C++ Ownership System by jesse__ on Wednesday January 21, 2026 @02:03AM (#46702062)
This is exactly the kind of thing I was after, thank you!