Dangling Pointer Memory

I appear to be having difficulty freeing memory and I think that it may be causing leaks, I could use some assistance in figuring out what to do.
Last edited on
Does anyone know how to do this? I've racked my mind for it and searched everywhere.
I started doing this:

main.cpp
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

#include <iostream>
#include "polynomial.h"

using namespace std;
void cls();

int main()
{
    int coeff;
    polynomial first(2);
    polynomial second(2);
    for (int fill = 0; fill < 3; fill++)
    {
        cls();
        cout << "Enter the coefficient of the " << (fill+1) << " term: ";
        cin >> coeff;
        first.set_term(coeff, (2-fill));
    }
    for (int fill = 0; fill < 3; fill++)
    {
        cls();
        cout << "Enter the coefficient of the " << (fill+1) << " term: ";
        cin >> coeff;
        second.set_term(coeff, (2-fill));
    }
    polynomial sum(9);
    sum = first*second;
    first.display_polynomial();
    cout << "\n";
    second.display_polynomial();
        cout << "\n";
    sum.display_polynomial();
    return 0;
}

void cls()
{
    for(int count = 0; count < 50; count++)
    {
        cout << "\n";
    }
}


polynomial.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include "polynomial.h"

using namespace std;

polynomial::polynomial(int hi_term)
{
    count = 0;
    numterm = (hi_term+1);
    cof = new int[numterm];
    pwr = new int[numterm];
}

polynomial::polynomial()
{
    count = 0;
    numterm = (50);
    cof = new int[numterm];
    pwr = new int[numterm];
}

void polynomial::set_term(int cof_val, int pwr_val)
{
    *(pwr+count) = pwr_val;
    *(cof+count) = cof_val;
    count++;
}

void polynomial::display_polynomial()
{
    reset_count();
    for (int dcount = 0; dcount < (numterm-2); dcount++)
    {
        if (*(cof+count) != 0)
        {
            cout << *(cof+count) << "X^" << *(pwr+count) << " + ";
        }
        count++;
    }
    if (*(cof+count) != 0)
    {
        cout << *(cof+count) << "X + ";
    }
    count++;
    if (*(cof+count) != 0)
    {
        cout << *(cof+count);
    }
    count++;
    reset_count();
}

void polynomial::reset_count()
{
    count = 0;
}

polynomial& polynomial::operator=(polynomial& ro)
{
    if (numterm < ro.numterm)
    {        cout << "Error, cannot assign larger polynomial to memory address of smaller polynomial";
        exit(1);
    }
    numterm = ro.numterm;
    for (int i = 0; i < numterm; i++)
    {
        *(cof+i) = *((ro.cof)+i);
        *(pwr+i) = *((ro.pwr)+i);
    }
    return *this;
}

polynomial& polynomial::operator-(polynomial& _2)
{
    int tempterms = 0;
    if (numterm >= _2.numterm)
    {
        tempterms = numterm;
    }
    else if (numterm < _2.numterm)
    {
        tempterms = _2.numterm;
    }
    tempterms--;
    polynomial* temp = new polynomial(tempterms);
    if (*pwr < *(_2.pwr))
    {
        int line = 0;
        while (*pwr < *((_2.pwr)+line))
        {
            line++;
        }
        int lc = 0;
        for (lc; lc<line; lc++)
        {
            *((temp -> cof)+lc) = -(*((_2.cof)+lc));
            *((temp -> pwr)+lc) = *((_2.pwr)+lc);
        }
        int i = 0;
        for (lc; lc<tempterms; lc++)
        {
            *((temp -> cof)+lc) =  *((cof)+ i) - *((_2.cof)+lc);
            *((temp -> pwr)+lc) = *((_2.pwr)+lc);
            i++;
        }
    }
    else if (*pwr > *(_2.pwr))
    {
        int line = 0;
        while (*(pwr+line) > *(_2.pwr))
        {
            line++;
        }
        int lc = 0;
        for (lc; lc<line; lc++)
        {
            *((temp -> cof)+lc) = -(*(cof+lc));
            *((temp -> pwr)+lc) = *(pwr+lc);
        }
        int i = 0;
        for (lc; lc<tempterms; lc++)
        {
            *((temp -> cof)+lc) =  *((cof)+ lc) - *((_2.cof)+i);
            *((temp -> pwr)+lc) = *(pwr+lc);
            i++;
        }
    }
    else if (*pwr == *(_2.pwr))
    {
        for (int i = 0; i<tempterms; i++)
        {
            *((temp -> cof)+i) = *((cof)+ i) - *((_2.cof)+i);
            *((temp -> pwr)+i) = *(pwr+i);
        }
    }
    
    return *temp;
}

polynomial& polynomial::operator+(polynomial& _2)
{
    int tempterms = 0;
    if (numterm >= _2.numterm)
    {
        tempterms = numterm;
    }
    else if (numterm < _2.numterm)
    {
        tempterms = _2.numterm;
    }
    tempterms--;
    polynomial* temp = new polynomial(tempterms);
    if (*pwr < *(_2.pwr))
    {
        int line = 0;
        while (*pwr < *((_2.pwr)+line))
        {
            line++;
        }
        int lc = 0;
        for (lc; lc<line; lc++)
        {
            *((temp -> cof)+lc) = *((_2.cof)+lc);
            *((temp -> pwr)+lc) = *((_2.pwr)+lc);
        }
        int i = 0;
        for (lc; lc<tempterms; lc++)
        {
            *((temp -> cof)+lc) = *((_2.cof)+lc) +*((cof)+ i);
            *((temp -> pwr)+lc) = *((_2.pwr)+lc);
            i++;
        }
        
    }
    else if (*pwr > *(_2.pwr))
    {
        int line = 0;
        while (*(pwr+line) > *(_2.pwr))
        {
            line++;
        }
        int lc = 0;
        for (lc; lc<line; lc++)
        {
            *((temp -> cof)+lc) = *(cof+lc);
            *((temp -> pwr)+lc) = *(pwr+lc);
        }
        int i = 0;
        for (lc; lc<tempterms; lc++)
        {
            *((temp -> cof)+lc) = *((_2.cof)+i) + *((cof)+ lc);
            *((temp -> pwr)+lc) = *(pwr+lc);
            i++;
        }
    }
    else if (*pwr == *(_2.pwr))
    {
        for (int i = 0; i<tempterms; i++)
        {
            *((temp -> cof)+i) = *((_2.cof)+i) + *((cof)+ i);
            *((temp -> pwr)+i) = *(pwr+i);
        }
    }
    return *temp;
}

polynomial& polynomial::operator*(polynomial& _2)
{
    int tempterms;
    tempterms = (numterm*_2.numterm);
    polynomial* sub_temp = new polynomial(tempterms-1);
    int subterm = 0;
    for (int j = 0; j < numterm; j++)
    {
        for (int i = 0; i < _2.numterm; i++)
        {
            *((sub_temp -> cof)+subterm) = *(cof+j) * *((_2.cof)+i);
            *((sub_temp -> pwr)+subterm) = *(pwr+j) + *((_2.pwr)+i);
            subterm++;
        }
    }
    int terms;
    terms = ((numterm-1)+((_2.numterm)-1));
    polynomial* temp = new polynomial(terms);
    for (int i = 0; i < temp->numterm; i++)
    {
        int cumsum = 0;
        for (int j = 0; j < sub_temp->numterm; j++)
        {
            if (*((sub_temp->pwr)+j) == temp->numterm-(i+1))
            {
                cumsum += *((sub_temp->cof)+j);
            }
        }
        *((temp->cof)+i) = cumsum;
        *((temp->pwr)+i) = (temp->numterm-(i+1));
    }
    
    
    delete sub_temp;
    //delete temp;
    return *temp;
}

polynomial::~polynomial()
{
    delete [] cof;
    delete [] pwr;
}


polynomial.h
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
#ifndef ___1_Polynomials__polynomial__INCLUDED__
#define ___1_Polynomials__polynomial__INCLUDED__

#include <iostream>

class polynomial{
private:
    int* cof;
    int* pwr;
    int numterm;
    int count;
    void reset_count();
public:
    polynomial();
    polynomial(polynomial&);
    polynomial(int);
    void set_term(int, int);
    void display_polynomial();
    polynomial& operator=(polynomial&);
    polynomial& operator+(polynomial&);
    polynomial& operator-(polynomial&);
    polynomial& operator*(polynomial&);
    ~polynomial();
};

#endif 

Last edited on
Fixed the bad access error and now I'm wondering how to free the memory used in all of the temp objects?
Updated the code up top
Last edited on
No need to use new for those temp objects, and with the exception of your assignment operator, you should be returning copies for the operators you've implemented.

For some general guidelines, see:-
http://stackoverflow.com/questions/4421706/operator-overloading
http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
Topic archived. No new replies allowed.