removing memory leaks in c++ and GNU scientific library code
double a[] = { 0.11, 0.12, 0.13,
0.21, 0.22, 0.23 };
double b[] = { 1011, 1012,
1021, 1022,
1031, 1032 };
double c[] = { 0.00, 0.00,
0.00, 0.00 };
gsl_matrix_view A = gsl_matrix_view_array(a, 2, 3);
gsl_matrix_view B = gsl_matrix_view_array(b, 3, 2);
gsl_matrix_view 开发者_StackOverflow中文版C = gsl_matrix_view_array(c, 2, 2);
/* Compute C = A B */
gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
1.0, &A.matrix, &B.matrix,
0.0, &C.matrix);
how do I deallocate the memory assigned to the matrices?
The compiler will take care of those matrices. Unless you use malloc()/new[]
or any function that uses malloc()/new[]
and gives you ownership of the allocated memory there're no chances to leak memory.
If you asked about gsl_matrix_view_array() - the documentation says that the return value is a pointer to a view in the original matrix which means that no extra matrix is allocated - you only get a pointer into the same matrix. So unless you used malloc()/new
to allocate the original matrix you shouldn't do anything. If you use malloc()/new[]
for the original matrix (not your case, but anyway) - call free()/delete[]
on the original matrix, not on a view.
精彩评论