Saturday, 14 September 2013

Will this leak?

Will this leak?

calculate2 essentially does matrix calculation based on neighbors. I
haven't written C in a while and I was wondering if the memcpy at every
iteration was going to be a problem for memory or if I should free the
tmpMatrix after every k iteration before doing a new memcpy?
void transform2(int *pMatrix, int iteration)
{
if(iteration == 0)
return;
int fullLength = MATRIX_DIM * MATRIX_DIM;
int tmpMatrix[fullLength];
int start;
int row;
int col;
for(start = 0; start < iteration ; start++)
{
memcpy(tmpMatrix, pMatrix, sizeof(pMatrix[0]) * (fullLength));
for(row = 0; row < MATRIX_DIM ; row++)
{
for(col = 0; col < MATRIX_DIM ; col++)
{
int res = calculate2(pMatrix, tmpMatrix, row, col , iteration);
set_at(pMatrix, res, row, col);
}
}
}
}
Also, I'm open to suggestions if you guys think there's a cleaner way of
handling this. Essentially the tmpMatrix is the previous matrix at
iteration-1.
P.S pMatrix is a global int *_Matrix declaration and I use free() at the
end of my main for that one.

No comments:

Post a Comment