Can anyone tell me why I'm getting a segmentation fault?
number * sum;
Why is sum declared as a pointer? You overuse pointers. Also I can see a possible memory leak (in C++, every new operation must have a corresponding delete operation.)
Ok I'm trying to add a destructor in order to avoid the memory leakage. I get the error message :
lab4.cpp:46: error: `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' used where a `int' was expected
lab4.cpp:47: error: `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' used where a `int' was expected
after using the destructor:
1 2 3 4 5 6 7 8
struct number {
string * data;
number(string num = NULL);
number * flip();
~number() {
delete data;
}
};
number * number :: flip() {
number * flipped = new number(data);
for (int i=0; i<data.length(); i++) {
flipped->data[i] = data[(data.length()-1)-i];
}
return flipped;
}
And how about deleting flipped, where will that happen?
Look, I won't sugarcoat this. Your code is currently a pointer mess.
The first thing that you should do now is to correct your code, and use "plain" variables instead of pointers.
lab4.cpp:46: error: `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' used where a `int' was expected
lab4.cpp:47: error: `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' used where a `int' was expected
My assignment is to create a struct capable of holding an object that is a number of any size with functions capable of adding, multiplying, subtracting and factorialing that object. I also have to have a constructor, a destructor and a print method. My current print method is:
1 2 3 4 5 6
void number :: print() {
for (int i=0; i<data->length(); i++) {
cout << data[i];
}
cout <<"\n";
}
to see what is wrong it gives me a segmentation fault. I do believe you are correct in that I probably have memory leakage going on. My destructor is not helping in this regard.
E: Actually it seems like the problem stems from:
1 2 3 4 5 6 7 8 9 10 11 12 13
struct number {
string * data;
number(string num);
number * flip();
void print();
~number() {
delete data;
}
};
number :: number(string num) {
data = num;
}
I think I am running into problems for assigning num to a pointer to a string rather than a string. I don't know how to assign it to the string, though as it seems everything I try will result in an error message.
@Pranay: not if he was tasked with coding an arbitrary precision arithmetic library.
Honestly Gulopey, I'm too dumb to help you.
I know one thing though, if I were you I'd say "screw the requirements" and go the path of least resistance, by using an std::vector<unsigned char>, no destructor, and only accepting hexadecimal formatted input (that is, strings of numbers in base 16). That would simplify the tasks of addition and subtraction a bit, but this is still hard stuff.
Many languages have built-in libraries doing what you try to code. C++ has 3rd party libraries, Boost and GMP.
I'd go with the non-pointer based methods being recommended, but I think I see a reason for the crashes with your pointer based method.
The member data is a pointer to a string, so data[i] would refer to the ith string in an array of strings pointed to by data, not to the ith character in the single string pointed to by data. There are many places in your code which would crash the program.
Try (*data)[i] instead?
EDIT: Is dynamic allocation an actual requirement? If so then maybe it's intended that you work with a dynamically allocated array of characters or integers, not a std::string.
Thanks a lot for the help guys. I went and spoke to my professor and he cleared up why a lot of those unnecessary things were part of the program requirements. I'm going to completely redo it using an array of ints that take the place of the strings I was using before. To any newbie that reads this: I highly recommend that you don't try to figure out how to make things work when your professor gives you strange requirements like this. Go talk to him and see what he had in mind when he assigned it.