Heap Corruption in overloaded operators

I've been building a Big Integer calculator. If I use a member function to multiply the BigInts together, then everything works fine. When I move my multiply function into an overloaded * or *= function, I start getting heap corruptions. Can anyone help me find where the problem lies? I did the same thing with my add function and have no problems there, just with the multiplication.

Here is the add multiplication functions I was using:
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
BigInteger BigInteger::add(const BigInteger& rhs)
{
	BigInteger result;
	unsigned int length = (this->m_digitCount > rhs.m_digitCount) ? this->m_digitCount : rhs.m_digitCount;

	int carry = 0;
	for (unsigned int digit = 0; digit < length; digit++)
	{
		int v1 = this->getDigit(digit);
		int v2 = rhs.getDigit(digit);
		int sum = v1 + v2 + carry;
		int single = sum % 10;
		carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;

		result.setDigit(digit, single);
	}
	if (carry > 0)
	{
		result.setDigit(length, carry);
	}

	return result;
}

BigInteger BigInteger::multiply(const BigInteger& rhs)
{
	BigInteger result;
	const BigInteger& b = (this->m_digitCount < rhs.m_digitCount) ? *this : rhs;
	const BigInteger& t = (this->m_digitCount < rhs.m_digitCount) ? rhs : *this;

	for (unsigned int bDigit = 0; bDigit < b.m_digitCount; bDigit++)
	{
		BigInteger temp(0);
		int v1 = b.getDigit(bDigit);
		int carry = 0;
		for (unsigned int tDigit = 0; tDigit < t.m_digitCount; tDigit++)
		{
			int v2 = t.getDigit(tDigit);
			int sum = v1 * v2 + carry;
			int single = sum % 10;
			carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;

			temp.setDigit(bDigit + tDigit, single);
		}
		if (carry > 0)
		{
			temp.setDigit(bDigit + t.m_digitCount, carry);
		}

		BigInteger temp2 = result.add(temp);
		result = temp2;

	}

	return result;
}


From BigInteger.hpp
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
#ifndef _BIGINTEGER_HPP_
#define _BIGINTEGER_HPP_

#include <cstdint>
#include <iostream>
#include <string>

class TestCases;

class BigInteger
{
public:
	BigInteger()
	{
		m_sizeReserved = 4;
		m_digitCount = 0;
		m_number = new uint8_t[m_sizeReserved];
	}
	BigInteger(int number) 
	{
		int inbound = number;
		m_digitCount = 1;
		for (unsigned int i = number; i > 1; i /= 10)
		{
			++m_digitCount;
		}
		m_sizeReserved = 4;
		while (m_sizeReserved <= m_digitCount)
		{
			m_sizeReserved *= 2;
		}
		m_number = new uint8_t[m_sizeReserved];
		for (unsigned int i = 0; i < m_digitCount; ++i)
		{
			uint8_t input = inbound % 10;
			setDigit(i, input);
			inbound /= 10;
		}
	}
	BigInteger(std::string number)
	{
		m_digitCount = number.size();
		m_sizeReserved = 4;
		while (m_sizeReserved <= m_digitCount)
		{
			m_sizeReserved *= 2;
		}
		m_number = new uint8_t[m_sizeReserved];
		for (unsigned int i = 0; i < m_digitCount; ++i)
		{
			unsigned int input = (number[m_digitCount - i - 1] - '0');
			setDigit(i, input);
		}
	}
	BigInteger(const BigInteger& rOther)
	{
		m_sizeReserved = rOther.m_sizeReserved;
		m_digitCount = rOther.m_digitCount;
		m_number = new uint8_t[m_sizeReserved];
		for (unsigned int  i = 0; i < m_digitCount; ++i)
		{
			setDigit(i, rOther.m_number[i]);
		}
	}

	//overridden opperators
	BigInteger operator=(const BigInteger& rOther);
	BigInteger operator+(const BigInteger& rhs);
	BigInteger operator+(const unsigned int& rhs);
	BigInteger operator+=(const BigInteger& rhs);
	BigInteger operator++(int);
	BigInteger operator*(const BigInteger& rhs);
	BigInteger operator*=(const BigInteger& rhs);
	operator double();
	bool operator<=(const BigInteger& rhs);
	bool operator==(const BigInteger& rhs);
	friend std::ostream& operator<<(std::ostream& os, const BigInteger rhs);
	
	friend TestCases; // TODO Have students do this first thing

	~BigInteger()
	{
		for (unsigned int i = 0; i < m_sizeReserved; ++i)
		{
			m_number[i] = NULL;
		}
		m_digitCount = NULL;
		m_sizeReserved = NULL;
		delete[] m_number;
	}

private:
	std::uint8_t* m_number;		// Internal representation of the number.
	unsigned int m_sizeReserved;	// Total size of the allocated space used to internally store the number
	unsigned int m_digitCount;	// How many digits are in the number.

	std::uint8_t getDigit(unsigned int position) const;
	void setDigit(unsigned int position, std::uint8_t digit);
};

#endif 


I'll add another post with BigInteger.cpp and main.cpp
BigInteger.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
250
251
252
253
#include <iostream>
#include <algorithm>
#include <cmath>

#include "BigInteger.hpp"


// ------------------------------------------------------------------
//
// Returns the digit as the specified positon.  If the position is greater
// that the number representation, a 0 is returned.
//
// ------------------------------------------------------------------
std::uint8_t BigInteger::getDigit(unsigned int position) const
{
	if (position < m_digitCount)
	{
		return m_number[position];
	}

	return 0;
}

void BigInteger::setDigit(unsigned int position, uint8_t digit)
{
	if (position < m_digitCount)
	{
		m_number[position] = digit;
	}
	else
	{
		if (position + 1 < m_sizeReserved)
		{
			m_digitCount = position + 1;
			m_number[position] = digit;
		}
		else
		{
			while (m_sizeReserved <= position)
			{
				m_sizeReserved *= 2;
			}
			uint8_t* copy_m_number = new uint8_t[m_sizeReserved];
			m_digitCount = position + 1;
			for (unsigned int i = 0; i < m_digitCount; ++i)
			{
				if (m_number[i] < 10)
				{
					copy_m_number[i] = m_number[i];
					m_number[i] = NULL;
				}
				else
				{
					copy_m_number[i] = 0;
				}
			}
			delete[] m_number;
			m_number = new uint8_t[m_sizeReserved];
			
			for (unsigned int i = 0; i < m_digitCount; ++i)
			{
				m_number[i] = copy_m_number[i];
				copy_m_number[i] = NULL;
			}
			delete[] copy_m_number;
			m_number[position] = digit;
		}

	}
}

BigInteger BigInteger::operator= (const BigInteger& rOther) // overridden
{
	for (unsigned int i = 0; i < m_digitCount; ++i)
	{
		this->m_number[i] = NULL;
	}
	this->m_sizeReserved = rOther.m_sizeReserved;
	this->m_digitCount = rOther.m_digitCount;
	delete[] this->m_number;
	this->m_number = new uint8_t[this->m_sizeReserved];
	for (unsigned int i = 0; i < m_digitCount; ++i)
	{
		this->setDigit(i, rOther.m_number[i]);
	}
	return *this;
}

BigInteger BigInteger::operator+(const BigInteger& rhs)
{
	BigInteger result;
	unsigned int length = (this->m_digitCount > rhs.m_digitCount) ? this->m_digitCount : rhs.m_digitCount;
	result.m_digitCount = length;
	while (result.m_digitCount > result.m_sizeReserved)
	{
		result.m_sizeReserved *= 2;
		result.m_number = new std::uint8_t[result.m_sizeReserved];
		for (unsigned int i = 0; i < result.m_digitCount; ++i)
		{
			result.setDigit(i, 0);
		}
	}
	int carry = 0;
	for (unsigned int digit = 0; digit < length; digit++)
	{
		int v1 = this->getDigit(digit);
		int v2 = rhs.getDigit(digit);
		int sum = v1 + v2 + carry;
		int single = sum % 10;
		carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;

		result.setDigit(digit, single);
	}
	if (carry > 0)
	{
		result.setDigit(length, carry);
	}

	return result;
}

BigInteger BigInteger::operator+(const unsigned int& inboundInt)
{
	BigInteger rhs(inboundInt);
	BigInteger result;
	unsigned int length = (this->m_digitCount > rhs.m_digitCount) ? this->m_digitCount : rhs.m_digitCount;
	result.m_digitCount = length;
	while (result.m_digitCount > result.m_sizeReserved)
	{
		result.m_sizeReserved *= 2;
		result.m_number = new std::uint8_t[result.m_sizeReserved];
		for (unsigned int i = 0; i < result.m_digitCount; ++i)
		{
			result.setDigit(i, 0);
		}
	}
	int carry = 0;
	for (unsigned int digit = 0; digit < length; digit++)
	{
		int v1 = this->getDigit(digit);
		int v2 = rhs.getDigit(digit);
		int sum = v1 + v2 + carry;
		int single = sum % 10;
		carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;

		result.setDigit(digit, single);
	}
	if (carry > 0)
	{
		result.setDigit(length, carry);
	}
	return result;
}

BigInteger BigInteger::operator+=(const BigInteger& rhs)
{
	BigInteger result = *this + rhs;
	*this = result;
	return *this;
}

BigInteger BigInteger::operator++(int)
{
	BigInteger temp = *this;
	BigInteger postfix(1);
	*this += postfix;
	return temp;
}

BigInteger BigInteger::operator*(const BigInteger& rhs)
{
	BigInteger result;
	const BigInteger& b = *this;
	const BigInteger& t = rhs;

	for (unsigned int bDigit = 0; bDigit < b.m_digitCount; bDigit++)
	{
		BigInteger temp(0);
		int v1 = b.getDigit(bDigit);
		int carry = 0;
		for (unsigned int tDigit = 0; tDigit < t.m_digitCount; tDigit++)
		{
			int v2 = t.getDigit(tDigit);
			int sum = v1 * v2 + carry;
			int single = sum % 10;
			carry = ((sum - single) > 0) ? (sum - single) / 10 : 0;

			temp.setDigit(bDigit + tDigit, single);
		}
		if (carry > 0)
		{
			temp.setDigit(bDigit + t.m_digitCount, carry);
		}
		result += temp;
	}

	return result;
}

BigInteger BigInteger::operator*=(const BigInteger& rhs)
{
	BigInteger result = *this * rhs;
	*this = result;
	return *this;
}

BigInteger::operator double()
{
	double result = 0;
	for (unsigned int i = 0; i < this->m_digitCount; ++i) 
	{
		result += (this->m_number[i] * pow(10, i));
	}
	return result;
}

bool BigInteger::operator<=(const BigInteger& rhs)
{
	if (this->m_digitCount < rhs.m_digitCount) return true;
	if (this->m_digitCount > rhs.m_digitCount) return false;
	// If m_digitCount for both are equal check digit by digit
	for (int digit = m_digitCount - 1; digit >= 0; digit--)
	{
		if (this->m_number[digit] < rhs.m_number[digit]) return true;
		if (this->m_number[digit] > rhs.m_number[digit]) return false;
	}
	//if they are equal, return true
	return true;
}

bool BigInteger::operator==(const BigInteger& rhs)
{
	if (this->m_digitCount < rhs.m_digitCount) return false;
	if (this->m_digitCount > rhs.m_digitCount) return false;
	// If m_digitCount for both are equal check digit by digit
	for (int digit = m_digitCount - 1; digit >= 0; digit--)
	{
		if (this->m_number[digit] < rhs.m_number[digit]) return false;
		if (this->m_number[digit] > rhs.m_number[digit]) return false;
	}
	//if they are equal, return true
	return true;
}

std::ostream& operator<<(std::ostream& os, const BigInteger rhs)
{
	for (unsigned int digit = rhs.m_digitCount; digit > 0; digit--)
	{
		std::cout << static_cast<int>(rhs.m_number[digit - 1]);
	}
	return os;
}


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
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

#include "BigInteger.hpp"

// ------------------------------------------------------------------
//
// Site to compare long factorial results: http://www.dcode.fr/factorial
//
// ------------------------------------------------------------------
BigInteger factorial(BigInteger value)
{
	BigInteger result(1);

	for (BigInteger next = 1; next <= value; next++)
	{
		result *= next;
	}

	return result;
}

int main()
{
	BigInteger one(1234);
	BigInteger two(9999);
	BigInteger result = one + two;
	std::cout << "one + two = " << result << std::endl;
	std::cout << "one + two (as double) = " << static_cast<double>(result) << std::endl;
	one += two;
	std::cout << "one += two = " << one << std::endl;
	
	BigInteger three("1234567890");
	BigInteger four("123456789");
	BigInteger result2 = three * four;
	std::cout << "three * four = " << result2 << std::endl;
	three *= four;
	std::cout << "three *= four = " << three << std::endl;
	
	BigInteger f = factorial(123);
	std::cout << "factorial of 123 is: " << f << std::endl;
	std::cout << "factorial (as double) = " << static_cast<double>(f) << std::endl;
	BigInteger factorialFromWeb("12146304367025329675766243241881295855454217088483382315328918161829235892362167668831156960612640202170735835221294047782591091570411651472186029519906261646730733907419814952960000000000000000000000000000");

	if (f == factorialFromWeb)
	{
		std::cout << "My factorial result matches!!" << std::endl;
	}
	else
	{
		std::cout << "Still work left to do..." << std::endl;
	}

	return 0;
}
The problem was in my overloaded + and my setdigit functions apparently. I made the following changes to my + operators:
1
2
3
4
5
6
7
8
9
10
BigInteger result;
	unsigned int length = (this->m_digitCount > rhs.m_digitCount) ? this->m_digitCount : rhs.m_digitCount;
	result.m_digitCount = length;
	while (result.m_digitCount > result.m_sizeReserved)
	{
		result.m_sizeReserved *= 2;
		delete[] result.m_number;
		result.m_number = new uint8_t[result.m_sizeReserved];
	}
// the rest of the function remained the same 

That solved most of it, but I was still getting heap corruption on occasion, so I played around a bit more. I ended up removing all of the m_number[i] = NULL; from my setdigit and overloaded function. I'm not having any more heap corruption so I suppose it was causing excessive calls to delete.
Last edited on
Topic archived. No new replies allowed.