【问题标题】:Invalid object access c++无效的对象访问c ++
【发布时间】:2015-05-11 14:26:27
【问题描述】:

我试图从 Stroustroup 的 C++ 书中运行这段代码,我添加了一些代码,因为书中没有提供所有内容。我一直遇到以下问题。我知道这里有多个关于同一个错误的问题,但我的代码不同,因此这个问题。

错误是

copy(5826,0x7fff76b09300) malloc: * 对象 0x7ff6a9404c18 的错误:已释放对象的校验和不正确 - 对象可能在被释放后被修改。 * 在 malloc_error_break 中设置断点进行调试 中止陷阱:6

#include<stdio.h>
#include<iostream>

using namespace std;
class Vector {
private:
    double * elem; // elem points to an array of sz doubles
    int sz;
public:
    Vector(int s) {
        sz = s;
        elem = new double[sz];
        for (int i = 0; i<sz; i++) {
            elem[i] = i;
        }
    }
    ~Vector() { delete[] elem; } // destructor: release resources
    Vector(const Vector& a); // copy constructor
    Vector& operator=(const Vector& a); // copy assignment
    double& operator[](int i);
    const double& operator[](int i) const;
    int size() const;
};


Vector::Vector(const Vector& a) // copy constr uctor
{
    elem = new double[sz], // allocate space for elements
        sz = a.sz;
    for (int i = 0; i != sz; ++i) // copy elements
        elem[i] = a.elem[i];
}


double& Vector::operator[](int k) {
    return this->elem[k];
}


Vector& Vector::operator=(const Vector& a) // copy assignment
{
    double* p = new double[a.sz];
    for (int i = 0; i != a.sz; ++i)
        p[i] = a.elem[i];
    delete[] elem; // delete old elements
    elem = p;
    sz = a.sz;
    return *this;
}


int main() {
    Vector v1(10);
    Vector v2 = v1;
    v1[0] = 2;
    v2[1] = 3;
    cout << v1[0] << "\n";
    return 0;
}

【问题讨论】:

    标签: c++ arrays memory constructor copy


    【解决方案1】:

    您的复制构造函数应该反转两行,因为您尚未为 sz 设置值。

    Vector::Vector(const Vector& a) // copy constructor
    {
        elem = new double[sz];
        sz = a.sz;
        for (int i=0; i!=sz; ++i)
            elem[i] = a.elem[i];
    }
    

    所以你可以这样做

    Vector::Vector(const Vector& a) // copy constructor
    {
        sz = a.sz;
        elem = new double[sz];
        for (int i=0; i!=sz; ++i)
            elem[i] = a.elem[i];
    }
    

    【讨论】:

      【解决方案2】:

      您的复制构造函数有一个错误:

      elem = new double[sz], // allocate space for elements
      

      这是使用您正在创建的对象的sz,在这种情况下sz 尚未初始化。应该是:

      elem = new double[a.sz], // allocate space for elements
      

      我还建议不要使用逗号,并将其更改为

      elem = new double[a.sz];//<--semicolon instead of comma
      

      【讨论】:

      • 谢谢,这个我没注意,我收到的错误信息真的很难找到这个,为什么系统会抛出这个错误?
      【解决方案3】:

      查看复制构造函数。 sz 在语句elem = new double[sz] 中使用时未初始化,因此访问其值会导致未定义的行为。大概你打算声明是elem = new double[a.sz]

      这样做的结果是,在 main() 中,定义/初始化 Vector v2 = v1 会产生未定义的行为。

      【讨论】:

        【解决方案4】:

        此复制构造函数无效

        Vector::Vector(const Vector& a) // copy constr uctor
        {
            elem = new double[sz], // allocate space for elements
                sz = a.sz;
            for (int i = 0; i != sz; ++i) // copy elements
                elem[i] = a.elem[i];
        }
        

        首先你要设置 sz 或在 operator new 中使用a.sz

        Vector::Vector(const Vector& a) // copy constr uctor
        {
            sz = a.sz;
            elem = new double[sz], // allocate space for elements
        
            for (int i = 0; i != sz; ++i) // copy elements
                elem[i] = a.elem[i];
        }
        

        考虑到最好将数据成员 sz 定义为具有类型 size_t 而不是 int

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-04
          • 1970-01-01
          • 2012-07-07
          • 1970-01-01
          • 1970-01-01
          • 2011-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多