Memory management for matrices built with dynamic arrays I am working on C++ code which builds matrices (3D and 4D) using dynamic arrays.
I am using arrays instead of std::vector; as in the future, I might extend this code to some kind of multithreading or parallel process (e.g. CUDA).

In my code, I often initialize and define the source matrix in main.cpp.

Then I have a function (sometimes it may belong to a class) which inputs the matrix along with other variable and I need to return the modified matrix. Which is subsequently used in main.

This is what I usually work with in my code:

    #include &lt;iostream&gt;
    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    using namespace std;

    float ***matFunc ( ***mat3D, int n);

    int main()
    {
      // define dimensions of the matrix 
      int M , N, S = 3;

     //define and initialize the matrix
      float ***myMat = new float **[M];
         for (int i = 0; i&lt; M; i++ ) {
              myMat[i] = new float *[N];
              for ( int j = 0; j &lt; N; j++ ) {
                 myMat[i][j] = new float [S];
                    for (int k = 0; k &lt; S; k++ ) {
                      myMat[i][j[k] = someValue obtained or calculated 
                          }
                   }
              }

         // here I call my member function to perform addition        
         // This is also used if I have more than one matrices

        float ***newMat = matFunc( myMat, int n ); //returns modified matrix

         //deleting the original myMat

         for (int i = 0; i &lt; M; i++) {
             for (int j = 0; j &lt; N; j++) {
               delete[]myMat[i][j];
        }
            delete[] myMat[i];
      }
   delete[]myMat;

      return 0;
     }

     //function definition for matFunc

      float***matFunc(float ***mat, int n ){

       int p, q, r;
       int M, N, S = 3;

           float ***modifiedMat = new float **[M];
                for ( p = 0, p &lt; M; p++ ) {
                   modifiedMat[p] = new float *[N];
                     for ( q = 0; q &lt; n; q++ ) {
                         modifiedmat[p][q] = new float [S];
                          for (r  =0; r &lt; S; r++ ) {
                          modifiedMat[p][q][r] = mat[p][q][r] * do something
                         }
                        }
                      }

           return modifiedMat;
        }


As you can observe, I am always defining and initializing new matrices with help of dynamic arrays. This becomes more troublesome if matrix dimensions are to be increased. I use dynamic arrays due to their faster operations as compared to vectors. But I think there might be some memory management issues with code.  As I have observed it consumes more RAM and sometimes my IDE/compiler will complain lack of index or space for integer type.

Could you please suggest how I can improve on this code?
At first, you should give your program some structure.

Start by indenting all the lines consistently. Ideally, let a program do this automatically. Search for indenter or beautifier.

Then, define a class called Matrix, give it a proper constructor and destructor. This could already help with the memory leak.

Or if you don't like classes, at least define an allocation and a deallocation function for a matrix. But then, it's almost a class anyway.

Finally, fix the syntax errors in your code so that it compiles at least.

Make the code in main so simple to read that you can explain it to a layman. That is, no explicit memory allocation, no details, just the high level view. All the rest of the code needs to go to separate functions.