title: C Having Trouble Resizing a Hash Table
body: <p>I'll post snippets of the code here which (I think) are relevant to the problem, but I can pastebin if necessary. Probably posting more than enough code already :P</p>
  
  <p>My program includes a hash table which needs to double when a certain hash bucket reaches 20 entries. Although I believe the logic to be good, and it compiles like a charm, it throws up a  <em>Segmentation Fault</em>. The code runs like a charm when not resizing, but resizing messes things up.</p>
  
  <p>Thanks for any help :)</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x0000000000401012 in ml_add (ml=0x7fffffffe528, me=0x75a5a0) at mlist.c:74
  74          while((cursorNode-&gt;next) != NULL){
  Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6_3.5.x86_64
  (gdb) backtrace
  #0  0x0000000000401012 in ml_add (ml=0x7fffffffe528, me=0x75a5a0) at mlist.c:74
  #1  0x0000000000401554 in main (argc=1, argv=0x7fffffffe638) at finddupl.c:39
  </code></pre>
  
  <p><strong>Structure of Hash Table</strong></p>
  
  <pre><code>typedef struct bN { //linked list node containing data and next
      MEntry *nestedEntry;
      struct bN *next;
  } bucketNode;
  
  typedef struct bL { // bucket as linked list
      struct bN *first;
      int bucketSize;
  } bucket;
  
  struct mlist {
      struct bL *currentTable; //bucket array
  };
  </code></pre>
  
  <p><strong>Add Function</strong></p>
  
  <pre><code>int ml_add(MList **ml, MEntry *me){
  
      MList *tempList;
      tempList = *ml;
  
      bucketNode *tempNode = (bucketNode *)malloc(sizeof(bucketNode));
      tempNode-&gt;nestedEntry = me;
      tempNode-&gt;next = NULL;
  
      unsigned long currentHash = me_hash(me, tableSize);
  
      if((tempList-&gt;currentTable[currentHash].bucketSize) == 0)   {
          tempList-&gt;currentTable[currentHash].first = tempNode;
          tempList-&gt;currentTable[currentHash].bucketSize = (tempList-&gt;currentTable[currentHash].bucketSize) + 1;
      }
      else if((tempList-&gt;currentTable[currentHash].bucketSize) == 20){
          printf("About to resize");
          printf("About to resize");
          tempList = ml_resize(&amp;tempList, (tableSize * 2));
          tableSize = tableSize * 2;
          ml_add(&amp;tempList,me);
      }
      else{
          bucketNode *cursorNode;
          cursorNode = tempList-&gt;currentTable[currentHash].first;
          while((cursorNode-&gt;next) != NULL){
              cursorNode = cursorNode-&gt;next;
          }
          cursorNode-&gt;next = tempNode;
          tempList-&gt;currentTable[currentHash].bucketSize = (tempList-&gt;currentTable[currentHash].bucketSize) + 1;
          return 1;
      }
  
      return 1;
  
  }
  </code></pre>
  
  <p><strong>Resize Function</strong></p>
  
  <pre><code>MList *ml_resize(MList **ml, int newSize){
      MList *oldList;
      oldList = *ml;
  
      MList *newList;
  
      if ((newList = (MList *)malloc(sizeof(MList))) != NULL){
          newList-&gt;currentTable = (bucket *)malloc(newSize * sizeof(bucket));
          int i;
          for(i = 0; i &lt; newSize; i++){
              newList-&gt;currentTable[i].first = NULL;
              newList-&gt;currentTable[i].bucketSize = 0;
          }
      }
  
      int j;
      for(j = 0; j &lt; tableSize; j++){
          bucketNode *cursorNode = oldList-&gt;currentTable[j].first;
          bucketNode *nextNode;
          while(cursorNode != NULL){
              nextNode = cursorNode-&gt;next;
              ml_transfer(&amp;newList, cursorNode, newSize); 
              cursorNode = nextNode;  
          }
      }
  
      free(oldList);
  
      return newList;
  }
  </code></pre>
  
  <p><strong>Transfer to new list function</strong></p>
  
  <pre><code>void ml_transfer(MList **ml, bucketNode *insertNode, int newSize){
  
      MList *newList;
      newList = *ml;
  
      bucketNode *tempNode = insertNode;
  
      tempNode-&gt;next = NULL;
  
      unsigned long currentHash = me_hash((tempNode-&gt;nestedEntry), newSize);
  
      if((newList-&gt;currentTable[currentHash].bucketSize) == 0)    {
          newList-&gt;currentTable[currentHash].first = tempNode;
          newList-&gt;currentTable[currentHash].bucketSize = (newList-&gt;currentTable[currentHash].bucketSize) + 1;
      }
      else{
          bucketNode *cursorNode;
          cursorNode = newList-&gt;currentTable[currentHash].first;
          while((cursorNode-&gt;next) != NULL){
              cursorNode = cursorNode-&gt;next;
          }
          cursorNode-&gt;next = tempNode;
          newList-&gt;currentTable[currentHash].bucketSize = (newList-&gt;currentTable[currentHash].bucketSize) + 1;
      }
  
  }
  </code></pre>