【问题标题】:Using new and delete on object arrays C++在对象数组 C++ 上使用 new 和 delete
【发布时间】:2015-07-10 20:02:58
【问题描述】:

Does delete call the destructor? 我已经稍微修改了第一个答案的代码,但我无法理解我的错误。它正在编译时出现 0 个错误,但是当我运行它时程序崩溃,devC++ 调试模式也给了我 SIGTRAP 信号。我真的很感激这里的二手货。

所以这个想法是; 改革()将删除旧数组(如有必要,第二个参数是可选的,我只在构造函数中使用它,因为此时指针实际上为空(?))。然后它将使用输入参数大小再次分配它。我真的不知道哪一部分可能是错的。谢谢大家的回答。

#include <iostream>

using namespace std;

class A
{
    private:
        int val;
    public:
        A();
        int GetVal();
        void SetVal(int Val);
};

A::A()
{
    this->val = 0;
}

int A::GetVal()
{
    return this->val;
}

void A::SetVal(int Val)
{
    this->val = Val;
}


#define DEF_A_SIZE 1

class B
{
    private:
        A *a_s;
        int aSize;
        int aCount;
        void reform(const int size, int dlt = 1);
public:
    B();
    ~B();
    void SetSize(const int newSize);
    int GetSize();
    void AddA(A a);
    A GetA(int index);
};

void B::reform(const int size, int dlt)
{
    if(dlt)
    {
        delete[] (this->a_s);
    }
    this->a_s = new A[size];
    this->aCount = 0;
    this->aSize = size;
}

B::B()
{
    reform(DEF_A_SIZE, 0);
}

B::~B()
{
    delete [] (this->a_s);
}

void B::SetSize(const int newSize)
{
    reform(newSize);
}

int B::GetSize()
{
    return this->aSize;
}

void B::AddA(A a)
{
    if (aCount < aSize)
    {
        this->a_s[aCount].SetVal(a.GetVal());
        aCount++;
    }
}

A B::GetA(int index)
{
    if (index < aCount)
    {
        return this->a_s[index];
    }
}

#define B_NUM 1

class C
{
    private:
        B *b_s;
        int bc;
        int bs;
        void reform(const int size, int dlt = 1);
    public:
        C();
        ~C();
        void addb(B b);
        B getB(int index);
};

void C::reform(const int size, int dlt)
{
    if (dlt)
    {
        delete [] (this->b_s);
    }
    bc = 0;
    bs = size;
    b_s = new B[size];
}

void C::addb(B b)
{
    if (bc < bs)
    {
        b_s[bc].SetSize(b.GetSize());
        for (int i = 0; i < b.GetSize(); i++)
        {
            b_s[bc].AddA(b.GetA(i));
        }
        bc++;
    }
}

B C::getB(int index)
{
    if (index < bc)
    {
        return b_s[index];
    }
}

C::C()
{
    reform(B_NUM, 0);
}

C::~C()
{
    delete [] (this->b_s);
}

int main()
{
    C *cptr = new C();

    B b = B();
    b.SetSize(2);

    A a = A();
    a.SetVal(10);
    b.AddA(a);

    A a2 = A();
    a2.SetVal(20);
    b.AddA(a2);

    cptr->addb(b);

    cout << cptr->getB(0).GetA(0).GetVal() << endl;
    cout << cptr->getB(0).GetA(1).GetVal() << endl;

    delete cptr;

    cin.get();

    return 0;
}

已解决

原来我的问题是我的添加或获取功能。我以这种方式改变了它们; Getter 将返回对各个对象的引用(例如 B &getB(int index);) Adder 会将指针作为参数,而不是对象(或值)(例如 void addb(B *b);)

【问题讨论】:

  • 你的代码到底在哪里崩溃了?看来您没有在 B 构造函数中将 a_s 初始化为 0,当您调用 reform 时,它会尝试将 delete[] a_s 设置为垃圾值。尝试将a_s 初始化为0(或c++11 中的nullptr)。如果失败,在你的代码中放置一个断点并观察它崩溃的那一行。
  • 我以为我通过使用第二个参数来改革函数来解决这个问题,在 B 构造函数中我以这种方式调用改革->reform(DEF_A_SIZE, 0);哪个不应该删除 a_s,我现在正在尝试随机的东西,比如更改分别放置在 B 和 C 中的 A 和 B 的吸气剂,以通过在它们之前放置 & 来引用吸气剂。现在我的代码实际上已经到了最后。然后我按 enter 输入 cin.get() 并出现分段错误。

标签: c++ arrays object memory-management


【解决方案1】:

“delete 会调用析构函数吗?”

是的,deletedelete[] 调用受影响类实例的析构函数。

【讨论】:

  • 这不是我的问题。我修改了该页面中发生的代码,因此我没有复制粘贴那里的代码,而是使用了链接。我不知道堆栈溢出会打印链接的显示名称(或其他)
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2012-10-26
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多