I am supposed to be writing a class called MyInt that will be able to hold integers larger than 4,294,967,295 (max size of an int), not dealing with negative values. You are supposed to be able to do things such as MyInt x(121401); and I have done operator overloading for cout so far. I have done this with dynamically allocated arrays, and everything seems to work fine up until I go past 6 digits, to which I get a very odd output. I compiled using g++ main.cpp myint.h myint.cpp
Code:
1214016 has 7 digits.
Grow should be done 1 times.
X is 1000000
1 went into bigint spot 0
X is 100000
2 went into bigint spot 1
X is 10000
1 went into bigint spot 2
X is 1000
4 went into bigint spot 3
X is 100
0 went into bigint spot 4
X is 10
1 went into bigint spot 5
X is 1
6 went into bigint spot 6
1214016
1 should be copied now.
2 should be copied now.
1 should be copied now.
4 should be copied now.
0 should be copied now.
1 should be copied now.
6 should be copied now.
1214016
*** glibc detected *** a.out: free(): invalid next size (fast): 0x000000000c73a010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x32e7e722ef]
/lib64/libc.so.6(cfree+0x4b)[0x32e7e7273b]
a.out[0x400e5c]
a.out(__gxx_personality_v0+0x1ce)[0x400a4e]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x32e7e1d994]
a.out(__gxx_personality_v0+0x49)[0x4008c9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:1a 5346614 /home/majors/muller/COP3330/Assignment_5/temp/a.out
00601000-00602000 rw-p 00001000 00:1a 5346614 /home/majors/muller/COP3330/Assignment_5/temp/a.out
0c719000-0c77c000 rw-p 0c719000 00:00 0 [heap]
32e6e00000-32e6e1c000 r-xp 00000000 08:02 7253499 /lib64/ld-2.5.so
32e701b000-32e701c000 r--p 0001b000 08:02 7253499 /lib64/ld-2.5.so
32e701c000-32e701d000 rw-p 0001c000 08:02 7253499 /lib64/ld-2.5.so
32e7e00000-32e7f4d000 r-xp 00000000 08:02 7253519 /lib64/libc-2.5.so
32e7f4d000-32e814d000 ---p 0014d000 08:02 7253519 /lib64/libc-2.5.so
32e814d000-32e8151000 r--p 0014d000 08:02 7253519 /lib64/libc-2.5.so
32e8151000-32e8152000 rw-p 00151000 08:02 7253519 /lib64/libc-2.5.so
32e8152000-32e8157000 rw-p 32e8152000 00:00 0
32e8200000-32e8282000 r-xp 00000000 08:02 7253520 /lib64/libm-2.5.so
32e8282000-32e8481000 ---p 00082000 08:02 7253520 /lib64/libm-2.5.so
32e8481000-32e8482000 r--p 00081000 08:02 7253520 /lib64/libm-2.5.so
32e8482000-32e8483000 rw-p 00082000 08:02 7253520 /lib64/libm-2.5.so
32eda00000-32eda0d000 r-xp 00000000 08:02 7253523 /lib64/libgcc_s-4.1.2-20080825.so.1
32eda0d000-32edc0d000 ---p 0000d000 08:02 7253523 /lib64/libgcc_s-4.1.2-20080825.so.1
32edc0d000-32edc0e000 rw-p 0000d000 08:02 7253523 /lib64/libgcc_s-4.1.2-20080825.so.1
3c63200000-3c632eb000 r-xp 00000000 08:05 2443458 /usr/lib64/libstdc++.so.6.0.10
3c632eb000-3c634eb000 ---p 000eb000 08:05 2443458 /usr/lib64/libstdc++.so.6.0.10
3c634eb000-3c634f2000 r--p 000eb000 08:05 2443458 /usr/lib64/libstdc++.so.6.0.10
3c634f2000-3c634f4000 rw-p 000f2000 08:05 2443458 /usr/lib64/libstdc++.so.6.0.10
3c634f4000-3c63507000 rw-p 3c634f4000 00:00 0
2b7225d02000-2b7225d04000 rw-p 2b7225d02000 00:00 0
2b7225d2e000-2b7225d31000 rw-p 2b7225d2e000 00:00 0
7fff3c4b1000-7fff3c4c6000 rw-p 7ffffffea000 00:00 0 [stack]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Abort
I understand this has to do with memory allocation but I am not to sure at to where to look for this problem. If I need to provide what type of system I am running it on could someone tell me how to show it? I am doing it through my Mac OS X machine running on my college's computer science account, I am not sure of my version of g++ I am using.
MAIN.CPP
Code:
#include <iostream>
#include "myint.h"
using namespace std;
int main()
{
MyInt x(1214016);
cout << x << endl;
MyInt y = x;
cout << y << endl;
return 0;
}
MYINT.H
Code:
#include <iostream>
using namespace std;
class MyInt
{
friend MyInt operator+ (const MyInt& x, const MyInt& y);
friend bool operator< (const MyInt& x, const MyInt& y);
friend ostream& operator<<(ostream& s, const MyInt& m);
public:
MyInt(int n = 0); // first constructor
MyInt(char number);
~MyInt();
MyInt(const MyInt & h); // copy constructor
int GetCurrent() const;
int GetInt(int i) const;
private:
int maxsize,
currentsize;
int* bigint;
void Grow();
char number;
};
MYINT.CPP
Code:
#include <iostream>
#include "myint.h"
using namespace std;
ostream& operator<< (ostream& s, const MyInt& m)
{
for (int i = 0; i < m.GetCurrent(); i++)
{
s << m.GetInt(i);
}
return s;
}
int C2I(char c)
// converts character into integer (returns -1 for error)
{
if (c < '0' || c > '9') return -1; // error
return (c - '0'); // success
}
char I2C(int x)
// converts single digit integer into character (returns '\0' for error)
{
if (x < 0 || x > 9) return '\0'; // error
return (static_cast<char>(x) + '0'); // success
}
MyInt::MyInt(int n)
{
if (n <= 0)
{
maxsize = 5;
currentsize = 0;
bigint = new int[maxsize];
bigint[currentsize] = 0;
currentsize++;
}
else
{
currentsize = 0;
maxsize = 5;
bigint = new int[maxsize];
int a = n;
while (a > 0)
{
a /= 10;
++currentsize;
}
cout << n << " has " << currentsize << " digits." << endl;
double g = currentsize / 5;
cout << "Grow should be done " << g << " times." << endl;
if (g > 1)
{
do
{
Grow();
--g;
cout << "Grow was done." << endl;
}while (g >= 1);
}
for (int i = 0; i < currentsize; i++)
{
int x = 1;
int b = 0;
for (int j = 1; j < (currentsize - i); j++)
{
x *= 10;
}
cout << "X is " << x << endl;
if (x <= n)
{
do
{
n -= x;
b++;
} while (n >= x);
}
else;
bigint[i] = b;
cout << b << " went into bigint spot " << i << endl;
}
}
}
MyInt::MyInt(char number)
{
C2I(number);
if (number <= 0)
{
maxsize = 5;
currentsize = 0;
bigint = new int[maxsize];
}
else
{
currentsize = 0;
int a = number;
while (a > 0)
{
a /= 10;
currentsize++;
}
double g = currentsize / 5;
if (g > 1)
{
do
{
Grow();
g--;
}while (g > 1);
}
}
}
MyInt::~MyInt()
{
delete [] bigint;
}
MyInt::MyInt(const MyInt & d)
{
maxsize = d.maxsize;
currentsize = d.currentsize;
bigint = new int[maxsize];
for (int i = 0; i < currentsize; i++)
{
bigint[i] = d.GetInt(i);
cout << d.GetInt(i) << " should be copied now." << endl;
}
}
int MyInt::GetCurrent() const
{
return currentsize;
}
int MyInt::GetInt(int i) const
{
return this->bigint[i];
}
void MyInt::Grow()
{
maxsize = currentsize + 5;
int* newint = new int[maxsize];
delete [] bigint;
bigint = newint;
}
If you notice, I haven't quite finished doing the input for a cstring such as MyInt x("12345") and have not even looked at testing it yet until I fix this problem.