A C allocator can't compact, because the program holds the addresses
Another follow-up: why not slide every live block down and end fragmentation for good?
A C program holds raw addresses, not handles, so nothing can update them after a move.
Moving every live block down to the start of the arena, compaction, would turn all the free space into one run. A C allocator can't do it, because the program holds the addresses. A pointer to a block can sit in any variable, struct or register, and the allocator can't find them all to update them. In the figure, a pointer that still holds 4 for B now points into C.
So a C allocator never moves a live block on its own. realloc moves one only when asked, and hands back the new address: "The value of realloc is the new address of the block." CPython makes the same promise about objects. id() is "guaranteed to be unique and constant for this object during its lifetime", and the same docs say that in CPython it's the address of the object in memory.
Compaction works when every reference goes through something the allocator controls. Give callers a handle, a block id, and keep a table from id to address. Then compaction slides each live block down, updates the table, and leaves one free run. It costs a copy of every live unit plus one table update per block. The program has to be stopped or locked out while it runs.
compact(table, size) compacts an arena whose callers hold handles: table maps each handle to (address, n), as in the figure. Write it to slide every block down in address order.