2016-09-20 81 views
-4

下面我們可以找到我實現hashmap的代碼。我不完全確定它爲什麼是seg。斷裂。我相信這是與構造函數或析構函數有關的,但似乎無法完全確定。HashMap實現中的分段錯誤C++

typedef struct _node 
    { 
     char *key; 
     int value;   /* For this, value will be of type int */ 
     struct _node *next; /* pointer to the next node in the list */ 
    } node; 


/* HashMap class */ 
class HashMap 
{ 
private: 
    node ** hashTable; 
    int numSlots; 
public: 
    /* Initializes a hashmap given its set size */ 
    HashMap(int size) 
    { 
     numSlots = size; 
     hashTable = new node*[size] ; 
     for (int i = 0; i < size; i++) 
     { 
      hashTable[i] = NULL; 
     } 
    } 

    /* Deconstructor */ 
    ~HashMap() 
    { 
     for (int i = 0; i < numSlots; i++) 
     { 
      node *temp = hashTable[i]; 
      if (temp != NULL) 
      { 
       delete temp; 
      } 
     } 
     delete [] hashTable; 
    } 

    /*** Hash function. ***/ 

    int hash(char *s) 
    { 
     int i; 
     int sum = 0; 

     for (i = 0; * (s + i) != '\0'; i++) 
     { 
      sum += *(s + i); 
     } 

     return (sum % numSlots); 
    } 

    /* 
    *Free all the nodes of a linked list. Helper method for *deconstructor 
    */ 
    void free_list(node *list) 
    { 
     node *temp; 
     char *tempKey; 
     int tempValue; 
     while (list != NULL) 
     { 
      temp = list; 
      tempKey = temp->key; 
      tempValue = temp->value; 
      list = list->next; 
      if (tempKey != NULL) 
      { 
       delete(tempKey); 
      } 
      delete(temp); 
     } 
    } 

    /* Create a single node. */ 
    node *create_node(char *key, int value) 
    { 
     node *result = new node(); 
     result->key = key; 
     result->value = value; 
     result->next = NULL; 

     return result; 
    } 


    /* 
    *Stores given key/value pair in hashmap 
    *returns boolean for success/failure 
    */ 

    void set (char* key, int value) 
    { 
     int keyValue = hash(key); 
     node *current = hashTable[keyValue]; 
     node *original = current; 
     node *newNode; 
     if (current == NULL) 
     { 
      hashTable[keyValue] = create_node(key, value); 
     } 
     else 
     { 
      while (current != NULL) 
      { 
       current = current -> next; 
      } 

      if (current == NULL) 
      { 
       newNode = create_node(key, value); 
       newNode -> next = original; 
       hashTable[keyValue] = original; 
      } 
     } 
    } 

    /* Return a float value representing the load factor 
    *(item in hash map)/(size of hash map) of the data structure. 
    */ 

    float load() 
    { 
     float numUsed = 0.0; 
     for (int i = 0; i < numSlots; i++) 
     { 
      if (hashTable[i] != NULL) 
      { 
       numUsed++; 
      } 
     } 
     return (numUsed/numSlots); 
    } 

    /* Removes value corresponding to inputted key from table */ 

    int remove (char* key) 
    { 
     int keyValue = hash(key); 
     node *listOfInterest = hashTable[keyValue]; 
     if (listOfInterest == NULL) 
     { 
      return -999; 
     } 
     int toReturn = listOfInterest -> value; 
     delete(listOfInterest); 
     return toReturn; 
    } 

    /* 
    * Look for a key in the hash table. Return -999 if not found. 
    * If it is found return the associated value. 
    */ 
    int get(char *key) 
    { 
     int keyValue = hash(key); 

     node *listOfInterest = hashTable[keyValue]; 

     while (listOfInterest != NULL) 
     { 
      if (listOfInterest != NULL) 
      { 
       return listOfInterest->value; 
      } 
      listOfInterest = listOfInterest -> next; 
     } 

     return -999; 
    } 

    /* Prints hash table */ 

    void print_hash_table() 
    { 
     int i; 
     node *listIterator = NULL; 

     for (i = 0 ; i < numSlots ; i++) 
     { 
      listIterator = hashTable[i]; 
      while (listIterator != NULL) 
      { 
       printf("%s %d\n", listIterator->key, listIterator -> value); 
       listIterator = listIterator -> next; 
      } 
     } 

    } 
}; 
+1

請給我們一些想法,看看錯誤在哪裏;有太多的代碼期望很多人通過它看。 –

+0

解決這些問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+0

如何調試分段錯誤取決於您的平臺以及某些情況下的偏好。你還沒有告訴我們任何關於這件事。 –

回答

0

一個可能的崩潰是由create_node它通過

result->key = key; 

存儲任意字符串指針的我會是你把它與一些字符指針是註定要很快引起不當超出範圍。也許結構節點應該使用std :: string使它更有可能工作。或者至少複製字符串併爲節點創建一個析構函數來處理該內存,如果您更喜歡C風格的指針。

另外,試試Valgrind。我很確定這個問題會被記錄下來。