未分配 C++ 错误,释放内存指针中的三个大泄漏(不重复)

C++ Error with big three leak in memory-pointer being freed was not allocated (not duplicate)

提问人:Brogrammer 提问时间:7/6/2015 最后编辑:Brogrammer 更新时间:7/6/2015 访问量:77

问:

我的代码无法正常运行,我不知道如何修复它。对于问什么是三法则的人来说,这不是一个重复的问题,因为该帖子并不能帮助我解决我的问题,因为在这篇文章中,我使用指针指针数组。我不知道我在三大职能中做错了什么,但有人可以帮我纠正我的错误吗?编译器突出显示 delete[] matrix[i];在析构函数中,当 i=2 时,在 for 循环内

在我的头文件中,我有:

#ifndef fasdf_dynn_h
#define fasdf_dynn_h

#include <iostream>
#include <fstream>
#include<string>
#include <cstdlib>
#include <vector>

using namespace std;
template <class T>

class MatrixdynVector{

public:
   // MatrixdynVector()
      {
        //creates a 3 by 3 matrix with elements equal to 0
        m=3;
        n=3;

        matrix=new int*[m];

      for(int i=0;i<m;i++)
         matrix[i]=new int[n];

    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
           matrix[i][j]=0;
      }
   // MatrixdynVector(int m,int n);

    template <class H>
    MatrixdynVector<H>(const MatrixdynVector<H>& c)//copy constructor
    {
        m=c.m;
        n=c.n;

        for (int i = 0; i < c.m; i++)
            for (int j = 0; j < c.n; j++)
                matrix[i][j] = c.matrix[i][j]; // add data to it
    }

    template <class H>
    MatrixdynVector<H>& operator =(const MatrixdynVector<H>& c)//asignment
    {
        if (this == &c)
        {
            return *this;
        }
        else
        {
            matrix = new int*[c.m];
            for (int i = 0; i < c.m; i++)
                matrix[i] = new int[c.n]; // create a multi dimensional array

            for (int i = 0; i < c.m; i++)
                for (int j = 0; j < c.n; j++)
                    matrix[i][j] = c.matrix[i][j]; // add data to it

            for (int i = 0; i < c.m; i++)
                delete[] matrix[i]; // delete the second dimension of the matrix

            delete[] matrix; // delete the first*/

            return *this;
        }
    }

    ~MatrixdynVector()
    {
        if(matrix!=NULL)
        {
            for (int i = 0; i < m; i++)
               delete[] matrix[i]; // delete the second dimension of the matrix

            delete[] matrix; // delete the first

            matrix=NULL;
        }
    }
private:
    int m,n;
    int** matrix;
};


#endif
C++ 三法则

评论

0赞 drescherjm 7/6/2015
我的代码无法正确编译也许您应该编辑问题以发布编译器错误。
0赞 Brogrammer 7/6/2015
@drescherjm我做到了,但我担心有人可能会认为这是一个重复的帖子,因为它与三巨头有关,但原始帖子对我的问题没有帮助。无论如何,我已经更新了标题
0赞 drescherjm 7/6/2015
运行时错误不是编译器错误。
0赞 Brogrammer 7/6/2015
@drescherjm 好的,对不起,我的意思是说我收到一个运行时错误,导致程序设置断点。请帮帮我
0赞 drescherjm 7/6/2015
您的复制构造函数代码是错误的。

答:

1赞 user2249683 7/6/2015 #1

模板不是复制构造函数。

template <class H>
MatrixdynVector(const MatrixdynVector<H>&) // without <H>, which is an syntax error

你需要

MatrixdynVector(const MatrixdynVector&)

另外:正确配对 和 (构造函数中没有)。newdeletenew

0赞 Sébastien Doncker 7/6/2015 #2

我无法编译它进行验证,但我认为您应该有这样的东西:

编辑: 编译后,与问题作者聊天。这是一个工作版本。

#ifndef fasdf_dynn_h
#define fasdf_dynn_h

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

template <class T>
class MatrixdynVector {
private:
    int m,n;
    T **matrix;

public:
    MatrixdynVector();
    MatrixdynVector(int m,int n);
    MatrixdynVector(T diagonal, int n, int m);
    MatrixdynVector(const MatrixdynVector<T>& c);//copy constructor
    virtual ~MatrixdynVector();

    MatrixdynVector<T>& operator =(const MatrixdynVector<T>& c); //assignment
    MatrixdynVector<T> operator -();

    friend ostream& operator <<(ostream& outs, const MatrixdynVector<T> &obj) {
        for (int i = 0; i < obj.m; i++) {
                for (int j = 0; j < obj.n; j++) {
                    outs << " "<< obj.matrix[i][j];
                }
                outs<<endl;
            }
            outs<<endl;

            return outs;
    }
    friend istream& operator >>(istream& in, MatrixdynVector<T> &obj) {
        for (int i = 0; i < obj.m; i++) {
            for (int j = 0; j < obj.n; j++) {
                //if (in==cin)
                    //cout <<"input elements for element at row:"<<i<<" column:"<<j<<": ";
                in >> obj.matrix[i][j];
            }
        }

        return in;
    }

    friend MatrixdynVector<T> operator *(const MatrixdynVector<T>& A, const MatrixdynVector<T>& B) {
        MatrixdynVector<T> product(A.m, A.n);

            for (int i=0;i<A.m; i++) {
                for (int j=0;j<A.n; j++) {
                    T sum=0;
                    for (int k=0; k<A.n; k++) {
                        sum = sum+A.matrix[i][k]*B.matrix[k][j];
                    }
                    product.matrix[i][j]=sum;
                }
            }

            return product;
    }
    friend MatrixdynVector<T> operator +(const MatrixdynVector<T>& a, const MatrixdynVector<T>& b) {
        MatrixdynVector<T> additon(a.m, a.n);   // you should initialize it with the good size.

            if((a.n != b.n) || (a.m != b.m)) {
                // here you should throw an error : the two matrixes should be the same size.
            }

            for(int i=0;i<a.m;i++) {
                for(int j=0;j<b.n;j++) {
                    additon.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];
                }
            }

            return additon;
    }
    friend MatrixdynVector<T> operator -(const MatrixdynVector<T>& A, const MatrixdynVector<T>& B) {
        MatrixdynVector<T> subtract(A.m, A.n);

        if( A.m!= B.m || A.n!= B.n) {//if they have different rows and columns then they cant be equal
            // throw an exception.
        }
        else {
            for(int i=0;i<A.m;i++) {
                for(int j=0;j<B.n;j++) {
                    subtract.matrix[i][j]=A.matrix[i][j]-B.matrix[i][j];
                }
            }
        }

        return subtract;

    }
    friend bool operator ==(const MatrixdynVector<T>& A,const MatrixdynVector<T>& B) {
        //if they have different rows and columns then they cant be equal
            if( A.m!= B.m || A.n!= B.n) {
                return false;
            }
            else {
                for(int i=0;i<A.m; i++) {
                    for(int j=0;j<B.n; j++) {
                        if(A.matrix[i][j] != B.matrix[i][j]) {
                            cout<<A.matrix[i][j]<< " does not equal "<<B.matrix[i][j]<<endl;
                            return false;
                        }
                    }
                }

                return true;
            }
    }
};


template <class T>
MatrixdynVector<T>::MatrixdynVector() {
    m=3;
    n=3;

    matrix=new T*[m];

    for(int i=0; i<m; i++) {
        matrix[i]=new T[n];
        for(int j=0;j<n;j++) {
            matrix[i][j]=0; // be careful! If T is not int, 0 can be bad initializer !!!
        }
    }
}

template <class T>
MatrixdynVector<T>::MatrixdynVector(int x,int y) {
    m = x;
    n = y;

    matrix=new T*[m];

    for(int i=0; i<m; i++) {
        matrix[i]=new T[n];
        for(int j=0;j<n;j++) {
            matrix[i][j]=0; // be careful! If T is not int, 0 can be bad initializer !!!
        }
    }
}

template <class T>
MatrixdynVector<T>::MatrixdynVector(T diagonal, int x, int y) {
    m=x;
    n=y;

    matrix=new T*[m];

    for(int i=0;i<m;i++) {
        matrix[i]=new T[n];
        for(int j=0;j<n;j++) {
            if(i==j) {
                matrix[i][j]=diagonal;
            }
            else {
                matrix[i][j]=0;// be careful! If T is not int, 0 can be bad initializer !!!
            }
        }
    }
}

template <class T>
MatrixdynVector<T>::MatrixdynVector(const MatrixdynVector<T>& c) {
    m=c.m;
    n=c.n;

    matrix = new T*[m];

    for (int i = 0; i < m; i++) {
        matrix[i] = new T[n];
        for (int j = 0; j < c.n; j++) {
            matrix[i][j] = c.matrix[i][j]; // add data to it
        }
    }
}

template <class T>
MatrixdynVector<T>::~MatrixdynVector() {
    if(matrix!=NULL) {
        for (int i = 0; i < m; i++) {
            delete[] matrix[i]; // delete the second dimension of the matrix
        }

        delete[] matrix; // delete the first

        matrix=NULL;
    }
}

template <class T>
MatrixdynVector<T>& MatrixdynVector<T>::operator =(const MatrixdynVector<T>& c) {
    if(this != &c) {
        if(matrix != NULL) {
            for (int i = 0; i < m; i++) {
                delete[] matrix[i]; // delete the second dimension of the matrix
            }
            delete[] matrix; // delete the first*/
        }

        m=c.m;
        n=c.n;

        matrix = new T*[c.m];

        for (int i = 0; i < c.m; i++) {
            matrix[i] = new T[c.n]; // create a multi dimensional array
            for (int j = 0; j < c.n; j++) {
                matrix[i][j] = c.matrix[i][j]; // add data to it
            }
        }
    }

    return *this;
}

template <class T>
MatrixdynVector<T> MatrixdynVector<T>::operator -() {
    MatrixdynVector<T> new_matrix(m, n);

    for(int i=0;i<m;i++) {
        for(int j=0;j<n;j++) {
            new_matrix.matrix[i][j]=-matrix[i][j];
        }
    }
    return new_matrix;
}

#endif

评论

0赞 Brogrammer 7/6/2015
我仍然收到相同的运行时错误。编译器突出显示 delete[] matrix[i];在析构函数中,当 i=2 时,在 for 循环内
0赞 Sébastien Doncker 7/6/2015
您遇到的运行时错误是什么,您能使用您的类给我们函数吗?
0赞 Brogrammer 7/6/2015
阅读我发布的评论上方
0赞 Sébastien Doncker 7/6/2015
好的,我认为您的代码缺少将矩阵初始化为 null 和 n 和 m 为 0 的默认构造函数。我把它添加到我的代码中。
0赞 Brogrammer 7/6/2015
不过,我的默认构造函数应该创建一个元素等于零的 3 x 3 矩阵,所以我不明白将矩阵初始化为 null 对我有什么帮助。我会将我的默认构造函数添加到我的帖子中,以便您看到我做了什么