logoalt Hacker News

Why malloc always does more than I asked for?

42 pointsby nathaah3last Monday at 11:50 AM33 commentsview on HN

Comments

phiretoday at 6:56 AM

> so alloc() doesn’t just need to hand back a pointer. it needs to hand back a pointer that’s correctly aligned for whatever type the caller is about to store there.

Malloc doesn't know the required alignment (because has no idea what the type is, everything is cast through void). So all malloc implementations have a minimum alignment guarantee. Typically 16 bytes these days on x86, as that means even 128bit SSE values will end up aligned by default.

You couldn't go below the sizeof(void ) anyway, the backpointer needs to aligned too.

The padding only happens when you use memalign or aligned_malloc to specify a much larger alignment.

show 1 reply
CodesInChaostoday at 7:14 AM

Your bump allocator suffers from integer overflows turned into buffer overflows when the requested allocation is big enough:

  if (a->cursor + size > a->limit) return NULL; // out of memory
I'd rewrite it like this:

  if (size > a->limit - a->cursor) return NULL; // out of memory
show 1 reply
lexicalitytoday at 7:44 AM

I'm a little confused. We start with

  [ Header ][ ...variable padding... ][ Back Pointer ][ User Memory ]
                                       ^ always exactly sizeof(void*)
                                         bytes before User Memory,
                                         no matter how much padding
                                         came before it
and then it says we don't need to align the back pointer and we end up with

  [ Header ][ Back Pointer ][ Padding ][ User Memory ]
without a clear explanation of how we now get to the back pointer if it's behind the variable alignment.
show 1 reply
drivebyhootingtoday at 7:05 AM

Why bother with dynamic padding and a back pointer? That wastes at least 8 bytes.

You might as well always align to 8 bytes and make your header a multiple of 8.

show 1 reply
pjmlptoday at 8:42 AM

Old magazines like The C/C++ Users' Journal and DDJ used to have ads for companies selling malloc()/free() replacement libraries, exactly because a single implementation isn't adequate to all scenarios.

ncrucestoday at 9:00 AM

I wrote this bump malloc that I use for very short lived Wasm modules: https://github.com/ncruces/wasm2go/blob/main/libc-gen/c/mall...

ameliustoday at 10:33 AM

What if I want to allocate only 4 bits?

I bet this is better optimized in Rust.

iLoveOncalltoday at 9:17 AM

Having to recode malloc and free from "scratch" in school was a great teaching experience and there's not a month since where I don't encounter something that I understand better thanks to that exercise.

irenaeuslast Monday at 12:09 PM

I've read some allocator walkthroughs before but I thought that this line stood out:

"to get individual free() working, the allocator needs to remember something about every allocation it handed out. and that’s the moment metadata stops being optional."

That's just a very nice distillation of an important concept.

show 4 replies