1 hour ago · Tech · hide · 0 comments

I read this article via Hacker News, where Senthilnathan explains why, if you want to allocate 13 bytes, it allocates more behind the scenes. He writes that what you end up with, after running malloc, is this: +----------------+ | Header | +----------------+ | Variable | | padding | +----------------+ | Back pointer | +----------------+ | User memory | <- Pointer returned by malloc +----------------+ But this, I have to point out, is his own implementation, which he shows in the article. Glibc uses a different implementation. I wrote a memory allocator back in 2013 which used yet a different layout. In short; Senthil creates a header (basically the size of request), a variable padding (which is not properly explained), and a back pointer that points back to the header. So when the memory is freed, free() can take the pointer (user memory), go one step back and use the back pointer to find the pointer to the original header that contains the size of the block, including the size of the…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.