I'm trying to implement a long math data type in MSVC++. It(is supposed to)store a number as an array of char intead of bytes. In this fashion I thought I could write new math functions that model long-hand math, so the type could precisely compute extremely large numbers(limited only by the amount of addressable memory). It contains a
char* and an
int (for strlen). Im in the process of overloading the operators so i can implement it like a standard data type. I have <,>,= up and running, and im trying to implement operator+. I have to allocate a new string to hold the output data, but when I declare a new object inside the function, it throws an exception. Also, when I declare a
new char[num]num is longer than it's supposed to be. Also, the destructor throws an exeption if i try to delete the string I allocated in the constructor. Im a first year student, so im sure im doing something wrong, but I dont know what. Im uploading the code in a .txt(I truncated a lot of it and its down around 278, still way to big to post in this window)
The operator+ is kinda sloppy (Ive been testing the %#* out of it, sorry)
When you delete your array, did you do it like this:
Is it possible to cut the txt file in two and upload it in two posts?
Hmm.. I tried to u/l it as a text file.. guess it didn't work. My first concern is why the new char[length] is giving me an array of the wrong length. I'm thinking its something to do with the copy/assignment constructor & the fact that they're not freeing the memory right.
LongMath& operator=(const LongMath& a)
{
if(this->string)
{
//std::cout << "Data present in this->string before assignment: " << this->string;
//delete [] string;
}
string = new char[a.length];
strcpy(this->string, a.string);
this->length = a.length;
return *this;
}
public:
LongMath operator+(const LongMath& a)
{
int len1 = this->length;
//std::cout << "\nthis->length = " << strlen(this->string);
int len2 = a.length;
CONTINUED NEXT POST
I've rewritten and tested this part DOZENS of times, I left my little testers in there... Eventually I'm going to make a class hierarchy to orgainize all this better(instead of all in the same class) but it was way easier to test it when its all in the same class.
The operator+ has the pieces it needs to do what its intended to do, but I cant get the new char[string] to be the right length, so its left unfinished. It compiles, but I get runtime errors when I change anything from this point. Maybe you can suggest something I havent tried yet.
Gets a little sloppy here:
if(*this > a )
{
//---- PRETTY MUCH THIS WHOLE BLOCK IS TESTS
// /*---All it REALLY NEEDS to do is allocate 1 or 2 strings the
proper length
//^---REMOVE THAT C++ STYLE COMMENT TO COMMENT THE WHOLE BLOCK
larger = len1+1;
//std::cout << "larger:" << larger;