title: Dynamic allocation in function C++
body: <p>i have some trouble in dynamic allocation with 'new' and reference. Please see a simple code below.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  void allocer(int *pt, int *pt2);
  int main()
  {
      int num = 3;
      int num2 = 7;
      int *pt=&amp;num;
      int *pt2 = &amp;num2;
      allocer(pt, pt2);
      cout &lt;&lt; "1. *pt= " &lt;&lt; *pt &lt;&lt; "   *pt2= " &lt;&lt; *pt2 &lt;&lt; endl;
      cout &lt;&lt; "2. pt[0]= " &lt;&lt; pt[0] &lt;&lt; "   pt[1]= " &lt;&lt; pt[1] &lt;&lt; endl;
  
  }
  
  
  void allocer(int *pt, int *pt2)
  {
      int temp;
      temp = *pt;
      pt = new int[2];
      pt[0] = *pt2;
      pt[1] = temp;
      cout &lt;&lt; "3. pt[0]= " &lt;&lt; pt[0] &lt;&lt; "   pt[1]= " &lt;&lt; pt[1] &lt;&lt; endl;
  }
  </code></pre>
  
  <p>What i want to do is to make the function 'allocer' get 2 arguments which are the int pointer and allocate memory on one of them. As you can see, *pt becomes an array to take 2 integers. Inside of the function it works well which means the sentence that i marked 3. printed as what i intended. However, 1, 2 doesn't work. 1 prints the original datas(*pt= 3, *pt2= 7), 2 prints error(*pt= 3, *pt2= -81203841).
  How to solve it?</p>