News for hackers. Stuff that matters.
Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by sirwhinesalot on Sunday July 13, 2025 @11:18AM (#44551032)
The advantage is that the allocations are grouped: they're allocated in the same memory region (good memory locality) and freed in bulk. The tradeoff is needing to explicitly create these scopes and not being able to have custom deallocation logic like you can in a destructor.

(This doesn't seem to have anything to do with borrow checking though, which is a memory safety feature not a memory management feature. Rust manages memory with affine types which is a completely separate thing, you could write an entire program without a single reference if you really wanted to)

Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by ameliaquining on Sunday July 13, 2025 @11:26AM (#44551080)
You can also do those things in an RAII language with an arena library. Is the complaint just that it's too syntactically verbose?
Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by sirwhinesalot on Sunday July 13, 2025 @12:06PM (#44551389)
I think the point is that it is the blessed/default way of doing things, rather than opt-in, as in C++ or Rust.

Rust doesn't even have a good allocator interface yet, so libraries like bumpalo have a parallel implementation of some stdlib types.

Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by cogman10 on Sunday July 13, 2025 @11:52AM (#44551287)
It seems like exactly the same verbosity as what you'd do with a custom allocator.

I think the only real grace is you don't have to pass around the allocator. But then you run into the issue where now anyone allocating needs to know about the lifetimes of the pool of the caller. If A -> B (pool) -> C and the returned allocation of C ends up in A, now you potentially have a pointer to freed memory.

Sending around the explicit allocator would allow C to choose when it should allocate globally and when it should allocate on the pool sent in.

Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by jdcasale on Sunday July 13, 2025 @11:46AM (#44551248)
I am also struggling to see the difference between this and language-level support for an arena allocator with RAII.
Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by lerno on Sunday July 13, 2025 @12:28PM (#44551545)
You can certainly do it with RAII. However, what if a language lacks RAII because it prioritizes explicit code execution? Or simply want to retain simple C semantics?

Because that is the context. It is the constraint that C3, C, Odin, Zig etc maintains, where RAII is out of the question.

Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by imtringued on Monday July 14, 2025 @09:00AM (#44559690)
If you want RAII to be explicit, then show an error if you fail to call the destructor. That's it.
Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by lerno on Monday July 14, 2025 @09:27AM (#44559981)
You lost me there I'm afraid.
Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by ameliaquining on Monday July 14, 2025 @01:26PM (#44562824)
The idea is, you could have a language like Rust, but with linear rather than affine types. Such a language would have RAII-like idioms, but no implicit destructors; instead, it'd be a compile-time error to have a non-Copy local variable whose value is not always moved out of it before its scope ends (i.e., to write code that in Rust could include an implicit destructor call). So you would have explicit deallocation functions like in C, but unlike in C you could not have resource leaks from forgetting to call them, because the compiler would not let you.

To the extent that you subscribe to a principle like "invisible function calls are never okay", this solves that without undermining Rust's safety story more broadly. I have no idea whether proponents of "better C" type languages have this as their core rationale; I personally don't see the appeal of that flavor of language design.

Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by bmacho on Friday July 18, 2025 @05:28AM (#44602746)
Verdagon has this linear types/higher-RAII article: https://verdagon.dev/blog/higher-raii-7drl

It is about types that can't be copied and can't go out of scope, and the only way to destroy them is to call one of their destructors. This is compile time checkable.

In theory they can solve a lot of problems easily, mainly resource management. Also it generalizes C++'s RAII, and similar to Rust's ownership.

In practice they haven't got support in any mainstream programming language yet.

Re:Forget Borrow Checkers: C3 Solved Memory Lifetimes with S... by lerno on Monday July 14, 2025 @01:44PM (#44563043)
Ok then I understand what you mean (I couldn't respond directly to your answer, maybe there is a limit to nesting in HN?).

Let me respond in some more detail then to at least answer why C3 doesn't have RAII: it tries to the follow that data is inert. That is – data doesn't have behaviour in itself, but is acted on by functions. (Even though C3 has methods, they are more a namespacing detail allowed to create methods that derive data from the value, or mutate it. They are not intended as organizational units)

To simplify what the goal is: data should be possible to create or destroy in bulk, without executing code for each individual element. If you create 10000 objects in a single allocation it should be as cheap to free (or create) as a single object.

We can imagine things built into the type system, but then we will need these unsafe constructs where a type is converted from its "unsafe" creation to its "managed" type.

I did look at various cheap ways of doing this through the type system, but it stopped resembling C and seemed to put the focus on resource management rather than the problem at hand.

So that is why it's closer to C than Rust.