malloc: Secret Memory Dealer in Your Code
https://www.youtube.com/watch?v=kcsVhdHKupQ Every time you call malloc, something happens that most programmers never think about. You asked for a hundred bytes. You got a pointer. But where did th...

Source: DEV Community
https://www.youtube.com/watch?v=kcsVhdHKupQ Every time you call malloc, something happens that most programmers never think about. You asked for a hundred bytes. You got a pointer. But where did those bytes come from? And what happens when you give them back? The Heap: Your Program's Scratch Space Stack variables are easy. Allocate on function entry, gone on return. But malloc hands you memory from a completely different region β the heap. A giant pool where blocks get allocated and freed in any order. Here's what most people miss: malloc doesn't just return a pointer to your data. It secretly stashes a header right before your allocation β a metadata block recording the size. That's how free knows how many bytes to reclaim. You never see it. But it's always there, eating a few extra bytes on every single allocation. Where does the heap itself come from? On Linux, malloc calls one of two syscalls: brk β pushes the heap boundary forward. Used for small allocations. mmap β grabs an entir