Your code says malloc(fsize)
but what you want is malloc(fsize * sizeof(size_t))
. The malloc
function takes the number of bytes to allocate. It has no way to know that you are going to use the memory to store size_t
's.
Update: I may have misunderstood the question. I'll update this answer when the questioner answers the questions posed in my comment above.