C ++ - 在重新分配的指针指针上自由崩溃

时间:2015-11-15 09:39:29

标签: c++ pointers

这是我第一次来到这里,我是编码新手。 我正在尝试创建一个Matrix类,

//Matrix.hpp
#ifndef __MATRIX_H__
#define __MATRIX_H__
class Matrix{
private:
    unsigned int m_row, m_column;
    unsigned int** matrix;

public:
    Matrix(unsigned int row, unsigned int column);
    ~Matrix();

    void transposeMatrix();

    void fill(unsigned int x);
    void fill_x(unsigned int row, unsigned int column, unsigned int x);
    void setMatrix(unsigned int** mat);

    bool isValid(unsigned int row, unsigned int column);

    unsigned int getRowNum();
    unsigned int getColumnNum();
    unsigned int get_x(unsigned int row, unsigned int column);
    unsigned int** getMatrix();

};
#endif
Matrix.cpp文件中的

#include <cstdlib>
#include <cstdbool>
#include "Matrix.hpp"

bool Matrix::fail = false;

Matrix::Matrix(unsigned int row, unsigned int column){
    m_row=row;
    m_column=column;

    matrix = new unsigned int*[row];
    for(int i=0; i<row; i++){
        matrix[i]=new unsigned int[column];
        for(int j=0; j<column; j++){
            matrix[i][j]=0;
        }
    }
}

Matrix::~Matrix(){
    for(int i=0; i < m_row; i++){
        free(matrix[i]);
        matrix[i]=NULL;
    }
    free(matrix);
    matrix=NULL;
}

void Matrix::transposeMatrix(){
    Matrix tmp_matrix(m_column, m_row);
    for(int i=0; i < m_row; i++){
        for(int j=0; j< m_column; j++){
            tmp_matrix.fill_x(j, i, this->get_x(i, j));
        }
    }

    this->setMatrix(tmp_matrix.getMatrix());
    unsigned int tmp = m_row;
    m_row = m_column;
    m_column = tmp;
}

void Matrix::setMatrix(unsigned int** mat){
    for(int i=0; i < m_row; i++){
        free(matrix[i]);
        matrix[i]=NULL;
    }
    free(matrix);
    matrix=NULL;
    matrix=mat;
}

unsigned int** Matrix::getMatrix(){
    return matrix;
}

void Matrix::fill(unsigned int x){
    for(int i=0; i<m_row; i++){
        for(int j=0; j<m_column; j++){
            matrix[i][j]=x;
        }
    }
}

void Matrix::fill_x(unsigned int row, unsigned int column, unsigned int x){
    if(row>= m_row||column>=m_column) return;
    matrix[row][column]=x;
}

bool Matrix::isMatch(unsigned int x, unsigned int y){
    if (x==y) return true;
    return false;
}

bool Matrix::isValid(unsigned int row, unsigned int column){
    if(row<m_row&&column<m_column) return true;
    return false;
}

unsigned int Matrix::get_x(unsigned int row, unsigned int column){
    if(isValid(row, column)) return matrix[row][column];
    throw "Invalid matrix index exception.";
}

unsigned int Matrix::getRowNum(){
    return m_row;
}

unsigned int Matrix::getColumnNum(){
    return m_column;
}

但是如果我运行这个程序,如果在调用转置函数后调用析构函数,程序总会崩溃。 问题是由unsigned int** matrix引起的吗?我不能将释放的指针重新分配给另一个指针,然后再将其释放吗?

更新:现在我已将所有free()更改为删除[]。但它仍然无法奏效。在main.cpp中:

#include <iostream>
#include "Matrix.cpp"

int main(){
    Matrix c(2,3);
    c.fill(30);
    c.transposeMatrix();
    try{
        for(int i=0; i<c.getRowNum(); i++){
            for(int j=0; j<c.getColumnNum(); j++){
                cout<<c.get_x(i, j)<<" ";
            }
            cout<<endl;
        }
    } catch (const char* e){
        cout<<e;
    }
}

输出

[some random number] 0
[some random number] 0
30 30

1 个答案:

答案 0 :(得分:0)

好吧,问题在于当转置函数结束时在tmp_matrix上调用析构函数,从而删除它的矩阵指针,该矩阵指针也被实际矩阵容器指向。

它已被修复。谢谢大家。即使被投票放弃也有点令人沮丧。但也许这确实是一个愚蠢的问题。