title: Dynamic Array in C - realloc
body: <p>I know how to build Dynamically allocated arrays, but not how to grow them.</p>
  <p>for example I have the following interface..</p>
  <pre><code>void insertVertex( vertex p1, vertex out[], int *size);
  </code></pre>
  <p>This method takes a vertex and stores it into the  out array. After storing the vertex I increase the count of length for future calls.</p>
  <p>p1 - is the vertex I'm going to add.</p>
  <p>out[] - is the array I need to store it in (which is always full)</p>
  <p>length - the current length</p>
  <p>Vertex is defined as..</p>
  <pre><code>typedef struct Vertex{
  int x;
  int y;
  } Vertex;
  </code></pre>
  <p>This is what I'm using in Java..</p>
  <pre><code>Vertex tempOut = new Vertex[size +1];
  //Code to deep copy each object over
  tempOut[size] = p1;
  out = tempOut;
  </code></pre>
  <p>This is what I believed I could use in c..</p>
  <pre><code>out = realloc(out, (*size + 1) * sizeof(Vertex));
  out[(*size)] = p1;
  </code></pre>
  <p>However, I keep on receiving an error message that the object was not allocated dynamically.</p>
  <p>I found a solution that will resolve this.. Instead of using Vertex* I was going to switch to Vertex** and store pointers vs. vertex. However, after switching everything over I found out that I over looked the fact that the unit test will be providing me a Vertex out[] that everything has to be stored in.</p>
  <p>I have also tried the following with no luck.</p>
  <pre><code>Vertex* temp = (Vertex *)malloc((*size + 1) * sizeof(Vertex));
  for(int i = 0; i &lt; (*size); i++)
  {
  temp[i] = out[i];
  }
  out = temp;
  </code></pre>
  <p>However, no matter what I do when I test after both of these the array returned has not changed.</p>
  <p><strong>Update - Requested information</strong></p>
  <p>out - is defined as an array of Vertex (Vertex out[])</p>
  <p>It is originally built with the number of vertex in my polygon. For example.</p>
  <p>out = (Vertex *)malloc(vertexInPolygon * sizeof(Vertex))</p>
  <p>Where vertexInPolygon is an integer of the number of vertex in the polygon.</p>
  <p>length was a typo that should have been size.</p>
  <p>Size is an integer pointer</p>
  <pre><code>int *size = 0;
  </code></pre>
  <p>Each time a vertex is in the clipping plane we add it to the array of vertex and increase the size by one.</p>
  <p><strong>Update</strong></p>
  <p>To better explain myself I came up with a short program to show what I'm trying to do.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct Vertex {
      int x, y;
  } Vertex;
  
  void addPointerToArray(Vertex v1, Vertex out[], int *size);
  
  void addPointerToArray(Vertex v1, Vertex out[], int *size)
  {
      int newSize = *size;
      newSize++;
      
      out = realloc(out, newSize * sizeof(Vertex));
      out[(*size)] = v1;
      
      //  Update Size
      *size = newSize;
  }
  
  int main (int argc, const char * argv[])
  {
      //  This would normally be provided by the polygon
      int *size = malloc(sizeof(int)); *size = 3;
      
      //  Build and add initial vertex
      Vertex *out = (Vertex *)malloc((*size) * sizeof(Vertex));
      Vertex v1; v1.x = 1; v1.y =1;
      Vertex v2; v2.x = 2; v2.y =2;
      Vertex v3; v3.x = 3; v3.y =3;
      
      out[0] = v1;
      out[1] = v2;
      out[2] = v3;
      
      //  Add vertex
      //  This should add the vertex to the last position of out
      //  Should also increase the size by 1;
      Vertex vertexToAdd; vertexToAdd.x = 9; vertexToAdd.y = 9;
      addPointerToArray(vertexToAdd, out, size);
      
      for(int i =0; i &lt; (*size); i++)
      {
          printf(&quot;Vertx: (%i, %i) Location: %i\n&quot;, out[i].x, out[i].y, i);
      }
     
  }
  </code></pre>