I am writing a program that allows for the creation of a theoretically infinitely large integer. I have created the class MyInt and implemented the storage using DMA.
I have attached the .cpp and .h files. These are not the complete files, there are several other overloads but for simplocity sake I narrowed it down to only those functions involved in the problem.
I have placed many cout statements in here trying to trace the problem. And the problem seems to be in the Grow() function that increases the size of the array. Any help would be greatly appreciated.
Yes in Grow you use delete instead of the proper delete[]
Why struggle with all this memory management yourself? You could use a std::vector<unsigned int> instead.
EDIT: std::vector<unsigned char> would be better to avoid current waste of memory (or change to use binary arithemetic)
Last edited by S_M_A; November 7th, 2010 at 04:39 PM.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
It's an assignment. They don't want you using things that might make your life too easy. I still can't use the string class much less vectors. I have tried putting the [] in there and it doesn't work either. I have tried everything I can think of. I have never been so stumped. There error I am getting starts with this, and then the "memory map" which I am not experienced enough to understand.
It crashes when it tries to use delete. From the research I have done this error is often times caused by trying to delete an object/variable that was not created using new. Not sure I can see how that is the case here. It seems like amybe I am doing something wrong in line 178 where this is:
unsigned int *newArray = new unsigned int[maxSize];
It's an assignment. They don't want you using things that might make your life too easy.
Using container and string classes make things easier in terms of having working components that you can use to build the larger application. However, you still have to write the program using these components, so that part is still not "easy", but at least your thinking is now geared towards solving the actual problem that needs to be solved.
Having said that, C++ beginner courses that still teach C++ as if it's 1990 and not 2010 should be abolished (if I had my way). It's the year 2010, and string classes, vectors, etc. have been standardized in C++ since 1998. I wish these courses were honest, and say you are learning 'C' and not C++.
It's an assignment. They don't want you using things that might make your life too easy. I still can't use the string class much less vectors.
When you think you've got the hang of dynamic memory allocation, then maybe you could write you own simple string and vector classes and use them in future assignments that ban the STL. They can't complain about that!
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
I have actually considered that. I wasn't sure which would be more work though, writting the vector and string classes or just using the libraries permitted. Next semester I'm taking data structures and algorithms. Apparently in that class they introduce the STL so I'm guessing at that point we will be able to use more of the libraries.
In the prereq course intro to c++ we weren't even alowwed to use iomanip or cctype. We had to use ios:: to format output. I understand why they did it this way it's kind of like in math they didn't want you to use a calculator until you got to a certain level. It's just frustrating to know there is a tool that could make what I am trying to do allot easier.
I was actually dreaming about this program last night. I think I got something, but I need to take a look at it first.
I understand why they did it this way it's kind of like in math they didn't want you to use a calculator until you got to a certain level.
That would be a defensible argument (though I still won't agree with it) iff you were taught to write completely safe programs. But as Paul has pointed out, the code provided by your professor is not safe. Hence, you are simply being taught bad programming practices, rather than the basics.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
When you think you've got the hang of dynamic memory allocation, then maybe you could write you own simple string and vector classes and use them in future assignments that ban the STL. They can't complain about that!
I got it working! Haven't added all the operator overloads yet. It would seem that the problem was in the operator+ function. Still not sure what i did wrong exactly. I know though that I was somehow writing way out of bounds when creating a new array in the Grow() function. That's why I thought the problem was in the Grow() function because that's where it crashed. It was trying to dynamically create an array of like 3 billion ints or maybe it was more can't remember but it was a really long number.
I appreciate all of the help. This is the first forum that I have ever been a member of. I was surprised to see how helpful people were.
I would guess that you write out of bounds in either MyInt(int n) or the + operator. Have you runned the code in a debugger?
EDIT: Have you tried using numbers <= 99999? For numbers > 99999 I guess this was supposed to kick in but it's not
Code:
int i = 0;
while(i != 0) // Guess you meant to have n here?
{
intArray[i] = n % 10;
n = n/10;
currentSize++;
if(currentSize == maxSize)
Grow();
i++;
}
Last edited by S_M_A; November 7th, 2010 at 05:11 PM.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Thanks Paul, actually most of that code block came from an example program from my proffessor. I will take a look at it again and see if I can find something. I had to take a break starting to see cross eyed and want to break stuff, just kidding. Thanks again, I will post what I find when I look at it.
Thanks Paul, actually most of that code block came from an example program from my proffessor.
If it came from your professor, then it's quite revealing that he/she didn't see this issue, as anyone who is responsible for teaching a complex language such as C++ should/would have corrected it.
More than likely, C++ is not this teacher's main programming language, which can be an issue when it comes to teaching C++ (as opposed to teaching other computer languages).
Actually c++ is his main course. I took him for intro to c++ and am currently taking oop with him. He has a Ph.d in something I'm guessing computer science.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.