Conversation

In malloc-ng, does realloc to a smaller size return a different pointer? (If you want to be pedantic I am asking about returning a pointer with a different representation, provenance is always different but I'm not asking about that .)
2
The question I was really interested in was whether realloc to a smaller size can return NULL, and I thought that the answer to that was so clearly “obviously not” that I didn't dare ask it, but it seems it is a situation that can arise?
3
3
Since even munmap can fail, realloc to a smaller size can fail for large allocations in any allocator. It's up to them if they report it, but I expect most report it. Most modern allocators use slab allocation. If the size class changes, they need to allocate and copy to that.
2
1
Replying to and
I wonder how many C developers believe that realloc to a smaller size will never fail. I was one of them this morning. (I do check the result, but it is in preparing the explanation “yeah it's boilerplate but you know…” to go with it that I ended up wondering about it.)
1
3
It can fail even with dlmalloc/glibc if you go from an allocation they manage as a separate region to a smaller size they're willing to manage via free lists within regions. They still essentially have 2 types of size classes and glibc determines the approach dynamically.
1
1
The jemalloc approach is to use a certain number of size classes per doubling of size. It puts a low bound on space wasted from size class rounding. It also means you can only have 4 actual reallocations to double in size even if the calling code keeps incrementing by 1 element.
1
The same size classes are used by hardened_malloc and I think mimalloc uses the same approach with more size classes per doubling of size. When the size class changes, including shrinking, it needs to be moved to a new allocation in a different slab since slabs are 1 size class.