简体   繁体   English

我刚刚了解了 C++ 中的动态 memory 分配

[英]I just learned about dynamic memory allocation in C++

I need to prevent memory leak as It dynamically allocate and delete properly for his homework.我需要防止 memory 泄漏,因为它会为他的作业动态分配和删除。 My homework code (large) didn't work.我的作业代码(大)不起作用。 So I made small example code to understand better, but it doesn't work neither.所以我制作了小示例代码以更好地理解,但它也不起作用。

include <iostream>
using namespace std;

const int MAX=5;
class son;
class dau;

class papa
{
public:
    papa(int pair);
    void show();
    int pair();
    void allocate(int howmanyeach);

private:
    son *sonpnt[MAX];
    dau *daupnt[MAX];
    int m_pair;

};

class son
{
public:
    son(int don);
    int money();

private:
    int m_money=0;        
};

class dau
{
public:
    dau(int don);
    int money();

private:
    int m_money=0;    
};

//////////////////////////////////////////

papa::papa(int pair)
{
    m_pair=pair;
}

int papa::pair()
{
    return m_pair;
}

void papa:: allocate(int howmanyeach)
{
    if( howmanyeach > MAX || howmanyeach<1)
        cout<<"impossible"<<endl;
    else
        for(int i=0; i<howmanyeach; i++)
        {
            sonpnt[i]=new son(7);
            daupnt[i]=new dau(5);
        }
}

void papa::show()
{
    for(int i=0;i<MAX; i++)
    {
        if(sonpnt[i]!=nullptr)
            cout<<sonpnt[i]->money();
    }
    cout<<'\n';

    for(int i=0;i<MAX; i++)
    {
        if(daupnt[i]!=nullptr)
            cout<<daupnt[i]->money();
    }        
}

son::son(int don)
{
    m_money+=don;
}

int son::money()
{
    return m_money;
}

dau::dau(int don)
{
    m_money+=don;
}

int dau::money()
{
    return m_money;
}

/////////////////////////////

int main()
{
    papa p(5);
    p.allocate(1);
    p.show();   
}

It can be helpful if you can point it out Also, any advice about my main project would be helpful.如果你能指出它会很有帮助另外,关于我的主要项目的任何建议都会有所帮助。 I have three classes that I need to get things from each others.我有三个班级,我需要从彼此那里得到东西。 It's a game.游戏而已。 player, monster, field are the classes. player, monster, field 是类。 I am not allowed to touch class public interfaces.我不允许触摸 class 公共接口。 The challenge is thatcI need to new and delete properly with a private member m_pointer and a private member array of pointers m_monster,which has its size some global const like MAX.挑战在于,我需要使用私有成员 m_pointer 和指针的私有成员数组 m_monster 正确地新建和删除,其大小为一些全局常量,如 MAX。 I think, except the challenge, my code seems ok.我认为,除了挑战之外,我的代码似乎还可以。 This dynamic thingy I just learned is the bummer.我刚刚学到的这个动态的东西真是太糟糕了。 I want to at least try my small case code properly.我想至少正确地尝试我的小案例代码。 What should be tried on this code?应该对此代码进行什么尝试?

Thanks谢谢

As stated above, you should delete dynamically allocated objects of papa in it's destructor.如上所述,您应该在其析构函数中删除动态分配的 papa 对象。 As you might know, C++ provides default constructor for each class but it doesn't include dynamically allocated pointers.您可能知道,C++ 为每个 class 提供默认构造函数,但它不包括动态分配的指针。 You should add a destructor:您应该添加一个析构函数:

papa::~papa()
{
    for(int i=0;i<MAX; i++)
    {
        delete sonpnt[i];
        delete daupnt[i];
    }   
}

I would have changed the constructor as well to explicitly initialize these pointers to null ptr:我也会更改构造函数以显式初始化这些指向 null ptr 的指针:

papa::papa(int pair)
{
  for(int i=0;i<MAX; i++)
  {
    sonpnt[i] = nullptr;
    daupnt[i] = nullptr;
  }
    m_pair=pair;
}

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

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