I just learned how to correctly return an array, Return a 2d array from a function, but how can I release the memory of a returned array used on the fly if I want to do something like the following
dMAT=matmul(DIM,transpose(DIM,EState),dMAT);
where both Matmul and Transpose are user-defined functions returning a 2D array. Since now Matmul and Transpose are used on the fly, I don't have a handler towards the returned arrays and thus cannot release the memory in the said method.
EDIT:
An example of Matmul and Transpose are given below,
double **transpose(int & dim, double ** mat){
double **mat1 = new double *[dim];
for(int i=0; i<dim; i++) {
mat1[i] = new double [dim];
for(int j=0; j<dim; j++) mat1[i][j] = mat[j][i];
}
return mat1;
}
double **matmul(int & dim, double ** mat1, double **mat2){
double **mat3 = new double *[dim];
for(int i=0; i<dim; i++) {
mat3[i] = new double [dim];
for(int j=0; j<dim; j++) {
mat3[i][j]=0.0;
for(int k=0; k<dim; k++) mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
return mat3;
}