2013-12-08 36 views
0

我不明白爲什麼我的程序中發生了分段錯誤。有人可以解釋這個問題嗎?哈希表中的分段錯誤

#include <stdlib.h> 
#include <iostream> 
#include <vector> 
using namespace std; 
class HashTable { 
private: 
    int *A[1]; 
    int n; 
    int bsize; 
    vector<int> B; 

public: 
    HashTable(int size) { 
     n = size; 
     bsize = 0; 
     *A = new int[n]; 
    } 
    /* 
    You should use another array B besides A. You do not initialize A, and thus the cells of A contain 
    garbage initially. The array B is a std::vector which is initially empty. Use the same hash function 
    h(x) = x. If A[x] contains i and B[i] contains x, it means that x exists in the hash table. If A[x] 
    contains i but B[i] does not contain x or i is out of the range of B, it means that x does not exist in 
    the hash table. 
    */ 
    bool insert_hash(int x) { 
     if (x >= n) {//sees if x is within the range of A 
     } else { 
      if (*A[x] > bsize){//sees if i is within the range of B 
      } else { 
       if (B[*A[x]] == x) {//sees if B[i] contains x 

        return false; 
       } 
      } 

     } 
     B.push_back(x);//adds key x to B 
     bsize++; 
     *A[x] = bsize;//store location at A 
     return true; 

    //The new key x should be pushed at the back of B and its location is stored at A[x]. This function 
    //should return false if x already exists in the hash table, and returns true otherwise. 
    } 

    bool member_hash(int x) { 
    //The function returns true if x exists in the hash table, and returns false otherwise. 
     if (x <= n) {//sees if x is within the range of A 
      if (*A[x] > bsize){//sees if i is within the range of B 
       if (B[*A[x]] == x) {//sees if B[i] is x 
        return true; 
       } 
      } 
     } 
     return false; 
    } 

    bool delete_hash(int x) { 
    //This function First checks whether x exists in the hash table: If no, then it returns false. Otherwise, 
    //it stores -1 at the cell of B that contains x and then returns true. 
     if (!member_hash(x)) { 
      return false; 
     } else { 
      B[*A[x]] = -1; 
      return true; 
     } 
    } 
}; 

int main() { 
    HashTable a(20); 
    a.insert_hash(5); 
    a.insert_hash(4); 
    a.insert_hash(2); 
    a.insert_hash(2); 
    a.insert_hash(11); 
    a.insert_hash(510); 
    a.member_hash(11); 
    a.delete_hash(11); 
    a.member_hash(11); 
    return 0; 
} 

我編寫的代碼只是在DEVC精細++,並在代碼:: Blocks的,但是當我嘗試運行這段代碼,它最終沒有響應,當我在鍵盤上運行它,我收到消息分段故障。 編輯:更具體地說,它說「分段錯誤(核心轉儲)」 編輯2:似乎分割錯誤開始在main中的第一個insert_hash,在條件語句(B [* A [x]] == x)關於如何解決這個問題的任何想法? 編輯3:B [* A [x]] == x,從member_hash開始,似乎是因爲B爲空。然而,我很困惑* A [x]中的垃圾值如何達到這個條件,因爲我有其他條件(* A [x] < bsize)和(x < n)來防止這種情況發生。解?

+0

這段代碼看起來像它試圖成爲[O(1)初始化數組伎倆]的實現(https://www.google.com/search ?q = o(1)+數組+初始化)但失敗失敗。那是你正在嘗試做什麼? –

+0

我正在實現一個哈希表,使用另一個,除A之外的所需數組B作爲賦值的一部分。是的,我正在嘗試O(1)技巧。但是什麼導致了seg-fault? – user3080777

+0

我很困惑'* A [x]> bsize'。 'A'被聲明爲'* A [1]'。我不認爲這些是兼容的。你想*(A + x)? – KeithSmith

回答

1

int *A[1];表示您將A聲明爲一個指向int的指針元素的數組。

你的數組分配的編譯,但它不是很好,你在一個未知的地址分配一個int數組(如A [0]此時未定義)

如果我正確地將明白你想達到什麼樣的,你想要的是A是一個由n個元素組成的數組。

所以我相應地更新你的代碼:

#include <stdlib.h> 
#include <iostream> 
#include <vector> 
using namespace std; 
class HashTable { 
private: 
    int *A; 
    int n; 
    int bsize; 
    vector<int> B; 

public: 
    HashTable(int size) { 
     n = size; 
     bsize = 0; 
     A = new int[n]; 
    } 
    /* 
    You should use another array B besides A. You do not initialize A, and thus the cells of A contain 
    garbage initially. The array B is a std::vector which is initially empty. Use the same hash function 
    h(x) = x. If A[x] contains i and B[i] contains x, it means that x exists in the hash table. If A[x] 
    contains i but B[i] does not contain x or i is out of the range of B, it means that x does not exist in 
    the hash table. 
    */ 
    bool insert_hash(int x) { 
     if (x >= n) {//sees if x is within the range of A 
     } else { 
      if (A[x] > bsize){//sees if i is within the range of B 
      } else { 
       if (B[A[x]] == x) {//sees if B[i] contains x 

        return false; 
       } 
      } 

     } 
     B.push_back(x);//adds key x to B 
     bsize++; 
     A[x] = bsize;//store location at A 
     return true; 

    //The new key x should be pushed at the back of B and its location is stored at A[x]. This function 
    //should return false if x already exists in the hash table, and returns true otherwise. 
    } 

    bool member_hash(int x) { 
    //The function returns true if x exists in the hash table, and returns false otherwise. 
     if (x <= n) {//sees if x is within the range of A 
      if (A[x] > bsize){//sees if i is within the range of B 
       if (B[A[x]] == x) {//sees if B[i] is x 
        return true; 
       } 
      } 
     } 
     return false; 
    } 

    bool delete_hash(int x) { 
    //This function First checks whether x exists in the hash table: If no, then it returns false. Otherwise, 
    //it stores -1 at the cell of B that contains x and then returns true. 
     if (!member_hash(x)) { 
      return false; 
     } else { 
      B[A[x]] = -1; 
      return true; 
     } 
    } 
}; 

int main() { 
    HashTable a(20); 
    a.insert_hash(5); 
    a.insert_hash(4); 
    a.insert_hash(2); 
    a.insert_hash(2); 
    a.insert_hash(11); 
    a.insert_hash(510); 
    a.member_hash(11); 
    a.delete_hash(11); 
    a.member_hash(11); 
    return 0; 
} 
+0

我需要用int * A = new int [n]來定義A.那麼該怎麼辦? 另外,程序不會在初始化int * A的情況下編譯,然後在構造函數中執行A = new int [n]。 – user3080777

+0

閱讀我發佈的代碼,聲明是'int * A;'和instanciation'A = new int [n]'。這相當於在一行上的聲明/實例'int * A = new int [n];' – jbh