How to define a dynamic nested array in MQL5?

 

I need to allocate dynamically an  int  nested array like:  [[1, 2], [3], [4, 5, 6]]

I tried with the following syntax, but it doesn't work:

   int arr[][];  // [[1, 2], [3], [4, 5, 6]]
   ArrayResize(arr, 3);
   ArrayResize(arr[0], 2);
   ArrayResize(arr[1], 1);
   ArrayResize(arr[2], 3);


I also tried to set the primary dimension, but it also doesn't work.

   int arr[3][];
   ArrayResize(arr[0], 2);
   ArrayResize(arr[1], 1);
   ArrayResize(arr[2], 3);

A statically defined array works, but I need a dynamic one.

Please show an example of a dynamically allocated nested array or a matrix (rectangular array). I need the array in  MQL5 to store indicator handlers, which are  int s.

 
Miroslav Popov: I tried with the following syntax, but it doesn't work:
  1. Of course, it doesn't. You can only resize the first index. All others are fixed.
  2. Create an array of a struct that contains an int array.
 

Or use CMult3D class:

template<typename T>
class CMultyVal
  {
private:
   T                 m_value;        // the one dimension array corresponding the multi dim array
public:
                     CMultyVal();
   void              operator =(const T val)
     {m_value=val;}
   T                 Value() {return m_value;}
  };
template<typename T> CMultyVal::CMultyVal()
  {
   m_value=NULL;
  }
template<typename T>
class CMultyArr
  {
private:
   CMultyVal<T>      *m_arr[];
public:
                     CMultyArr(const int dim);
                    ~CMultyArr();
   //T                 operator[](const int ind){return(m_arr[ind]);}
   CMultyVal<T> *    operator[](const int ind)
                     {return(m_arr[ind]);}
  };
template<typename T> CMultyArr::CMultyArr(const int dim)
  {
   ArrayResize(m_arr,dim);
   for(int i=0; i<dim; i++)
      m_arr[i]=new CMultyVal<T>();
  }
template<typename T> CMultyArr::~CMultyArr()
  {
   for(int i=0; i<ArraySize(m_arr); i++)
      delete m_arr[i];
   ArrayFree(m_arr);
  }
template<typename T>
class CMultLevel
  {
private:
   CMultyArr<T>      *m_arr[];
public:
                     CMultLevel(const int dim1,const int dim2);
                    ~CMultLevel();
   //T                 operator[](const int ind){return(m_arr[ind]);}
   CMultyArr<T> *    operator[](const int ind)
                     {return(m_arr[ind]);}
  };
template<typename T> CMultLevel::CMultLevel(const int dim1,const int dim2)
  {
   ArrayResize(m_arr,dim1);
   for(int i=0; i<dim1; i++)
      m_arr[i]=new CMultyArr<T>(dim2);
  }

template<typename T> CMultLevel::~CMultLevel()
  {
   for(int i=0; i<ArraySize(m_arr); i++)
      delete m_arr[i];
   ArrayFree(m_arr);
  }
template<typename T>
class CMult3D
  {
private:
   CMultLevel<T>     *m_arr[];
public:
                     CMult3D(const int dim1,const int dim2,const int dim3);
                     ~CMult3D();
   //T                 operator[](const int ind){return(m_arr[ind]);}
   CMultLevel<T> *    operator[](const int ind)
                     {return(m_arr[ind]);}
  };
template<typename T> CMult3D::CMult3D(const int dim1,const int dim2,const int dim3)
  {
   ArrayResize(m_arr,dim1);
   for(int i=0; i<dim1; i++)
      m_arr[i]=new CMultLevel<T>(dim2,dim3);
  }
template<typename T> CMult3D::~CMult3D()
  {
   for(int i=0; i<ArraySize(m_arr); i++)
      delete m_arr[i];
   ArrayFree(m_arr);
  }

use

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   CMult3D<int> b(3,2,4);
   for(int l1=0; l1<3; l1++)
      for(int l2=0; l2<2; l2++)
         for(int l3=0; l3<4; l3++)
            b[l1][l2][l3]=l1+l2+l3;
   for(int l1=0; l1<3; l1++)
      for(int l2=0; l2<2; l2++)
         for(int l3=0; l3<4; l3++)
            Print("IND ",l1,",",l2,",",l3,"=",b[l1][l2][l3].Value());
  }

result


 
Miroslav Popov:

I need to allocate dynamically an  int  nested array like:  [[1, 2], [3], [4, 5, 6]]

I tried with the following syntax, but it doesn't work:


I also tried to set the primary dimension, but it also doesn't work.

A statically defined array works, but I need a dynamic one.

Please show an example of a dynamically allocated nested array or a matrix (rectangular array). I need the array in  MQL5 to store indicator handlers, which are  int s.

Actually reading your post again, if what you need is just storing handles of indicators, you can use 1D array and calculate the index, like that:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int dim1=3,dim2=2,dim3=4;
   int arr[];
   ArrayResize(arr,dim1*dim2*dim3);
   for(int l1=0; l1<dim1; l1++)
      for(int l2=0; l2<dim2; l2++)
         for(int l3=0; l3<dim3; l3++)
            arr[(l1*dim2*dim3+l2*dim3+l3)]=l1+l2+l3;
   for(int l1=0; l1<dim1; l1++)
      for(int l2=0; l2<dim2; l2++)
         for(int l3=0; l3<dim3; l3++)
            Print("(",l1,",",l2,",",l3,")",arr[(l1*dim2*dim3+l2*dim3+l3)]);
  }