title: How to allocate memory for specific number of strings?
body: <p>I was given the task to program something like a dictionary, and the way I am allocating memory for the meanings is just to allocate for 100 meanings in the constructor, which works perfectly fine.</p>
  
  <p>However, the professor didn't approve that and he asked me to rewrite the code in a way that I allocate memory for a relevant number of meanings. I basically have no idea how to do that, how the constructor will know in advance how many meanings I will have?</p>
  
  <p>What would you guys suggest? I post just part of the code, which is relevant for the problem.</p>
  
  <pre><code>#include"expression.h"
  
  //---------------------METHODS-------------------------------------------
  
  Expression::Expression(int m_ctr)
  {
      count_meanings = m_ctr; // Set the counter to 0
      meanings = new char * [100]; // Allocate memory for 100 meanings
  }
  
  Expression::~Expression()
  {
      delete [] meanings; // Free the allocated memory
      delete [] word_with_several_meanings; // Free the allocated memory
  }
  
  void Expression::word(char *p2c)
  {
      word_with_several_meanings = new char[strlen(p2c)+1];
      strcpy(word_with_several_meanings, p2c); // copy the string, method: DEEP copy
  }
  
  void Expression::add_meaning(char *p2c)
  {
      meanings[count_meanings] = new char[strlen(p2c)+1];
      strcpy(meanings[count_meanings++], p2c); // copy the string, method: DEEP copy
  }
  
  char * Expression::get_word()
  {
      return word_with_several_meanings;
  }
  
  char * Expression::get_meaning(int n_meaning)
  {
      return * (meanings + n_meaning);
  }
  
  int Expression::get_total_number_of_meanings()
  {
      return count_meanings;
  }
  
  int main(void)
  {
      Expression expr;
  
      expr.word("bank");
      expr.add_meaning("a place to get money from");
      expr.add_meaning("a place to sit");
  
      cout &lt;&lt; expr.get_word() &lt;&lt; endl;
  
      for(int i = 0; i&lt;expr.get_total_number_of_meanings(); i++)
          cout &lt;&lt; " " &lt;&lt; expr.get_meaning(i) &lt;&lt; endl;
  </code></pre>