c++ memory leak problem...HELP..~~~

when I run it always appear, it stops work....
I don't know why, my classmate said it is about the memory leak problem, but he
also don't know how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include<fstream>
#include<string>
using namespace std;


class expensive
{

private:
    int len;
    string *Name = new string[len];
    double *Money = new double [len];
    string *Breakfast = new string[4];
    string *Lunch = new string[5];
    string *Dinner = new string[4];
    double b=0,l=0,d=0;
public:
    void getcategory()
    {
        fstream infile("Categ.txt");
        for(int i=0;i<4;i++)
        {
            infile>>Breakfast[i];
        }
        for(int i=0;i<5;i++)
        {
            infile>>Lunch[i];
        }
        for(int i=0;i<4;i++)
        {
            infile>>Dinner[i];
        }
    }

    void getexpensive1()
    {
        fstream infile("expense.txt");
        len=12;
        for(int i=0;i<len;i++)
        {
            infile >> Name[i]>>Money[i];
        }
    }

    void getexpensive2()
    {
        fstream infile("expense2.txt");
        len=13;
        for(int i=0;i<len;i++)
        {
            infile >> Name[i]>>Money[i];
        }
    }

    void getexpensive3()
    {
        fstream infile("expense3.txt");
        len=8;
        for(int i=0;i<len;i++)
        {
            infile >> Name[i]>>Money[i];
        }
    }


    void totalexpensive()
    {
        for(int i=0;i<4;i++)
            for(int j=0;j<20;j++)
        {
            if(Breakfast[i]==Name[j])
                b+=Money[j];
        }
        for(int i=0;i<5;i++)
            for(int j=0;j<20;j++)
        {
            if(Lunch[i]==Name[j])
                l+=Money[j];
        }
        for(int i=0;i<4;i++)
            for(int j=0;j<20;j++)
        {
            if(Dinner[i]==Name[j])
                d+=Money[j];
        }
    }
    void display()
    {
        cout<<"Total expensive report: "<<endl;
        cout<<Breakfast[0]<<"   "<<b<<endl;
        cout<<Lunch[0]<<"  "<<l<<endl;
        cout<<Dinner[0]<<"  "<<d<<endl;
        cout<<endl;
    }

    void update()
    {
        ofstream outfile("report.txt");
        outfile<<"Total expensive report: "<<endl;
        outfile<<Breakfast[0]<<"   "<<b<<endl;
        outfile<<Lunch[0]<<"  "<<l<<endl;
        outfile<<Dinner[0]<<"  "<<d<<endl;
        outfile<<endl;
    }
};

int main()
{
    int command=-1;
    cout <<"Input integers for this program.\nChoose which expense category you want to calculate.\nCommand list:\n1.expense1.\n2.expense2.\n3.expense3.\n0.exit.\n";

    while(command){
        cin>>command;
        switch (command){
            case 0:
                cout << "exit.";
                break;
            case 1:
                {
                    expensive e1;
                    e1.getexpensive1();
                    e1.getcategory();
                    e1.totalexpensive();
                    e1.display();
                    e1.update();
                    break;
                }
            case 2:
                {
                    expensive e2;
                    e2.getexpensive2();
                    e2.getcategory();
                    e2.totalexpensive();
                    e2.display();
                    e2.update();
                    break;
                }
            case 3:
                {
                    expensive e3;
                    e3.getexpensive3();
                    e3.getcategory();
                    e3.totalexpensive();
                    e3.display();
                    e3.update();
                    break;
                }
        }
    }

    return 0;
}
1. expensive has no user-defined destructor, so all pointers it has allocated (lines 12-16) are leaked every time an instance goes out of scope/is deleted.
2. expensive::Name and expensive::Money are initialized to arrays of unknown length, because expensive::len is not initialized at construction.
3. Lines 39, 49, and 59: Just setting expensive::len does not affect the length of the arrays pointed to by expensive::Name or expensive::Money.
4. expensive::totalexpensive() attempts to read the arrays pointed to by expensive::Name and expensive::Money up to offset 19, even though no function appears to write to these arrays past offset 12.

Overall the class is ill-defined, in my opinion. For example, expensive::getexpensive1(), expensive::getexpensive2(), and expensive::getexpensive3() should be a single function at most. It doesn't seem like the class should concern itself with I/O. I would suggest redoing it from scratch.
Topic archived. No new replies allowed.