Sorry about the formatting on that code block, I am not very experienced with that. Obviously the overload is not the perator= overload. I feel pretty certain the problem is in the Grow() function. But it may be caused by the creation of the intArray else where.
Sorry about the formatting on that code block, I am not very experienced with that. Obviously the overload is not the perator= overload. I feel pretty certain the problem is in the Grow() function. But it may be caused by the creation of the intArray else where.
To check if your object is copied correctly, you can write a simple test program. Something like this
Code:
// include MyClass
int main()
{
MyClass a; // default c'tor
MyClass b(...); // parameterized c'tor
MyClass c = b; // copy c'tor
a = c; // assignment operator
// possibly do some tests
}
Check not only that the objects are proper copies, but also that all memory is freed an no double deletes are performed.
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
I have a driver program, and I can successfully use the assignment operator. It is at the addition that I run into problems. But after putting several cout statements in my program I have traced the problem to the Grow() function. It seems to be a problem with the line where I have
delete [] intArray
in the Grow() function on line 193 of the files I attached.
If you have operator+= tested and working, and if you have the cctor and operator= tested and working, then there is absolutely no reason why operator+ would not work. So focus on the components first-----is the problem in the copying, or in the += operation?
I haven't overloaded the += operator. And I have even been able to get it to add two numbers. The problem is when the array reaches the upper bound and I call the Grow() function to increase the size of the array it is giving me a bad allocation error. It's not just giving me a segmentation fault it's scrolling a whole bunch of stuff on the screen, it says memory map, and shows stack and heap and memory addresses. I don't know how to use that stuff to help me, it might as well be chinese. It may be that there is a problem in the operator+ function, but it crashes in the Grow() function. I read something about debugging though that said that instead of using printf or cout statements that you should use stderr since it isn't buffered. But I can't remember how to do that. Is it
"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
That is what you should have done first. Maybe you should forget operator + for now, and concentrate on operator +=. Once you get operator += working, operator + will literally take 15 seconds to write, as operator + will just use += to do the work.
And I have even been able to get it to add two numbers. The problem is when the array reaches the upper bound and I call the Grow() function to increase the size of the array it is giving me a bad allocation error.
This goes to my point I made before. You have an assignment that is supposed to add big numbers -- when are you going to actually add the numbers? You haven't even gotten that started. If you were allowed to use containers, all of these allocation issues become moot, and you can then concentrate on the real goal of the assignment, and that is a class that adds big numbers.
This IMO is how a beginner C++ program should be taught, and that is for the student to concentrate on the goal and use standard containers, algorithms, classes, etc. to reach that goal. Instead of putting together the jigsaw puzzle, you've been told to get cardboard, glue, a pretty picture, a knife, and create the jigsaw puzzle yourself.
I can tell you this -- Java students would not have to go through this as you have -- they are required to use the components and not code their own containers and strings.
It's not just giving me a segmentation fault it's scrolling a whole bunch of stuff on the screen, it says memory map, and shows stack and heap and memory addresses. I don't know how to use that stuff to help me, it might as well be chinese. It may be that there is a problem in the operator+ function, but it crashes in the Grow() function.
Let's look at Grow:
Code:
void MyInt::Grow()
{
maxSize = currentSize + 5;
unsigned int *newArray = new unsigned int[maxSize];
for(int i = 0; i < currentSize; i++)
newArray[i] = intArray[i];
// delete intArray; // wrong form of delete!
delete [] intArray;
intArray = newArray;
// newArray = NULL; // no need for this line.
// Now where do you reset currentSize to maxSize?
// Isn't that missing?
}
1) You are using the wrong form of delete,
2) You do not set currentSize to the maxSize after all of this work. So what is probably happening is that Grow() is not growing anything, since you use the stale value of currentSize every time. If you're setting currentSize somewhere else, don't. Do it right there in Grow(), otherwise you're jumping around all over the place setting currentSize, when the logical place to set it once and for all is before Grow() returns.
In addition, you have a superfluous line that sets newArray to NULL.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 8th, 2010 at 12:20 PM.
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 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.
Is the header still the same, or did you just change the cpp file? I will take a look at your new code and comment if I see anything else glaringly wrong. I have a habit of destroying all hopes of new programmers by breaking their code with a very simple main() program, so just be aware.
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.
As long as you ask questions in detail, with a description of what problems you're having, then you will get all sorts of help here. The only thing you won't get is anyone to do the work for you (and I believe you knew that already).
Here are some things with the new code you should address:
Code:
MyInt::MyInt(const char* n)
Since "n" doesn't have it's contents it's pointing to change, this should be const char*.
I still don't understand why you do not update the size member in the Grow() function. Maybe that's where you're getting tied up in knots. I would expect Grow() to actually dynamically allocate memory, and at the same time, update "size" so that it now has the current size that is allocated.
What if you called Grow() twice in succession? You see that your "size" member variable never changes, and this to me is not correct in a logical sense. That's why you should change "size" in the function where it counts, and that is in Grow(). Right now, you're changing size all over the place except in the place where it makes sense.
Code:
size = 1;
Grow();
Grow();
Grow();
You will see nothing grows, even though I called it three times!
That's a good idea on the constructor with the char* n parameter. The grow() function is basically creating a dynamically allocated array that is 5 slots (indices not sure about the term here) larger than size. Size is the current length of the array and Max is the upper limit. Basically just so we're not dynamically creating and destroying memory and copy data every time the size is increased.
I did make some modifications to the header file, I will post it tomorrow when I get up.
I welcome any attempts to "break" my program. And I'm sure it won't be hard. Like math I enjoy programming and am always open to advice to improve my abilities/skills. The program isn't quite complete yet but what is done works (gives the proper result given the inputs I've tested so far).
Well, I fixed the addition part, and then added the multiplication. However, once I implemented the multiplication operator overload I started getting the same error. I have been getting two memory errors. One is a segmentation fault and the other is: " *** glibc detected *** ./myint: malloc(): memory corruption (fast) : 0x08e7c110 *** "
The assignment was due at that point so I just turned it in as it was. It compiles, it just crashes. It would seem that the error is taking place when multiplying an int times a myint object. The multiplication works on two myint objects. This leads me to believe that the problem is an object conversion issue. But I have the default constructor set up to accept ints. So not sure. Since the program was due this is no longer for credit but I am in school to learn this stuff so I would like to understand what I did wrong if I can. I am going to meet with my professor next week if I can (he's always pretty busy), his teaching assistants weren't allot of help. I am attaching the .cpp, .h, driver and the make file in a tar file.
* 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.