title: Memory error in stack
body: <p>I'm writing a program to solve this problem:</p>
  
  <blockquote>
    <p>Stock stored containers with goods of various kinds of N . All
    containers are written in N stacks . In each stack may be containers
    with goods of any species ( stack can be initially empty) .</p>
    
    <p>Forklift can take any of the top container pile and put it on top of
    any stack . You must put all the containers with the product of the
    first kind in the first stack , the second type - the second stack ,
    etc.</p>
    
    <p>The program should print the sequence of actions forklift or a message
    stating that the problem has no solution .</p>
    
    <p>Input Format</p>
    
    <p>The first line of input contains a single positive integer N, does not
    exceed 500 . The next N lines describe the stack of containers , first
    recorded by ki - number of containers in the stack , and then ki
    properties - types of goods in containers in the stack from the bottom
    up . At the beginning of each stack of not more than 500 containers (
    containers during transport this restriction can be broken ) . Output
    data format The program should print the description of the truck
    action : for each action type two numbers - from which to take the
    container stacks and stack which put. ( Note that minimize the amount
    of truck operations is not required. ) If there is no solution , you
    need to display a single number 0 . If the containers are initially
    correctly placed into piles , the output does not need anything .</p>
  </blockquote>
  
  <p>Example:</p>
  
  <p>Input:</p>
  
  <pre><code>3
  4 1 2 3 2
  0
  0
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1 2
  1 3
  1 2
  </code></pre>
  
  <p>So, I wrote a program. And you can see the code below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  // Здесь, короче, стек.
  struct Node{
      int data;
      Node *next;
  };
  Node* push(Node *start, int d){
      Node *a = new Node;
      a -&gt; data = d;
      a -&gt; next = start;
      return a;
  }
  Node* pop(Node *s, int &amp;d){
      d = (s -&gt; data);
      Node *a = s -&gt; next;
      delete s;
      return a;
  }
  void move(Node **stack, int from, int to, int save, int *size, int *number){
      int k = 0;
      while(size[to] != number[to]){
          stack[from] = pop(stack[from], k);
          if(k - 1 == to){
              stack[to] = push(stack[to], k);
              size[to]++;
              cout &lt;&lt; from + 1 &lt;&lt; " " &lt;&lt; to + 1 &lt;&lt; endl;
          }
          else{
              stack[save] = push(stack[save], k);
              size[save]++;
              cout &lt;&lt; from + 1 &lt;&lt; " " &lt;&lt; save + 1 &lt;&lt; endl;
          }
          size[from]--;
      }
      if(save &gt; to){
          while(size[save] != number[save]){
              stack[save] = pop(stack[save], k);
              stack[from] = push(stack[from], k);
              size[save]--;
              size[from]++;
              cout &lt;&lt; save + 1 &lt;&lt; " " &lt;&lt; from + 1 &lt;&lt; endl;
          }
      }
      else{
          while(size[save] != 0){
              stack[save] = pop(stack[save], k);
              stack[from] = push(stack[from], k);
              size[save]--;
              size[from]++;
              cout &lt;&lt; save + 1 &lt;&lt; " " &lt;&lt; from + 1 &lt;&lt; endl;
          }
      }
  
  }
  
  void to_one(Node **stack, int to, int m, int *size){
      int k = 0;
      for(int i = to + 1; i &lt; m; i++){
          while(size[i] != 0){
              stack[i] = pop(stack[i], k);
              stack[to] = push(stack[i], k);
              size[to]++;
              size[i]--;
              cout &lt;&lt; i + 1 &lt;&lt; " " &lt;&lt; to + 1 &lt;&lt; endl;
          }
  
      }
  
  }
  
  int main(){
      int n = 0;
      int k = 0;
      cin &gt;&gt; n;
      Node **stack = new Node*[n]; // наши полки
      int *size = new int[n]; // количество элементов на каждой полке изначально
      int *number = new int[n]; // количество элементов соответствующего типа
      for(int i = 0; i &lt; n; i++) number[i] = 0;
      for(int i = 0; i &lt; n; i++){
          cin &gt;&gt; size[i];
          for(int j = 0; j &lt; size[i]; j++){
                  cin &gt;&gt; k;
                  number[k - 1]++;
                  stack[i] = push(stack[i], k);
          } // формируем наши полки
      }
      int from = 0;
      int m = n; // вспомогательная переменная количества неотсортированных полок
      //сперва все перенесем в одну полку (первую)
      to_one(stack, from, m, size);
      int save = 1; // вспомогательная
      //Теперь восстановим последнюю стопку, а вспомогательной будет вторая.
      int to = m - 1;
      move(stack, from, to, save, size, number);
      //Теперь наша готовая последняя стопка станет резервной навсегда.
      save = to;
      //А теперь отдельно рассмотрим полки между последней и первой, повторив цикл выше.
      m--;
      for(int i = m; i &gt; from; i--){
          to = i - 1;
          to_one(stack, from, i, size);
          move(stack, from, to, save, size, number);
      }
      delete []number;
      delete []size;
  
      return 0;
  }
  </code></pre>
  
  <p>I tried to debug it and this is what I got.
  My input was:</p>
  
  <pre><code>4
  4 1 2 3 4
  1 3
  3 2 1 4
  3 2 1
  </code></pre>
  
  <p>Program crashed after this output:</p>
  
  <pre><code>2 1
  3 1
  3 1
  3 1
  4 1
  4 1
  1 2
  </code></pre>
  
  <p>Screenshot:
  <img src="https://i.stack.imgur.com/Z9gXA.jpg" alt="enter image description here"></p>
  
  <p>I don't know why I get this error. 
  Help me please.</p>