2013-12-08 3 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에서 잘 ++ 또한 코드 : : 블록의 코드를 컴파일,하지만 난이 코드를 실행하려고 할 때, 그것은 응답하지 끝, 나는 CodePad에서 실행할 때, 나는 메시지가 나타납니다 세그멘테이션 오류. 편집 : 구체적으로 말해서 "세그먼트 오류 (코어가 덤프 됨)"라고 표시됩니다.편집 2 : 분할 문 오류가 주 문에서 첫 번째 삽입 _ 시작 부분에서 시작되는 것으로 보입니다 (B [* A [x]] == x)이 문제를 해결하는 방법에 대한 아이디어가 있습니까? 편집 3 : B [* A [x]] == x는 member_hash에서 시작하여 B가 비어 있기 때문에 발생하는 것으로 보입니다. 그러나 다른 조건 (* A [x] < bsize)과 (x < n)이 있기 때문에 * A [x]의 쓰레기 값이이 조건에 어떻게 도달하는지 혼란 스럽습니다. 해결책?

+0

이 코드가 보인다 ? q = o (1) + array + initialization)하지만 비참하게 실패합니다. 그게 당신이하려는 일입니까? –

+0

나는 할당의 일부로 A 외에 다른 필수 배열 B를 사용하는 해시 테이블을 구현하고있다. 그리고 예, 저는 O (1) 트릭을 시도하고 있습니다. 그러나 무엇이 seg-fault를 일으키는가? – user3080777

+0

'* A [x]> bsize '라고 혼동합니다. 'A'는'* A [1]'로 선언됩니다. 나는 이것이 서로 양립 할 수 있다고 생각하지 않는다. * (A + x)를 원하십니까? – KeithSmith

답변

1

int *A[1];은 A를 int 포인터의 한 요소 요소의 배열로 선언한다는 것을 의미합니다.

배열 할당 컴파일하지만 좋지 않아, (A [0]이 시점에서 정의되지로) 당신은 알 수없는 주소 INT의 배열을 할당하는

나는 correcly 당신이 달성하려고하는 것을 이해한다면 , 당신은 A가 int 타입의 n 개의 원소로 이루어진 배열이되기를 원한다.

그래서 그에 따라 코드를 업데이트는 [O (1) 배열 초기화 트릭]의 구현 (https://www.google.com/search 될하려고처럼

#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

A는 int * A = new int [n]으로 정의해야합니다. 그 때 무엇을해야합니까? 또한 프로그램은 int * A를 초기화하고 A = new int [n]을 생성자로 사용하여 컴파일하지 않습니다. – user3080777

+0

내가 게시 한 코드를 읽고, 선언은'int * A;'와 instanciation'A = new int [n]'입니다. 이는 한 줄의 선언/인스턴스화와 동일합니다.'int * A = new int [n];' – jbh