News for hackers. Stuff that matters.
Re:What is malloc doing, anyway? by assumed_throwaw on Monday November 09, 2026 @08:13AM (#50425141)
Tangential, but the complexity and over-head of the malloc abstraction is precisely what made me very intrigued by "stack-only" languages, such a recently published Goose. There is something very elegant and exciting about the idea of (almost) completely getting rid of it. Just as modern programming languages have developed good ergonomics that replaces a garbage collector, the same could hopefully be done to replace heap-allocated memory.
Re:What is malloc doing, anyway? by nialv7 on Monday November 09, 2026 @08:29AM (#50425326)
the stack is just a bump allocator. the unique thing about stack-only languages is lifetime management, not really how they allocate.
Re:What is malloc doing, anyway? by saagarjha on Monday November 09, 2026 @08:59AM (#50425712)
A stack only language that called into malloc to service allocations would not be very interesting.
Re:What is malloc doing, anyway? by jacquesm on Monday November 09, 2026 @09:33AM (#50426195)
Most stack oriented languages also have a heap.
Re:What is malloc doing, anyway? by smashspectacle on Monday November 09, 2026 @09:35AM (#50426220)
This is actually what first drew me to zig. Most people talk about error handling, but the explicit allocator handling and making choosing an allocator a deliberate consideration has pushed me to better coding habits in general. I don't think it is possible or practical to completely abandon heap-allocated memory, but explicitly choosing between allocation types and styles that most precisely fit a job has great potential for meeting that spirit.
Re:What is malloc doing, anyway? by EPWN3D on Monday November 09, 2026 @10:54AM (#50427262)
I don't think we can get rid of heap memory, but I do think we can get rid of a shared heap.
Re:What is malloc doing, anyway? by tremon on Wednesday November 11, 2026 @12:15PM (#50455852)
Isn't a shared heap pretty much the difference between multithreading and multiprocessing? Won't it turn every form of thread synchronization into a remote procedure call?
Re:What is malloc doing, anyway? by dreamcompiler on Monday November 09, 2026 @03:35PM (#50431674)
You can definitely avoid heap-allocated memory; early versions of Fortran could not do dynamic memory allocation at all. You basically had to preallocate giant arrays and build your own memory manager. It was sheer hell but it worked.
Re:What is malloc doing, anyway? by tremon on Monday November 09, 2026 @03:47PM (#50431868)
To me the value of zig's approach is in being able to control the allocations that happen in (library) code. When you call a C function, all kinds of heap allocations may happen between the callee and malloc, even if the return value of the call is in caller-allocated memory. With zig, you can pass a local (arena) allocator instead and avoid uncontrolled global allocations.
Re:What is malloc doing, anyway? by connicpu on Monday November 09, 2026 @10:11AM (#50426740)
Avoiding the heap entirely isn't required and will prevent you from working with substantial amounts of data. The real key is to allocate all the memory you need up front and use that large block for a long duration.
Re:What is malloc doing, anyway? by packetlost on Monday November 09, 2026 @11:29AM (#50427811)
> Avoiding the heap ... will prevent you from working with substantial amounts of data

This isn't really true of Goose, but it's avoiding 'the heap' in the C sense, not in the OS allocated pages of memory sense. One is a programming abstraction provided by the other, but GP is only talking about the programming abstraction. I have never encountered a program that couldn't have memory be explicitly tied to lexical scope, but it can result in less efficient use of it in some cases.

Re:What is malloc doing, anyway? by dataflow on Monday November 09, 2026 @12:12PM (#50428413)
[delayed]
Re:What is malloc doing, anyway? by packetlost on Monday November 09, 2026 @12:57PM (#50429008)
Goose is a weird language. It uses a pile of pre-allocated datastacks, enforces lexical lifetimes for all data, and has first-class syntactic support for capped, growable, and shrinkable arrays. It also spits out a pile of C that gets all the optimization benefits of <insert C compiler of choices>.

Goose in particular excites me because I have done similar design work over the years.

Re:What is malloc doing, anyway? by senderista on Monday November 09, 2026 @01:46PM (#50429746)
I just want to point out that the author of Goose has plenty of technical cred, having authored flatbuffers and lots of other cool stuff.
Re:What is malloc doing, anyway? by dataflow on Monday November 09, 2026 @03:34PM (#50431665)
[delayed]
Re:What is malloc doing, anyway? by Someone on Monday November 09, 2026 @04:41PM (#50432644)
[delayed]
Re:What is malloc doing, anyway? by kbolino on Monday November 09, 2026 @05:00PM (#50432866)
It might be worthwhile to balance these positive claims against the language's own stated limitations [1].

Also, I took a closer look at the actual benchmark results [2]. For memory usage, at least, the median and mode would show Goose using slightly more memory than the well optimized C++ and Rust. It looks like the mean is skewed in Goose's favor by a couple of benchmarks where it does extremely well due to its variable-length enums.

[1]: https://aardappel.github.io/goose/docs/tutorial.html#18-limi...

[2]: https://aardappel.github.io/goose/bench/results.html

Re:What is malloc doing, anyway? by eschneider on Monday November 09, 2026 @10:23AM (#50426901)
Embedded programming has taught me that almost anything can be a stack-only language if you want it to be, even C++. :)
Re:What is malloc doing, anyway? by i2talics on Monday November 09, 2026 @01:39PM (#50429642)
If your takeaway from this was "wow malloc has so much overhead" then you may want to notice the part where they benchmark against glibc malloc and glibc is somewhere between 20-150x faster than their implementation. And that's fine, this exposition is basically what you would learn in an undergraduate systems programming course, but production allocators are able to significantly cut down the amount of work needed to grab an allocation, especially for small ones that occur frequently.
Re:What is malloc doing, anyway? by packetlost on Tuesday November 10, 2026 @10:46AM (#50442018)
> where they benchmark against glibc malloc and glibc is somewhere between 20-150x faster than their implementation

Where are you seeing that?

Re:What is malloc doing, anyway? by SkiFire13 on Monday November 09, 2026 @02:36PM (#50430641)
> the complexity and over-head of the malloc abstraction is precisely what made me very intrigued by "stack-only" languages, such a recently published Goose.

Goose's stacks have quite some complexity themselves. They preallocate a ton of memory. The compiler has to magically match your code to each stack. You are not allowed to have nested growing types (e.g. creating an adjacency list is not so easy). And after all for some usecases you still end up using an allocator (Goose's linked list example ends up building a pool allocator for the linked list nodes!)

Re:What is malloc doing, anyway? by chemodax on Monday November 09, 2026 @08:54PM (#50435091)
In my opinion pools are the best concept for unmanaged code.