简体   繁体   English

在复制构造函数中释放 memory 的问题

[英]Problems freeing memory in a copy constructor

I have this parent class我有这个父 class

class Monster 
{
    char* nume;
    double hp;
    float* dmgAbilitati;
    int nrAbilitati;
}

with this copy constructor使用此复制构造函数

Monster(const Monster& src)
    {
        if (nume != nullptr)
            delete[] nume;
        this->nume = new char[strlen(src.nume) + 1];
        strcpy_s(this->nume, strlen(src.nume) + 1, src.nume);
        this->hp = src.hp;
        this->nrAbilitati = src.nrAbilitati;
        if (dmgAbilitati != nullptr)
            delete[] dmgAbilitati;
        this->dmgAbilitati = new float[this->nrAbilitati];
        for (int i = 0; i < this->nrAbilitati; i++)
            this->dmgAbilitati[i] = src.dmgAbilitati[i];
    }

and I am asking if it is mandatory to use these statements我在问是否必须使用这些语句

if (nume != nullptr)
            delete[] nume;
if (dmgAbilitati != nullptr)
            delete[] dmgAbilitati;

because I am trying to overload the pre/post-increment operator in this child class因为我试图重载这个子 class 中的前/后增量运算符

class giantSpider : private Monster
{
    int durataStun;

.
.
.
const giantSpider operator++(int)
    {
        giantSpider aux(*this);
        durataStun++;
        return aux;
    }
}

and it throws an exception like in the image below(sometimes works tho), and if I'm not including those statements, everything is fine.它会引发如下图所示的异常(有时也可以),如果我不包括这些陈述,一切都很好。

I am using this constructor to initialize the parameters我正在使用这个构造函数来初始化参数

Monster(const char* nume, double hp, int nrAbilitati, float* dmgAbilitati)
    {
        if (nume == nullptr)
            throw new exception("Nume invalid!\n");
        else
        {
            this->nume = new char[strlen(nume) + 1];
            strcpy_s(this->nume, strlen(nume) + 1, nume);
        }
        if (hp <= 0)
            throw new exception("Hp invalid!\n");
        else
            this->hp = hp;
        if (nrAbilitati <= 0 && dmgAbilitati == nullptr)
            throw new exception("nrAbilitati invalid sau dmgAbilitati invalid!\n");
        else
        {
            this->nrAbilitati = nrAbilitati;
            this->dmgAbilitati = new float[nrAbilitati];
            for (int i = 0; i < nrAbilitati; i++)
                this->dmgAbilitati[i] = dmgAbilitati[i];
        }
    }

and for the child:对于孩子:

giantSpider(const char* nume, double hp, int nrAbilitati, float* dmgAbilitati, int durataStun)
        :Monster(nume, hp, nrAbilitati, dmgAbilitati)
    {
        if (durataStun <= 0)
            throw new exception("Numar introdus invalid!\n");
        else
            this->durataStun = durataStun;
    }

and this is in main:这主要是:

float v1[] = { 125,234.22,8643.3 };
giantSpider  g2("Ana", 1000, 3, v1, 3);
cout << g2 << ++g2 << g2++ ;

I have already overloaded the << operator.我已经重载了 << 运算符。

https://i.stack.imgur.com/u6kiz.png https://i.stack.imgur.com/u6kiz.png

These statements这些陈述

if (nume != nullptr)
            delete[] nume;
if (dmgAbilitati != nullptr)
            delete[] dmgAbilitati;

do not make a sense because the data members nume and dmgAbilitati are not initialized yet.没有意义,因为数据成员numedmgAbilitati尚未初始化。 More precisely neither memory was allocated where they would point to.更准确地说,memory 都没有分配到它们指向的位置。

So these statements invoke undefined behavior.所以这些语句调用未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM