Having trouble with dynamic memory. Any help would be greatly appreciated. Thanks!

I've begun constructing a class that overrides the default limitation of the size of an integer by employing dynamic memory and arrays. SPECIFICALLY MY OVERLOADED ADD OPERATOR METHOD IS NOT FUNCTIONING PROPERLY. It works when the two HugeInteger objects are equally sized; otherwise, it fails. Nonetheless, I am not receiving segmentation faults. thanks for any help!!!

Here is my cpp file (the driver file simply adds two objects of HugeInteger)

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
#include "integer.h"

        //////////////////
        //global methods//
        //////////////////

        //
        //overloaded istream operator
        istream &operator>>(istream &in, HugeInteger &x){

                x.input(in);

                return in;


        }

        //
        //overloaded ostream operator
        ostream &operator<<(ostream &out, HugeInteger &x){

                x.display(out);

                return out;

        }

        /////////////////
        //class methods//
        /////////////////

        //
        //default constructor

        HugeInteger::HugeInteger(){
                number = NULL;
                sigDigits = 0;
        }

        //
        //accessor method to return static member MAXDIGITS
        int HugeInteger::getMAXDIGITS(){

                return MAXDIGITS;

        }

        //
        //overloaded addition operator method to add two strings of numbers
        HugeInteger HugeInteger::operator+(const HugeInteger &x){

                HugeInteger sum;

                if(x.sigDigits>=sigDigits){
                        sum.sigDigits = x.sigDigits+1;
                }else{
                        sum.sigDigits = sigDigits+1;
                }

                sum.number = new int[sum.sigDigits];

                for (int z=0;z<sum.sigDigits;z++){
                        sum.number[z]=0;
                }

                for(int i=sigDigits-1,j=x.sigDigits-1,k=sum.sigDigits-1;i>=-1,j>=-1,k>=0;i--,j--,k--){

                        if(j!=-1&&i!=-1){
                                sum.number[k]=number[i]+x.number[j];
                        }else if(j==-1&&i!=-1){
                                sum.number[k]=number[i];
                        }else if(i==-1&&j!=-1){
                                sum.number[k]=x.number[j];
                        }else{
                                break;
                        }

                        if(sum.number[k]>=10){
                                sum.number[k]-=10;
                                sum.number[k+1]+=1;
                        }
                }
                return sum;
        }

        //
        //overloaded multiplication operator method to multiply two strings of numbers
        HugeInteger HugeInteger::operator*(const HugeInteger &x){

                /*
                HugeInteger product;
                return product;
                */
        }

        //
        //overloaded equality method that returns a boolean value accordingly
        bool HugeInteger::operator==(const HugeInteger &x){
                bool isEqual = false;

                if(sigDigits == x.sigDigits){
                        for(int i=MAXDIGITS; i>MAXDIGITS-sigDigits;i--){
                                if(number[i] != x.number[i]){
                                        break;
                                }else{
                                        isEqual = true;
                                }
                        }
                }

                return isEqual;
        }

        //
        //overloaded anti-equality method that returns a boolean value accordingly
        bool HugeInteger::operator!=(const HugeInteger &x){
                bool isnotEqual=true;

                if(*this==x){
                        isnotEqual = false;
                }

                return isnotEqual;
        }

        //
        //overloaded greater than method that returns an appropriate boolean value
        bool HugeInteger::operator>(const HugeInteger &x){
                bool isGreater = false;

                if(sigDigits>x.sigDigits){
                        isGreater=true;
                }else if(sigDigits == x.sigDigits){
                        for(int i=MAXDIGITS-sigDigits;i<MAXDIGITS;i++){
                                if(number[i] > x.number[i]){
                                        isGreater = true;
                                        break;
                                }
                        }
                }else{
                        isGreater=false;
                }
                return isGreater;
        }

        //
        //overloaded greater than or equal to method that returns an appropriate boolean value
        bool HugeInteger::operator>=(const HugeInteger &x){
                bool isGTorEQ=false;

                if((*this == x) || (*this>x)){
                        isGTorEQ = true;
                }
                return isGTorEQ;
        }

        //
        //overloaded less than method that returns a boolean value accordingly
        bool HugeInteger::operator<(const HugeInteger &x){

                return ( !(*this > x) );
        }

        //
        //overloaded less than or equal to operator that returns a relevant boolean value
        bool HugeInteger::operator<=(const HugeInteger &x){

                 bool isLTorEQ=false;

                if((*this == x) || (*this<x)){
                        isLTorEQ = true;
                }

                return isLTorEQ;
        }

        //
        //overloaded equality operator method
        HugeInteger HugeInteger::operator=(const HugeInteger &x){

                if(number == NULL){
                        number = new int[x.sigDigits];
                }else{
                        delete [] number;
                        number = new int[x.sigDigits];
                }

                sigDigits = x.sigDigits;

                for(int i=x.sigDigits;i>=0;i--){
                        number[i] = x.number[i];
                }

                return *this;
        }

        //
        //method to check if a given number is zero
        bool HugeInteger::isZero(const HugeInteger &x){
                bool isZero = false;

                if (sigDigits != 0){
                        for(int i=MAXDIGITS;i>MAXDIGITS-sigDigits;i--){
                                if(x.number[i]!=0){
                                        isZero = false;
                                }
                        }
                }
                return isZero;
        }

        //
        //input method
        void HugeInteger::input(istream &in){

                string temp;
                in >> temp;
                sigDigits = 0;
                number = new int[temp.length()];

                for(int i = temp.length()-1;i>=0;i--){
                        char x = temp[i];
                        number[i] = atoi(&x);
                        sigDigits++;
                }
        }

        //
        //display method
        void HugeInteger::display(ostream &out){

                for(int i=0;i<sigDigits;i++){

                        out << number[i];
                }
        }

.
Last edited on
There are a few issues with your code.
for(int i=sigDigits-1,j=x.sigDigits-1,k=sum.sigDigits-1;i>=-1,j>=-1,k>=0;i--,j--,k--){

The condition: i>= -1, j >= -1, k>=0 is equivalent to: k >= 0.

operator+ can be implemented in terms of operator+= as a non-member function.

operator= should not return a copy.

The way you use atoi in HugeInteger::input is wrong.

I don't see a copy constructor or destructor, both of which should be implemented for your type.


SakurasouBusters wrote:
Send me a private message.

I would suggest not sending a private message to the local troll.
Last edited on
@cire

I did not know SakarusouBusters was a troll. It seems obvious now.

Always seemed to criticize my responses and only gave me a reply saying that my code doesn't compile. Not helpful.

I'll keep this in mind.
@boost lexical cast
How about you?
If your code does not even compile, how can it possibly help other people?
@SakurasouBusters

Every time I fixed my code and those times where my code didn't compile were 1 - 2 times...

I actually helped while you spammed "send me private message" and didn't help at all. I'm not arguing anymore.
Last edited on
Topic archived. No new replies allowed.