title: How to create dynamic 2D array of different size strings
body: <p>I want to read a .txt file.</p>
  
  <p>.txt file will have N-rows and M-cols.</p>
  
  <p>Each word present in the txt file will have varying length.</p>
  
  <p><strong>Sample txt file:</strong></p>
  
  <pre><code>Suppose N = 4 rows
  
  Suppose M = 5 cols
  </code></pre>
  
  <p><strong><em>content of txt file:</em></strong></p>
  
  <pre><code>aa bbb cc dddddddd eeee
  
  aa bbbbbbbbbbbb cc ddddddddddd eeee
  
  aaaaaaaaaa bb cc d e
  
  a b c d eeee
  </code></pre>
  
  <p><strong><em>What I have to do:</em></strong></p>
  
  <p>I have to store these strings into a 2D array of strings such that it looks like this:</p>
  
  <pre><code>arr[4][5] =  
  
  [aa             bbb              cc     dddddddd        eeee]
  
  [aa             bbbbbbbbbbbb     cc     ddddddddddd     eeee]
  
  [aaaaaaaaaa     bb               cc     d               e   ]
  
  [a              b                c      d               eeee]
  </code></pre>
  
  <p>I know how to create dynamic 2D array of integer and its working fine:</p>
  
  <pre><code>int** arr;
  int* temp;
  
  arr = (int**)malloc(row*sizeof(int*));
  temp = (int*)malloc(row * col * sizeof(int));
  for (int i = 0; i &lt; row; i++)
  {
      arr[i] = temp + (i * col);
  }
  int count = 0;
  //setting values in 2-D array
  for (int i = 0; i &lt; row; i++)
  {
      for (int j = 0; j &lt; col; j++)
      {
          arr[i][j] = count++;
      }
  }
  </code></pre>
  
  <p>But, when I am trying to do the same thing for strings, its crashing.</p>
  
  <pre><code>string** arr;
  string* temp;
  
  arr = (string**)malloc(row*sizeof(string*));
  temp = (string*)malloc(row * col * sizeof(string));
  for (int i = 0; i &lt; row; i++)
  {
      arr[i] = temp + (i * col);
  }
  
  //setting values in 2-D array
  for (int i = 0; i &lt; row; i++)
  {
      for (int j = 0; j &lt; col; j++)
      {
          arr[i][j].append("hello"); // CRASH here !!
      }
  }
  </code></pre>
  
  <p><strong>How to store each words in an array??</strong></p>
  
  <p>This is what I have written:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;map&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;algorithm&gt;
  #include &lt;assert.h&gt;     /* assert */
  using namespace std;
  
  vector&lt;string&gt; readFile(const string file, int&amp; row, int&amp; col)
  {
      vector&lt;string&gt; buffer;
  
      ifstream read(file);
      string line;
      char * writable = NULL;
  
      if (read.is_open())
      {
          int temp_counter = 0;
          while (!read.eof())
          {
              std::getline(read, line);
              writable = new char[line.size() + 1];
              std::copy(line.begin(), line.end(), writable);
              writable[line.size()] = '\0'; // don't forget the terminating 0
              if (temp_counter == 0)//
              {
                  row = std::stoi(line);
                  ++temp_counter;
              }
              else if (temp_counter == 1)
              {
                  col = std::stoi(line);
                  ++temp_counter;
              }
              else
              {
                  buffer.push_back(line);
              }       
          }
      }
      // don't forget to free the string after finished using it
      delete[] writable;
      return buffer;
  }
  
  void create2DDynamicArray(std::vector&lt;string&gt;&amp;v, int row, int col)
  {
      string** arr;
      string* temp;
  
      arr = (string**)malloc(row*sizeof(string*));
      temp = (string*)malloc(row * col * sizeof(string));
      for (int i = 0; i &lt; row; i++)
      {
          arr[i] = temp + (i * col);
      }
  
  
      //setting values in 2-D array
      for (int i = 0; i &lt; row; i++)
      {
          for (int j = 0; j &lt; col; j++)
          {
              arr[i][j].append("hello");
          }
      }
  }
  int main()
  {
      vector&lt;string&gt; myvector;
      int row=0;
      int col=0;
  
      myvector = readFile("D:\\input.txt", row, col);
      create2DDynamicArray(myvector, row, col);
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>txt file look like:</p>
  
  <p>4</p>
  
  <p>5</p>
  
  <p>aa bbb cc dddddddd eeee</p>
  
  <p>aa bbbbbbbbbbbb cc ddddddddddd eeee</p>
  
  <p>aaaaaaaaaa bb cc d e</p>
  
  <p>a b c d eeee</p>