Live data from Hacker News

Ask HN: What if a language's structure determined memory lifetime?

news.ycombinator.com

11–19 of 19 posts

Re: Ask HN: What if a language's structure determined memory lifetime?

#11

I'm not sure this needs to be its own language. In C/C++, this can be done by just not using malloc() or new. You can get an awfully long way in C with only stack variables (or even no variables, functional style). You can get a little bit further with variable length arrays, and alloca() added to the mix. With C++, you have the choice of stack, or raw new/delete, or unique_ptr, or shared_ptr / reference counting. I…

Great feedback!

You’re absolutely right — C and C++ give you the primitives to do this manually. If every developer followed the “stack first, heap only when necessary” discipline, and carefully used unique_ptr or avoided new/delete when possible, you could achieve much of the same safety and determinism.

The difference I’m aiming for is that these constraints aren’t optional — they’re baked into the language and compiler. You don’t rely on every developer making the right choice; instead, the structure of the code itself enforces ownership and lifetime rules.

So in your terms, instead of “doing dumb things is an error,” it’s structurally impossible to do dumb things in the first place. The language doesn’t just punish mistakes with foot-guns, it makes the safe path the only path.

This also opens up other possibilities that are really awkward in C/C++, like structured concurrency with deterministic memory cleanup, restartable scopes, and safe parallel allocations, without relying on GC or heavy reference counting.

I’d be curious: if C++ had a compiler that made stack-first allocation the default and forbade escapes unless explicit, would that solve most of the problems you’ve experienced, or are there still edge cases that would require a fundamentally different runtime model?

Re: Ask HN: What if a language's structure determined memory lifetime?

#12

I'm not sure this needs to be its own language. In C/C++, this can be done by just not using malloc() or new. You can get an awfully long way in C with only stack variables (or even no variables, functional style). You can get a little bit further with variable length arrays, and alloca() added to the mix. With C++, you have the choice of stack, or raw new/delete, or unique_ptr, or shared_ptr / reference counting. I…

Great feedback! You’re absolutely right — C and C++ give you the primitives to do this manually. If every developer followed the “stack first, heap only when necessary” discipline, and carefully used unique_ptr or avoided new/delete when possible, you could achieve much of the same safety and determinism. The difference I’m aiming for is that these constraints aren’t optional — they’re baked into the language and com…

As far as I'm concerned, stack-first allocation _is_ the default. It's true that the default exists in my head rather than in in a compiler, though.

Maybe think about whether what you propose could exist as a compiler warning, or static analysis tool. Or, if you want to create your own language, go for it, that's cool too.

For my purposes... the choice of paradigms, compilers, platforms with C++ and ability to handle and work on decades of existing code outweighs the benefits of "improved" languages, but that's just me.

Re: Ask HN: What if a language's structure determined memory lifetime?

#13

I once worked for about a decade with a body of server-side C code that was written like this. Almost every data structure was either statically allocated at startup or on the stack. I inherited the codebase and kept the original style, once I'd got my head around it. Positives were that it made the code very easy to reason about, and my impression was that it made it reliable - ownership of data was mostly obvious,…

> it was hard to (for example) mistakenly use a data structure after it had been free'd

Hard? Why not impossible?

Re: Ask HN: What if a language's structure determined memory lifetime?

#14
post #3

J has some of this approach but it has been made mostly for math so it is not optimized for CRUDs.

That’s an interesting comparison. I agree J aligns philosophically (values over references), and you're right that it feels more optimized for pure mathematical work rather than managing long-lived, mutable state in concurrent services. What I’m exploring is whether a model like this can provide similar benefits in CRUD-heavy systems without needing GC or manual memory management. If you’ve seen J used effectively in…

No I haven't, but I want you to notice that this script language has no GC at all. So theoretical part of your message is possible. Just it needs a Kenneth Iverson level of programmer if to continue the work in array programming paradigm.

It may be not efficient at all for using rich types and structs because stack language is the earliest approach whose pros are coming from ability to make the single-pass compiler. If your requirements do not fit in single-pass approach, then you are going to have a really hard time of guessing what and when is needed to be recycled.

Re: Ask HN: What if a language's structure determined memory lifetime?

#15
post #13

I once worked for about a decade with a body of server-side C code that was written like this. Almost every data structure was either statically allocated at startup or on the stack. I inherited the codebase and kept the original style, once I'd got my head around it. Positives were that it made the code very easy to reason about, and my impression was that it made it reliable - ownership of data was mostly obvious,…

> it was hard to (for example) mistakenly use a data structure after it had been free'd Hard? Why not impossible?

I distrust absolutes.

Re: Ask HN: What if a language's structure determined memory lifetime?

#16
post #13

Earlier quoted context omitted.

> it was hard to (for example) mistakenly use a data structure after it had been free'd Hard? Why not impossible?

I distrust absolutes.

OK, so how was it possible?
Post reply on HN