title: Arbitrary Dimensional Array
body: <p>So I'm trying to create an n-dimensional array structure for use in a maze generating program.</p>
  
  <p>I've simplified my problem (for the purposes of trying to get the theory figured out before making it templatized and adding all the necessary helper functions)</p>
  
  <p>So my problem currently boils down to wanting to make an ArbitraryArray class that takes in an argument to its constructor specifying the number of dimensions. Each dimension will have length = 5. (for now)</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>class ArbitraryArray{
  public:
      int array[5];
      ArbitraryArray*subArray;
  
      ArbitraryArray(){}
  
      ArbitraryArray(int depth){
          if (depth == 2) subArray = new ArbitraryArray[5];
          else if (depth &gt; 2) for (int i = 0; i &lt; 5; i++) subArray = new ArbitraryArray(depth - 1);
      }
  };
  </code></pre>
  
  <p>And I'd create a 2 dimensional object like so:</p>
  
  <pre><code>ArbitraryArray testArray(2);
  </code></pre>
  
  <p>Or a 3 dimensional object like so:</p>
  
  <pre><code>ArbitraryArray testArray(3);
  </code></pre>
  
  <p>Problem is, when I tested it for depth = 3 and then tried to set an integer value, via:</p>
  
  <pre><code>testArray.subArray[3].subArray[4].array[4] = 7;
  </code></pre>
  
  <p>I received a runtime error, leading me to believe that I'm doing something wrong in how I allocate these objects dynamically.</p>
  
  <p>Also, I included an empty default constructor since that gets called by lines like:</p>
  
  <pre><code>subArray = new ArbitraryArray[5];
  </code></pre>
  
  <p>I'm aware this may not be the best way to go about creating an arbitrary dimensional array data structure, but I'd really like to figure out why this implementation is not working before potentially looking for better methods.</p>
  
  <p>Also I am aware I shouldn't have a line like:</p>
  
  <pre><code>int array[5];
  </code></pre>
  
  <p>And that it should be a pointer instead so that there isn't a ton of wasted memory allocation for all the levels of the array above the bottom dimension. And I intend to modify it to that after I get this basic idea working.</p>