What is malloc doing, anyway?
68 points by benmandrew 4 hours ago | 13 comments

assumed_throwaw 2 hours ago
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.
reply
smashspectacle 26 minutes ago
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.
reply
jacquesm 28 minutes ago
Most stack oriented languages also have a heap.
reply
nialv7 2 hours ago
the stack is just a bump allocator. the unique thing about stack-only languages is lifetime management, not really how they allocate.
reply
saagarjha 2 hours ago
A stack only language that called into malloc to service allocations would not be very interesting.
reply
wpmaven 59 minutes ago
I found this one very well explained time ago, for those looking for more details: https://sploitfun.wordpress.com/2015/02/10/understanding-gli...
reply
benmandrew 49 minutes ago
That's a great reference, thanks!
reply
TZubiri 58 minutes ago
like many C functions you must remember that C is not low level, you won’t learn how memory is allocated by inspecting malloc() anymore than you would learn how characters are printed to the screen by reading printf() or how a file is written by reading write().

you will only learn about the thin or heavy wrapper that C is responsible for, the real magic is happening in the kernel and the device firmware

reply
foobarian 19 minutes ago
Disagree, I found malloc source useful, and writing a toy allocator especially useful because you learn about the brk() syscall which is a great breadcrumb into kernel land and a great bridge to connect the memory mapping lessons from computer architecture with application code.
reply
stevekemp 5 minutes ago
Depending on how low-level you're going, you don't even need to call brk.

My toy stop/copy GC and allocation routines just have this:

    %define HEAP_SIZE (4 * 1024 * 1024 * 1024)

    .section bss
    ;;
    ;; Two heaps so we can flip-flop between them
    ;;
    heap_a:
        resb HEAP_SIZE
    heap_a_end:
    heap_b:
        resb HEAP_SIZE
    heap_b_end:
Of course there are other syscalls involved in my compiler/runtime, but for memory allocations the static area sufficed for me.
reply
Someone 26 minutes ago
Malloc needs the kernel to obtain memory, but in any reasonable implementation (you do not want to make a syscall for every malloc and free) is mostly user space, and studying that will tell you a lot about how your malloc works.
reply
arbor-group 3 minutes ago
[flagged]
reply
amelius 2 hours ago
If the DRAM scarcity continues, not much.
reply
xnickb 2 hours ago
It's a consumer market scarcity. Since you're supposed to run everything on rented hardware, own nothing and be happy, I'd expect the malloc to be the right place to invest into once it is IPO.
reply