title: Resize static array in C
body: <p>UPDATE:
  I've changed the static array for a dynamic, but I still get the segment violation error, although eclipse says:  </p>
  
  <pre><code>*** glibc detected *** (path to file) double free or corruption (!prev): 0x00000000004093d0 ***
  </code></pre>
  
  <p>StructHashTable is a typedef...</p>
  
  <pre><code>int main() {
     ...
     StructHashTable *B0 = (StructHashTable *) malloc(N_ELEMS*sizeof(StructHashTable));
     ...
  }
  
  void resizeHash(StructHashTable *hash) {
      int size = currentElements + N_ELEMS;
      StructHashTable newHash[size];
      int i;
  
      for (i = 0; i &lt; size; i++) newHash[i].key = FREE;
  
      for (i = 0; i &lt; currentElements; i++) insertHash(newHash, hash[i]);
  
      currentElements = size;
  
      hash = (StructHashTable *) realloc(hash, size*sizeof(StructHashTable));
      if (hash != NULL) {
          for (i = 0; i &lt; size; i++) hash[i] = newHash[i];
      }
  }
  </code></pre>
  
  <p>What's wrong now? Am I using in a bad way realloc? or what? C is driving me crazy...</p>
  
  <p>OLD:
  I'm working in university homework and I need to resize an static array in C, it has to be static, the debbugger says segment violation...</p>
  
  <p>I have a main function that declares the array...</p>
  
  <pre><code> // File: main.c
      int main() {
         ...
         StructHashTable hash[N_ELEMS];
         ...
      }
  </code></pre>
  
  <p>At some point on runtime I need more elements than N_ELEMS and I've written a function to do it in HashTable.c, that's the method:</p>
  
  <pre><code> // File: HashTable.c
      #define N_ELEMS 32
      int currentElements = N_ELEMS
  
      void resizeHashTable(StructHashTable *hash) {
          int size = currentElements + N_ELEMS;
          StructHashTable newHash[size];
          int i;
          // Inicialize newHash
          for (i = 0; i &lt; size; i++) newHash[i].key = FREE;
  
          // Insert old hash elements to the new table...
          for (i = 0; i &lt; currentElements; i++) {
              insertHash(newHash, hash[i]);
          }
  
          currentElements = size;
          // I've tried making hash null with no luck...
          //hash = NULL;
          //free(hash);
          // HERE'S THE ERROR...
          hash = newHash;
          // I've tried *hash = *newHash with the same result...
  
      }
  </code></pre>
  
  <p>Can someone tell me how to do what I'm trying to do?</p>
  
  <p>Thanks.</p>