gpt4 book ai didi

c++ - 双重自由或腐败(fasttop)——C++

转载 作者:行者123 更新时间:2023-11-30 03:26:04 24 4
gpt4 key购买 nike

我刚遇到一个奇怪的问题。我将在下面粘贴完整的代码:

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

using namespace std;

// declare a base class
class Base
{
public:
Base();
Base(int n, int arr[2]);
Base(const Base &base);

Base &operator=(const Base &rhs);

int getSize() const;
int *getA() const;

~Base();

private:
int *a;
int size;
};

class Case
{
public:
Case();
Case(int n, int arr[2]);
Case(const Case &rhs);

Case &operator=(const Case &rhs);

int getSize() const;
int *getA() const;

~Case();

private:
int *a;
int size;
};

class Dase
{
public:
Dase();
Dase(int bSize, int barr[2], int cSize, int carr[2]);
Dase(const Dase &dase);

Dase &operator=(const Dase &rhs);

Base getB() const;
Case getC() const;

~Dase();

private:
Base b;
Case c;
};

// implementation
Base::Base() : size(0)
{
a = NULL;
}

Base::Base(int n, int arr[2]) : size(n)
{
a = new int[n];
for (int i = 0; i < size; i++)
a[i] = arr[i];
}

Base::Base(const Base &base)
{
size = base.getSize();

a = new int[size];
for (int i = 0; i < size; i++)
a[i] = base.getA()[i];
}

Base &Base::operator=(const Base &rhs)
{
if (this == &rhs)
return *this;

size = rhs.getSize();
delete[] a;
a = new int[size];
for (int i = 0; i < size; i++)
a[i] = rhs.getA()[i];
return *this;
}

int *Base::getA() const
{
return a;
}

int Base::getSize() const
{
return size;
}

Base::~Base()
{
delete[] a;
}

Case::Case() : size(0)
{
a = NULL;
}

Case::Case(int n, int arr[2]) : size(n)
{
a = new int[n];
for (int i = 0; i < size; i++)
a[i] = arr[i];
}

Case::Case(const Case &rhs)
{
size = rhs.getSize();

a = new int[size];
for (int i = 0; i < size; i++)
a[i] = rhs.getA()[i];
}

Case &Case::operator=(const Case &rhs)
{
if (this == &rhs)
return *this;

size = rhs.getSize();
delete[] a;
a = new int[size];
for (int i = 0; i < size; i++)
a[i] = rhs.getA()[i];

return *this;
}

int *Case::getA() const
{
return a;
}

int Case::getSize() const
{
return size;
}

Case::~Case()
{
delete[] a;
}

// implement class Dase
Dase::Dase() : b(Base()), c(Case())
{
// delebrately left empty
}

Dase::Dase(int bSize, int barr[2], int cSize, int carr[2])
{
b = Base(bSize, barr);
c = Case(cSize, carr);
}

Dase::Dase(const Dase &dase)
{
b = dase.getB();
c = dase.getC();
}

Dase &Dase::operator=(const Dase &rhs)
{
if (this == &rhs)
return *this;

b = rhs.getB();
c = rhs.getC();

return *this;
}

Base Dase::getB() const
{
return b;
}

Case Dase::getC() const
{
return c;
}

Dase::~Dase()
{
b.~Base();
c.~Case();
}

在上面的代码中,我定义了 3 个类:Base、Case、Dase。我包括了他们的声明和实现。以下是主要代码:

#include "classes.h"

int main()
{
int arr[2] = {1, 2};
int brr[2] = {3, 4};

Dase d1(2, arr, 2, brr);
Dase d2;

d2 = d1;
}

这是一个非常简单的主代码,但我在运行时遇到了“double free or corruption (fasttop)”错误。

我注意到当我删除类 Dase 中的析构函数时,这个问题就消失了。如果我想保留 Dase 的析构函数,我该怎么做才能解决这个问题?我应该更改它的实现吗?

谢谢!

最佳答案

你不应该明确地调用析构函数。它是自动完成的。

所以,替换

Dase::~Dase()
{
b.~Base();
c.~Case();
}

Dase::~Dase()
{
}

关于c++ - 双重自由或腐败(fasttop)——C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48593302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com