0

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;

 }
bsmile
  • 59
  • 8

4 Answers4

1

Use std::vector; it will save you a lot of trouble.

In your code, there is no way to release memory without having a temporary variable on which you can call delete. std::vector (and other RAII wrappers) will help you by managing the lifetime of the resources (here the memory).

Using C-style coding (manual memory management with new and delete) is not the way to write code in C++: it's hard and you will write buggy code. Learn good C++ coding style (RAII, ...) from the beginning.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
nefas
  • 1,120
  • 7
  • 16
1

you can make something like that:

double *f(const int& size) 
{
   double *arr = new double[size];
   for (int i=0;i<size;i++)
       arr[i] = i;

   return arr;
}

int main()
{
  double *a = f(10);
  delete []a;
}

it is not for 2d array but I think you understand main idea

Ivan Sheihets
  • 802
  • 8
  • 17
1

You use a class, which encapsulates the allocation and freeing.

Something like

class matrix {
    std::vector<std::vector<double>> mat;
public:
    matrix(int dim) : mat(dim, std::vector(std::vector(dim))) {}
    // Other members, e.g. transpose, operator+ and operator*
};

Or, rather than writing it yourself, you use a library like Eigen that has written it for you.

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

I just learned how to correctly return an array

Since it is not possible to return an array by value, it will be useful to be precise: You've learned how to return a pointer that holds the address of an object that is an element of an array.

how can I release the memory of a returned array

You allocated the memory by calling new[]. Therefore you must release the memory by calling delete[]. delete[] is used by passing the address returned by new[] as an argument.

Since you wish to use the pointer not only for releasing the memory, but also to pass it to another function matmul (or sometimes matmul?), you will want to store the pointer in a variable so that you can copy it:

auto arr = transpose(DIM,EState);
dMAT = matmul(DIM,arr,dMAT);
// now, you can release the memory pointed by arr

However, don't shoot yourself in the foot by storing dynamic memory in bare pointers. Use RAII containers such as std::vector instead.

eerorika
  • 232,697
  • 12
  • 197
  • 326