c++ - Memory leak when calculating a determinant of a matrix -
while writing code calculate determinant of simple 3 x 3 matrix, noticed started accumulate memory leaks. i've reduced method following (meaning no longer use algorithm determine size of matrix, "by hand"):
double determinant(double** &m) { return m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); } i can't find problem this, since not allocate memory inside method. if change , return double (return 1.0, example) leak gone. happening here?
edit (more code):
double logmultivariatenormaldensity(unsigned char* &x, unsigned char* &mean, double** &sigma) { double det = determinant(sigma); ... } which in turn called inside loop
for(unsigned int = 0; < n; i++) { logmultivariatenormaldensity(_x[i], _mean[i], _sigma[i]) } being 2d array, _sigma allocated using malloc (in both dimensions).
memory allocated malloc must freed free. always, small amount of memory.
Comments
Post a Comment