What is the fastest way to record the names of each file in a directory?
Calling realloc in every iteration makes the code really slow. Algorithmically it is in \$\mathcal O(n^2)\$ since realloc already has complexity \$\mathcal O(n)\$. Plus, you are calling it interleaved with malloc for the name of the directory entries.
The performance of your program depends very much on the specific memory allocator. It might be very fast or very slow, probably somewhere between \$n\$ and \$n^2\$.
To fix this, you should use the well-known pattern of having a triplet (pointer, length, capacity) to represent the growing buffer of directory entries.
Ideally you define a separate struct for this, together with an append function. That way, the function that reads the directory entries doesn't have to fiddle with realloc directly.