What is malloc doing, anyway?

(benmandrew.com)

50 points | by benmandrew 2 hours ago

4 comments

  • assumed_throwaw 1 hour 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.
    • nialv7 56 minutes ago
      the stack is just a bump allocator. the unique thing about stack-only languages is lifetime management, not really how they allocate.
      • saagarjha 26 minutes ago
        A stack only language that called into malloc to service allocations would not be very interesting.
  • wpmaven 23 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...
    • benmandrew 13 minutes ago
      That's a great reference, thanks!
  • TZubiri 22 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

  • amelius 1 hour ago
    If the DRAM scarcity continues, not much.
    • xnickb 29 minutes 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.