title: something wrong with my dynamic array
body: <p>while add more than 13 element, the result seems to be not correct!
  my code is below! when add 12 elements, the result is </p>
  
  <blockquote>
    <p>the NO.0 is 0<br>
    the NO.1 is 1<br>
    the NO.2 is 2<br>
    the NO.3 is 3<br>
    the NO.4 is 4<br>
    the NO.5 is 5<br>
    the NO.6 is 6<br>
    the NO.7 is 7<br>
    the NO.8 is 8<br>
    the NO.9 is 9<br>
    the NO.10 is 10<br>
    the NO.11 is 11</p>
  </blockquote>
  
  <p>When add one more, the result is changed a lot, I have no idea what's going on here:</p>
  
  <blockquote>
    <p>the NO.0 is 0<br>
    the NO.1 is 1<br>
    the NO.2 is 2<br>
    the NO.3 is 3<br>
    the NO.4 is 4<br>
    the NO.5 is 5<br>
    the NO.6 is 6<br>
    the NO.7 is 7<br>
    the NO.8 is 0<br>
    the NO.9 is 1<br>
    the NO.10 is 2<br>
    the NO.11 is 3<br>
    the NO.12 is 12</p>
  </blockquote>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  typedef struct dynarray myarray;
  struct dynarray {
      int size;
      int length;
      int *array;
  };
  
  myarray *create(void) {
      /* TODO: define for arrays */
      myarray *da;
      da = malloc(sizeof(myarray));
      da-&gt;size = 0;
      da-&gt;length = 3;
      da-&gt;array = (int *)malloc(da-&gt;length * sizeof(int));
      return da;
  }
  
  
  void add(myarray *array, int val) {
      int i;
      int length = array-&gt;length;
      int size = array-&gt;size;
      int *temp;
      //printf("da-&gt;size1 = %d\nda-&gt;length1 = %d\n",array-&gt;size,array-&gt;length);
  
      if (size == length) {
          temp = (int *)malloc(length * sizeof(int));
          for (i = 0; i&lt;size; i++) {
              temp[i] = array-&gt;array[i];
              printf("%d,", array-&gt;array[i]);
          }
  
  
          //printf("!@#$@#$=%d\n",array-&gt;length);
  
          array-&gt;array = (int*)realloc(array-&gt;array, sizeof(int)*length);
          for (i = 0; i&lt;size; i++)
              array-&gt;array[i] = temp[i];
          array-&gt;length *= 2;
          length *= 2;
      }
      if (size &lt;length) {
          array-&gt;array[size] = val;
          printf("array[%d]====%d\n", size, array-&gt;array[size - 1]);
          array-&gt;size++;
      }
  }
  
  
  int main() {
      int i;
      myarray *da = create();
      //printf("da-&gt;size = %d\nda-&gt;length = %d\n",da-&gt;size,da-&gt;length);
  
      for (i = 0; i&lt;12; i++)
          add(da, i);
      // printf("a-&gt;size = %d\nda-&gt;length = %d\n",da-&gt;size,da-&gt;length);
      for (i = 0; i&lt;12; i++)
          printf("the NO.%i is %d\n", i, da-&gt;array[i]);
  }
  </code></pre>