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.
replysmashspectacle 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.
replyjacquesm 28 minutes ago
Most stack oriented languages also have a heap.
replynialv7 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.
replysaagarjha 2 hours ago
A stack only language that called into malloc to service allocations would not be very interesting.
replywpmaven 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...
replyTZubiri 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().
replyyou 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
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.
replystevekemp 5 minutes ago
Depending on how low-level you're going, you don't even need to call brk.
replyMy 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.