title: minimizing memory use in c++
body: <p>I was working on a problem in codeforces and I have no problems in the functionality of the code but the code exceeds the memory usage. Can someone explain how to improve this? How to learn about memory management in general while programming because I didn't find anything related to that. Here's my code :</p>
  
  <p><strong>Summary of the problem:</strong> You are given 6 input numbers, the first 5 numbers should be multiplied each by another integer and the summation of them after multiplication is the <strong>sixth</strong> integer in the input. You should find all the combinations of the numbers the can be multiplied by each value in the input to seek the summation. the output is basically the sum of the integers chosen for each number in the input to be multiplied by.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt; 
  
  #ifndef MAX
  #define MAX 100
  #endif
  
  using namespace std;
  
  void storeAndFilter(vector &lt;int&gt; &amp;arr,int chosenNumConst, int mypasha);
  void print(vector&lt;int&gt; &amp;v);
  int printsum(vector&lt;int&gt; &amp;v);
  
  
  int main(int argc, char const *argv[])
  {
      //array of input options
      int a1, a2, a3, a4, a5, pasha;
      cin &gt;&gt; a1 &gt;&gt; a2 &gt;&gt; a3 &gt;&gt; a4 &gt;&gt; a5 &gt;&gt; pasha;
  
      //declarations of vectors
      vector&lt;int&gt; arrStrA1;
      vector&lt;int&gt; arrStrA2;
      vector&lt;int&gt; arrStrA3;
      vector&lt;int&gt; arrStrA4;
      vector&lt;int&gt; arrStrA5;
  
      //sorting and filtering the vectors
      storeAndFilter(arrStrA1,a1,pasha);
      storeAndFilter(arrStrA2,a2,pasha);
      storeAndFilter(arrStrA3,a3,pasha);
      storeAndFilter(arrStrA4,a4,pasha);
      storeAndFilter(arrStrA5,a5,pasha);
  
      //cout&lt;&lt;"All Posibilities valid (Minimized by removing values &lt; pasha) : "&lt;&lt;endl;
      // print (arrStrA1);
      // print (arrStrA2);
      // print (arrStrA3);
      // print (arrStrA4);
      // print (arrStrA5);
  
      //scores vectors
      vector&lt;int&gt; resultsA1;
      vector&lt;int&gt; resultsA2;
      vector&lt;int&gt; resultsA3;
      vector&lt;int&gt; resultsA4;
      vector&lt;int&gt; resultsA5;
  
  
      int i,j,k,l,m;
      for (i=0; i &lt; (int)arrStrA1.size(); ++i)
      {
          for (j=0; j &lt; (int)arrStrA2.size(); ++j)
          {
              for (k=0; k &lt; (int)arrStrA3.size(); ++k)
              {
                  for (l=0; l &lt; (int)arrStrA4.size(); ++l)
                  {
                      for (m=0; m &lt; (int)arrStrA5.size(); ++m)
                      {
                          if(arrStrA1.at(i)+arrStrA2.at(j)+arrStrA3.at(k)+arrStrA4.at(l)+arrStrA5.at(m)==pasha)
                          { 
  
                              resultsA1.push_back(arrStrA1.at(i));
                              resultsA2.push_back(arrStrA2.at(j));
                              resultsA3.push_back(arrStrA3.at(k));
                              resultsA4.push_back(arrStrA4.at(l));
                              resultsA5.push_back(arrStrA5.at(m));
  
  
                          }
  
                      }   
                  }   
              }   
          }
      }
  
  
  
      //divise each term by the card value
      for (int i = 0; i &lt; (int)resultsA1.size(); ++i)
      {
          if (a1==0)
              resultsA1.at(i) /= 1;
          else
              resultsA1.at(i) /= a1;
      }
      for (int i = 0; i &lt; (int)resultsA2.size(); ++i)
      {
          if (a2==0)
              resultsA2.at(i) /= 1;
          else
              resultsA2.at(i) /= a2;
  
      }
      for (int i = 0; i &lt; (int)resultsA3.size(); ++i)
      {
          if(a3==0)
              resultsA3.at(i) /= 1;
          else
              resultsA3.at(i) /= a3;
      }
      for (int i = 0; i &lt; (int)resultsA4.size(); ++i)
      {
          if (a4==0)
              resultsA4.at(i) /= 1;
          else        
              resultsA4.at(i) /= a4;
      }
      for (int i = 0; i &lt; (int)resultsA5.size(); ++i)
      {
          if(a5==0)
              resultsA5.at(i) /= 1;
          else
              resultsA5.at(i) /= a5;
      }
  
      //Uncomment to show the table list after division
      // print(resultsA1);
      // print(resultsA2);
      // print(resultsA3);
      // print(resultsA4);
      // print(resultsA5);
  
      int scra1=printsum(resultsA1);
      int scra2=printsum(resultsA2);
      int scra3=printsum(resultsA3);
      int scra4=printsum(resultsA4);
      int scra5=printsum(resultsA5);
  
      cout &lt;&lt; scra1 &lt;&lt;" "&lt;&lt; scra2 &lt;&lt;" "&lt;&lt; scra3 &lt;&lt;" "&lt;&lt;scra4 &lt;&lt;" "&lt;&lt; scra5 &lt;&lt;endl;
  
  
      return 0;
  }
  
  
  void print(vector&lt;int&gt; &amp;v)
  {
      int size = v.size();
      cout&lt;&lt;"========================"&lt;&lt;endl;
      for (int i = 0; i &lt; size; ++i)
          cout&lt;&lt;v.at(i)&lt;&lt;endl;
      cout&lt;&lt;"========================"&lt;&lt;endl;
  
  }
  
  int printsum(vector&lt;int&gt; &amp;v)
  {
      int sum =0;
      for (int i = 0; i &lt; (int)v.size(); ++i)
          sum += v.at(i);
      return sum;
  }
  
  void storeAndFilter(vector &lt;int&gt; &amp;arr,int chosenNumConst, int mypasha)
  {
      arr.reserve(10);
      int i=0;
  
      for (; i &lt;= MAX; ++i)
      {
          arr.push_back(i*chosenNumConst);
          if (arr.at(i)&gt;mypasha)  
              break;
      }
  
      arr.resize(i);
  }
  
  </code></pre>
  
  <p>Some stuff that I thought about:</p>
  
  <ul>
  <li>Arrays instead of Vectors maybe better</li>
  <li>The nested for loops may be the one that is taking too much memory</li>
  </ul>
  
  <p>But to be clear, the nested for loops doesn't make too much calculations, they find all the combinations of 5 numbers '5 loops' to sum to a specific value. Filtering before entering the loop is applied so maybe the nested loop isn't the issue. 
  Max memory constrain in the problem is: <strong>256 MB</strong></p>