candito123
candito123

Reputation: 65

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. 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. I am not allowed to touch class public interfaces. 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. 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

Upvotes: 1

Views: 126

Answers (1)

Yitshak Yarom
Yitshak Yarom

Reputation: 84

As stated above, you should delete dynamically allocated objects of papa in it's destructor. As you might know, C++ provides default constructor for each class but it doesn't include dynamically allocated pointers. 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:

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

Upvotes: 1

Related Questions