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 <iostream>    #include <stdio.h>    #include <stdlib.h>    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< M; i++ ) {              myMat[i] = new float *[N];              for ( int j = 0; j < N; j++ ) {                 myMat[i][j] = new float [S];                    for (int k = 0; k < 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 < M; i++) {             for (int j = 0; j < 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 < M; p++ ) {                   modifiedMat[p] = new float *[N];                     for ( q = 0; q < n; q++ ) {                         modifiedmat[p][q] = new float [S];                          for (r  =0; r < 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?
Memory management for matrices built with dynamic arrays
c++;array;memory management