Earlier quoted context omitted.
I suppose by default it will just call free, but you can substitute different malloc implementations, maybe one with a malloc_no_header function
Because free still exists and there is no “allocate this number of bytes of memory; I promise to free it with a call to free_size ” call, you can’t have a malloc implementation that doesn’t track block sizes. I think the only thing free_size adds is robustness. Allocators can check the passed in size with what they know and abort the program if they do not match. That can thwart some security issues.
Why malloc always does more than I asked for?
41–50 of 52 posts
Re: Why malloc always does more than I asked for?
#42What if I want to allocate only 4 bits? I bet this is better optimized in Rust.
Re: Why malloc always does more than I asked for?
#43Old 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.
These still exist, like dlmalloc, jemalloc, tmalloc (which has new features). Built-in malloc is good enough for almost everything if you don't have excessive time on your hands, but thinking deeper about memory allocation is one of those things that can make your software higher quality if you have time for it—just like utilising SIMD (also on today's front page) or careful use of caching.
Third party allocators improved on this as well as usually avoiding the heap fragmentation problems that system heaps often suffered from back then.
These days the system heaps have improved to a point where you really only need to think about a third party heap for very specialized circumstances. They do usually come with nifty debugging tools though.
Re: Why malloc always does more than I asked for?
#44Re: Why malloc always does more than I asked for?
#45> 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 val…
> So all malloc implementations have a minimum alignment guarantee. Typically 16 bytes these days on x86 Fun story. Back in the early 2000s, my Delphi OpenGL program randomly crashed when I tried to use vertex buffers. I spent a lot of time checking this and that, before I realized it might be an alignment issue. Sure enough, Delphi memory allocator at the time only provided 4-byte alignment, while NVIDIA's drivers u…
Re: Why malloc always does more than I asked for?
#46> 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 val…
Re: Why malloc always does more than I asked for?
#47Your 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
Re: Why malloc always does more than I asked for?
#48What if I want to allocate only 4 bits? I bet this is better optimized in Rust.
You mean 4 bytes? You can't even address 4 bits. Small allocations can be handled by a slab allocator. Most likely implemented via a free list. A general purpose malloc should have that feature. TBH, I would consider allocating such tiny amounts of memory as a programmer error.
> You can't even address 4 bits.
This is not true, you can use a pointer plus extra offset data, etc.
Re: Why malloc always does more than I asked for?
#49Earlier quoted context omitted.
> So all malloc implementations have a minimum alignment guarantee. Typically 16 bytes these days on x86 Fun story. Back in the early 2000s, my Delphi OpenGL program randomly crashed when I tried to use vertex buffers. I spent a lot of time checking this and that, before I realized it might be an alignment issue. Sure enough, Delphi memory allocator at the time only provided 4-byte alignment, while NVIDIA's drivers u…
IMO that sounds either like a bug in Nvidia's drivers (IIRC OpenGL doesn't require that sort of alignment) or -more likely- your code having some other memory related-bug (e.g. accessing data out of bounds) that the extra bytes you got from alignment masked it.
However Microsoft's malloc at the time was indeed returning 16-byte aligned buffers so they probably didn't notice it in internal testing.
Re: Why malloc always does more than I asked for?
#50Earlier quoted context omitted.
These still exist, like dlmalloc, jemalloc, tmalloc (which has new features). Built-in malloc is good enough for almost everything if you don't have excessive time on your hands, but thinking deeper about memory allocation is one of those things that can make your software higher quality if you have time for it—just like utilising SIMD (also on today's front page) or careful use of caching.
Alternative allocators could make a huge difference to the runtime performance. When multithreading on real multiprocessor systems started to become a thing it was common that all your carefully written threads ended up waiting on the allocator mutex. The win32 heap was particularly prone to this. Third party allocators improved on this as well as usually avoiding the heap fragmentation problems that system heaps oft…