2017-12-08 6 views
-2

unordered_set에 대한 사용자 지정 클래스와 사용자 지정 해시 함수를 만들었습니다.unordered_set에 C++을 삽입하면 프로그램이 깨집니다.

malloc: *** error for object 0x9000000000000000: pointer being freed was not allocated 
*** set a breakpoint in malloc_error_break to debug 
Abort trap: 6 

내가 unordered_set를 사용하는 것이 필수적입니다 : 그 unordered_set에 삽입 할 때마다, 나는 메모리 오류가 발생합니다.

template <class T> 
class Seed { 

    private: 
    Point start; 
    int seed_size; 
    T** data; 
    Seed* seed_match; 
    T _value; 

    public: 
    Seed(int x, int y, int s): seed_size(s), data(new T*[s]), _value(T()) { 
     start = Point(x, y); 
     for (int i = 0; i < seed_size; i++) 
      data[i] = new T[seed_size]; 

     for (int i = 0; i < seed_size; i++) { 
     for (int j = 0; j < seed_size; j++) data[i][j] = NULL; 
     } 

     seed_match = NULL; 
    } 

    ~Seed() { 
     for (int x = 0; x < seed_size; x++) { 
      delete [] data[x]; 
     } 
     delete [] data; 
    } 

    void add(int x, int y, T color_val) { 
     assert(data[x][y] == NULL); 
     data[x][y] = color_val; 
     _value += color_val; 
    } 

    bool match (const Seed &_match) { 
     if (seed_match == NULL) { 
     seed_match = &_match; 
     return true; 
     } 
     else return false; 
    } 

    T get_color(int x, int y) const { 
     assert(x >= 0); 
     assert(y >= 0); 
     assert(x < seed_size); 
     assert(y < seed_size); 

     return data[x][y]; 
    } 

    bool operator==(const Seed<T> &b) { 

     for (int x = 0; x < seed_size; x++) { 
     for (int y = 0; y < seed_size; y++) { 
      if (get_color(x, y) != b.get_color(x, y)) return false; 
     } 
     } 

     return true; 

    } 

    int seed_value() const { return _value; } 

}; 

이 내 사용자 정의 해시 함수입니다 : 나의 주에서

template <class T> 
struct SeedEqualByValue { 
public: 
    bool operator()(const Seed<T> & seed1, const Seed<T> & seed2) const { 

     if (seed1.seed_value() == seed2.seed_value()) 
      return true; 
     else 
      return false; 
    } 
}; 

template <class T> 
struct SeedHashByValue { 
public: 
    size_t operator()(const Seed<T> & s1) const { 
     return std::hash<int>()(s1.seed_value()); 
    } 
}; 

, 내가 3 개 변수로 시드 클래스의 3 개 인스턴스를 인스턴스화하고 또한 인스턴스를

내 사용자 정의 클래스입니다 내 SeedHashByValue 구조체와 같은 해시 함수 및 SeedEqualByValue 구조체와 같은 내 함수를 사용하여 시드를 사용하는 unordered_set. unordered_map을 만든 후에는 unordered_map에 Seed 개체를 삽입 할 때마다 malloc 오류가 발생하며이를 해결하는 방법을 모르겠습니다.

Seed<int> b(0, 0, 5); 
Seed<int> a(0, 0, 5); 
Seed<int> c(0, 0, 5); 
c.add(4, 4, 100); 
a.add(1, 2, 4); 
a.add(1, 1, 3); 
b.add(1, 1, 3); 
unordered_set<Seed<int>, SeedHashByValue<int>, SeedEqualByValue<int> > seeds; 
seeds.insert(c); 

는 또한, 포인트는 사람이 설명이 필요한 경우 public 멤버 변수 int xint y으로 x와 y 값을 보유하고 단지 클래스입니다 :

내 주요 기능의 내용이다.

+4

가장 가능성있는 설명은 클래스 [3의 규칙을 위반] (https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)이지만 불행히도 [ mcve] 권위있는 대답이 불가능합니다. 질문을 게시하기 전에 읽어야 할 stackoverflow.com의 [help] 기사에서 설명한대로 질문을 편집하고 [mcve]의 모든 요구 사항을 준수하도록해야합니다. –

+1

클래스에 복사 생성자가 없으며 중복 할당 연산자가 오버로드되어 있지 않습니다.이 중 부족하여 동일한 메모리가 여러 번 할당 취소 될 수 있습니다. 이는 정의되지 않은 동작입니다. –

답변

0

@Algirdas가 말한 것에 대해 후속 조치를 취하기 위해 Seed은 세트로 얕게 복사되므로 세트가 꺼지면 동일한 부모 포인터에 delete이 두 개인이됩니다 그리고 일단 변수가 범위를 벗어나면

할당 연산자에서 데이터를 전송하거나 std::unique_ptr과 같은 것을 사용하여 데이터를 처리하는 방법을 수정하고 할당되어 있는지 확인해야합니다 (기본적으로 소유권이 이전 됨).