2016-09-20 19 views
-4

ハッシュマップを実装するためのコードは、次のとおりです。なぜそれが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

一つの可能​​性の高いクラッシュは

result->key = key; 

を経由して、文字列への任意のポインタを格納create_nodeによって引き起こされる私はあなたがすぐに不適切であることを運命づけられている文字にはいくつかのポインタでそれを呼び出すことになるでしょう範囲外。おそらくstructノードはstd :: stringを使用して、動作させる可能性が高くなるはずです。または、少なくとも文字列をコピーし、Cスタイルのポインタが好きな場合は、そのメモリを破棄するためのnodeのデストラクタを作成します。

また、Valgrindを試してください。私はかなり問題が注目されることを確信しています。

関連する問題