2014-02-26 32 views
0

我正在用C++構建一個圖像分析程序。它需要一個文本文件來保存這些值來構建灰度圖像。我使用平方差的總和來找到這個圖像中的特定塊。這是使用頭文件中的矩陣類構建的,所以我有兩個重載的構造函數和一個析構函數,它刪除指向分配兩個內存的double的指針堆這個巨大的數組值(768 x 1024)。然而,這拋出了一個內存錯誤;調試斷言失敗,表達式:塊類型有效。我無法理解爲什麼會發生這種情況。爲了進行SSD計算,我使用了兩個for循環;兩個矩陣對象被操作,其中一個修改調用其中一個構造函數來從一個更大的矩陣對象中獲取一個塊來創建一個新的矩陣對象。我知道當對象超出範圍時,析構函數會在每個循環中被調用兩次?這是雙重刪除,爲什麼會發生錯誤?以下是我的構造函數和循環。如果有人可以看到我爲什麼得到這個錯誤,我會很高興。使用C++類的巨大刪除錯誤

構造:

// Matrix constructor creating a new matrix object where all elements are the same number 
Matrix::Matrix(int sizeR, int sizeC, double val) 
{ 
    //cout << "Matrix(int sizeR, int sizeC, double val) is invoked.." << endl; 

    M = sizeR; 
    N = sizeC; 

    data = new double[M * N];// Initialise space for class array 'data' 

    for (int i = 0; i < M* N; i++) 
    { 
     data[i] = val;// Set each element of the array to the same value passed to the constructor from main 
    } 
} 

// Matrix constructor taking pointer to array as input; creates a new matrix object 
Matrix::Matrix(int sizeR, int sizeC, double* input_data) 
{ 
    //cout << "Matrix::Matrix(int sizeR, int sizeC, double* input_data) is invoked...." << endl; 
    M = sizeR; 
    N = sizeC; 

    data = new double[M * N];// Initialise space for class array 'data' 

    for (int i = 0; i < M * N; i++) 
    { 
     data[i] = input_data[i];// Set elements in data as elements from input_data passed to the constructor from main 
    } 
} 

析構函數:

// Matrix destructor 
Matrix::~Matrix() 
{ 
    //cout << "Matrix::~Matrix() is invoked..." << endl; 
    delete data; 
} 

代碼在主:

for (int i = 0; i < (768 - 21); i++) 
    { 
     for (int j = 0; j < (1024 - 21); j++) 
     { 
      counter++; 
      clutteredBlock = cluttered.getBlock(i, (i + 21), j, (j + 21)); 
      diff = clutteredBlock - wallyBlock; 
      diff = diff * diff; 
      tempVal = diff.Sum(); 
      if (i == 0 && j == 0) 
      { 
       ssd = tempVal; 
      } 

      if (tempVal <= ssd) 
      { 
       ssd = tempVal; 
       co1 = i; 
       co2 = j; 
      } 
     } 
    } 

所以M,N和數據都是私人類成員; M和N分別是int和數據是double*;數據是我試圖刪除的指針,並且無法使用。

更新:如果我忽略錯誤,然後給我一個HEAP CORRUPTION錯誤,說我試圖在緩衝區後寫入堆?

UPDATE:Assignment Operator;

Matrix& Matrix::operator=(const Matrix& input) 
{ 
    //cout << "Matrix::operator= is invoked..." << endl; 

    if (this == &input) 
    { 
     return *this; 
    } 
    else 
    { 
     delete data; 
     M = input.getR(); 
     N = input.getC(); 
     data = new double[M * N]; 

     for (int i = 0; i < M; i++) 
     { 
      for (int j = 0; j < N; j++) 
      { 
       Set(i, j, input.Get(i, j)); 
      } 
     } 
    } 

    return *this; 
} 

任何輸入,非常感謝:)

+2

應該很可能是'delete []數據;'也考慮使用std :: unique_ptr或std :: shared_ptr(如果你沒有C++ 11,使用std :: auto_ptr),它們將清除由他們自動 – Creris

+2

你也需要一個賦值操作符。查找*三條規則*。 – juanchopanza

+1

@TheOne一個普通的向量可能是一個更好的選擇。它是可複製的,可分配的,不共享。 – juanchopanza

回答

1

使用std::vector爲您的存儲。它自動處理分配和釋放。問題解決了。

+0

我欣賞小費,但目的這個任務是爲了證明我理解類結構和指針等。我已經證明我的工作已經完成,我只想解決我自己的問題 –

+1

@HollyMarieBatchelor:你應該在問題中陳述所有相關信息。因爲它是,你問了一些不是你的問題。無論對解決方案有什麼限制,你最好說明它們(例如,這是一個你必須使用原始'new'的任務,或者其他的 - 最好的可能是逐字引用這個任務)。 –

+0

對於未來的帖子,我會記住這一點,對不起 –