2016-09-20 57 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。我很确定这个问题会被记录下来。