r/ProgrammingLanguages • u/Null-Test-2026 • May 30 '26
TIL some old high-level languages did not have dynamic memory management (heap)
The other day I stumbled upon the statement that idiomatic FORTRAN and COBOL did not use a heap for dynamic memory allocation! Off-stack data structures had to be compiled into the `.data` segment, laid out when the program loads.
Does that mean "the Memory Management Problem" which has led to abominable "solutions" such as Garbage Collection and Rust Ownership has been a self-inflicted struggle for the past 50 years? Could a new modern language be designed without a heap?
31
Upvotes
68
u/Schnickatavick May 30 '26
Could a new language forgo the heap? Yeah, absolutely. Should it? Absolutely not... Getting rid of the heap won't get rid of memory management problems, it would just shuffle it around
66
u/awoocent May 30 '26
No.
These older languages did interact with memory in a way that's sort of unfamiliar to us today, but that's predominantly because they were so memory constrained. When your entire computer only has 8 kilobytes of memory it's a lot more practical to give literally every single one of those bytes a name and label, and a lot less permissible to have a little bit of wastage like you get in a malloc.
That all said, memory is memory! Really the only reason these languages are thought of as not having a heap is that a lot of programs didn't refer to it as such, but the fundamental problem of "I have a finite amount of memory, how do I dole it out to store different things and reuse it over the course of a program" is always present, no matter where that memory "came from" - even on a modern machine, the heap is just a name for a specific hunk of the RAM chip(s) in your laptop. That hunk just contains a lot more bytes than an equivalent machine from the 70s. But any pattern that manages a finite resource like address or index space and eventually wants to be able to reuse parts of that resource is vulnerable to the bug that someone is accidentally holding onto a pointer that they think is live but was really freed somewhere else. You might not realize it quite so suddenly in an old program given you don't have a protected memory system to throw a fault, or an OS to report it, but this type of memory unsafety could absolutely lead to all the same types of bugs.
30
u/shponglespore May 30 '26
We could solve all programming problems at once by eliminating software altogether!
19
u/GoblinToHobgoblin May 30 '26
You just force the users to implement their own heap, essentially
29
u/godeling May 30 '26
Not familiar with FORTRAN or COBOL, but this to me just sounds like they didn’t have dynamic memory at all. Putting your data into the
.datasection would mean that the data is known at compile-time. If it’s known at compile-time, it’s static, not dynamic13
u/Ambitious_Fortune537 May 30 '26 edited May 30 '26
I don't think the memory management problem was self-inflicted, only that early FORTRAN and COBOL could rely mostly on static memory because many programs had predictable memory requirements. As software became more complex, the need for data structures whose size and lifetime were not known at compile time became a problem, leading to the adoption of dynamic memory allocation.
A modern language without a heap is possible, and many embedded systems already follow that approach, but the trade-off is reduced flexibility and the need to know memory requirements ahead of time. Because of that, one could argue that such a language loses some of the characteristics expected from a language for general use and may become less convenient for applications that rely heavily on dynamically sized data structures. However, that does not make it impossible to build software, it simply means that the language is optimized for a different set of constraints and use cases.
12
u/el_extrano May 30 '26
I like retro programming as a hobby, and FORTRAN (the 77 standard) has been my focus for a couple years.
It's actually a pretty easy language to pick up, with only about 32 statements that are normally used (about the same as C). Most numerical problems, which of course is what FORTRAN was designed for, don't need dynamic memory. It's quite well suited to its designed task.
The difficulty of working in FORTRAN isn't numerical methods, but rather all the other code you need to setup your problem and interact with the user. I/O and string handling were famously difficult, and the only native data structure is the linear, typed array. There isn't a struct type nor are there pointers, so implementing more complicated data structures is tricky.
Since there's no heap allocations, a widely used work-around was to declare a "work array", and write your own allocators and deallocators on this memory. Then, you can either pass your work array into using routines (or declare it in
COMMONso that it's accessible everywhere) along with an address and length to index into the array. Note that since arrays have a single type, if you want to interpret space in the work array as another type, you need to declare another array of that type andEQUIVALENCEthem together so they occupy the same space in memory. Then, you need to do address arithmetic to convert addresses into indexes for the new array.I'd also mention that since you have file I/O, another technique available is to create a virtual memory system using the disk. This way, you only need to statically allocate space for the largest buffers you'll need. You can read blocks from disk on demand and write them back on change.
All this need for tricks led to Greenspun's Tenth Rule, which says that:
Here's a small example program showing some of the basic techniques one might use to work with a "work array" in a
COMMONblock. Note that since we don't have pointers, essentially what we're doing is declaring arrays of each type we need to all be at the location of the work array, then indexing into the correct "view" of the work array to get to the data. You might imagine how you could use this to define your own multi-typed structs with address offsets for fields, so that routines could accept a single address as a "pointer". It's easy to see why C was an immediate improvement for general purpose computing!Which gives the following output:
6
u/Snatchematician May 30 '26
Old Fortran also didn’t have a stack either. (And so recursive function calling was not possible.)
5
u/AustinVelonaut Admiran May 30 '26 edited May 30 '26
Some didn't even have the concept of a stack! I worked with an early Fortran compiler that reserved space at the beginning of a function (i.e. in the "text" section) to store the caller's return pointer (recursion was not allowed).
5
u/drinkcoffeeandcode mgclex, mgcpgen, & owlscript May 31 '26
a heap? My brother in christ, FORTRAN had statically allocated ACTIVATION RECORDS for crying out loud. Recursion wasn't just discouraged, it was impossible!
4
u/Tasty_Replacement_29 Bau May 30 '26
There are new programming languages that follow this model, e.g. the Wuffs language: it cannot allocate or free memory. I think this is somewhat extreme, and limits what you can do with a language.
But I could imagine a "middle ground", where memory management is limited to certain places. This is related to region-based memory management (arena allocation), and Ada/Spark, Odin. Some embedded C coding standards limit where and when you can allocate memory.
Memory management is one of the most interesting language design question in my view: it should be simple to use, but safe, and fast.
4
u/protimewarp May 31 '26
You don't need a new language! In automotive embedded software dynamic memory allocation is banned as per MISRA C guidelines.
"MISRA C:2023 D4.12: Dynamic memory allocation shall not be used."
Similar applies to other safety critical applications.
3
u/azimux May 30 '26
Would you even need a new language? You could of course just not use the heap in a C program today, right? Nothing is forcing one to use malloc and friends. You could just declare the things you need up-front in static variables. If not using the heap were superior universally, I would have to wonder why anybody uses malloc, right? My assumption is that preemptively instantiating everything instead of malloc is either annoying or even prohibitive to a non-trivially large class of software problems.
Re: "solutions", I guess "abominable" is subjective but my subjective opinion is that I enjoy several languages that include garbage collection and that they'd be less enjoyable for what I use them for if garbage collection were removed from them without replacement.
3
u/jojohohanon May 30 '26
You guesstimated how much memory you would need and manually laid out your arrays and soforth.
I worked with a query-oriented LISP application in 2003 that had JUST started using GC. Before then, each query was cons only. Each cons just bumped the heap pointer. We knew how much max memory we would use for a query and had enough ram for that. At the end of the query, we reset the heap pointer back to base.
What I mean by this anecdote is that this is hardly memory management. But also it worked fine for a modern application.
Because our problem was very hard but also constrained. I imagine many cobol and Fortran tasks fall into that category as well
4
u/stupid_cat_face May 30 '26
Software development was a totally different experience back then. Kids these days wouldn’t know what to do with themselves if there wasn’t dynamic (and automated) memory management.
2
u/bshapiro001 May 30 '26
not likely today. Just means that FORTRAN and COBOL did not have dynamic memory. Look at languages like PL/1, Ada and Algol 68.
2
u/patrickkidger May 30 '26
For a modern example of this, see JAX.
For those outside machine learning (or I guess we say AI, these days), this is an array-based (numpy-like) Python DSL, widely used in machine learning.
It uses fixed-sized arrays for all operations (specifically, the size of the array must be data-independent and known at compile time). At least in terms of the programming model, I believe this is equivalent to having no heap.
It's surprising how little this actually matters, there are relatively few operations that are complicated by this.
2
u/thalovry May 31 '26
Many console video games into the 2010s did not use the heap.
2
u/kenjiArthur Jun 01 '26
Hello! To be honest, I'm not an expert, but I don’t think dynamic memory management was exactly a self-inflicted problem. It is more like a tradeoff that became useful as programs became more flexible.
A language without a heap can definitely exist, especially for embedded systems, real-time software, or programs where the size and lifetime of data are predictable. In those cases, maybe avoiding heap allocation can even be a good design choice. But for many modern programs, you often need data whose size or lifetime is only known at runtime, like dynamic strings, trees, graphs, user-created objects, etc. At that point, some kind of dynamic allocation becomes very convenient.
So I think a modern language without a heap is possible, but probably with a more specific domain or with some restrictions. For a general-purpose high-level language, the heap is not just a mistake. It is a compromise.
1
u/mamcx May 30 '26
Just for fun, you can do this like creating a huge big "static" array and a call it "memory", then you are fully bounding, has not cost for resizing and such.
But now you have other problems: You have a fixed cost and then you need to deal with how coordinate memory, fragmentation, concurrent access and such.
1
u/Jaded_Following2089 Jun 01 '26
You can write applications without the heap, but I think this is largely impractical, and is probably why it did not continue to be the norm after FORTRAN and COBOL. Without dynamic allocation, every data structure's size must be fixed at compile time, and so users are forced to over-allocate massive static arrays in the
.datasegment, which is, in my opinion, a worse solution than the mentioned Garbage Collector and Rust Ownership.Also, since you must manually track which array slots are free or occupied, you end up having to write a lot of boilerplate, and you keep the heap problem of fragmentation, which shows that it simply transfers the burden of managing memory from the system to your application.
1
u/sreekotay Jun 02 '26
Postscript (which derived from Forth) also.
1
u/SuspiciousEbb4734 Jun 03 '26
Yes, in a sense the struggles with memory management over the past decades have been partly self-inflicted, because modern programming often demands flexibility, dynamic data structures, and safe abstractions that weren’t common in early languages like FORTRAN or COBOL. Back then, programs were simpler, data layouts were mostly static, and the compiler could place everything in fixed memory segments.
Designing a modern language without a heap is possible, but it would require rethinking many things we take for granted: dynamic collections, recursion, object lifetimes, concurrency, and general-purpose abstractions. You could focus on stack-only allocation, arenas, or region-based memory, but it would limit flexibility compared to languages with a heap. Some niche or embedded languages successfully do this, but for general-purpose applications, a heap still provides a lot of power and convenience.
1
u/un_virus_SDF Jun 04 '26
C does not have heap either.
You can talk about malloc but this is a external function.
In fortran you can also call a kind of malloc.
Every language must have a way to access heap. The only diff is how it's provided. Is it bultin or not?, gc, manual, raii, ...?
Edit : And for heap data that must be initialized at runtime. If you don't want heap use .bss not .data
1
u/sazasoo Jun 08 '26
The heap is actually a necessary tradeoff for flexibility, we needed the heap to allocate objects that must outlive the execution of the function, since local memory is destroyed the moment a function returns, any data that needs to persist and be shared across different parts of the program must live on the heap.
But to answer your second question: yes, modern systems can be designed without a heap, and it's actually the standard in some specific fields right now, like when working with devices with very smal memory (as in edge devices like microcontrollers). For example, wile I'm deploying TinyML models for those small devices, every byte counts so I basically pre-allocate the tensor arenas and state buffers so the system never predictably OOM (Out Of Memory).
Dropping the heap can be a nice design choice for a language targeting embedded or real-time systems, but for general-purpose programming i would say it's not a very practical idea.
0
u/[deleted] May 30 '26
[deleted] — view deleted comment