Logic Error? (Coding Blender)

I’m trying to add fuctionality to the build modifier, and I’m running into a problem. I think it’s where I copy from the source mesh into the new mesh.

(This is in modifier.c in the function buildModifier_applyModifier)

I want to change the code that copies the mesh in one big block

ndlm->mface = MEM_mallocN(sizeof(*ndlm->mface)*ndlm->totface, "build_mf");
memcpy(ndlm->mface, mface, sizeof(*mface)*ndlm->totface);

to copy the mesh one face at a time, so that I can re-order the faces
I’m using the following code: (sorry for the messy comments)

ndlm->mface = MEM_mallocN(sizeof(*ndlm->totfaces, "build_mf");
for (i=0; i<ndlm->totface; i++)
{
   void * target = ndlm->mface + (sizeof(*ndlm->mface)*i); /* start of the target mesh, offset by the size of a face * the number of faces */
   void * source = mface + (sizeof(*mface) * i); /*same as above, only with for source mesh */
   memcpy(target, source, sizeof(*mface));
}

If I use totface as the counter, the program crashes, sometimes after the first frame, some times after several frames. If i use ndlm->totface, it just freezes part way through the first frame. I think it might be an overflow error. But my real question is if this is even the right (read legal) way to copy the data, or if there’s
(1) a problem with how I get the indivdial face addresses
(2) a better way of doing it altogether