If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: new [] / delete [] errors, going crazy!

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.

Re: new [] / delete [] errors, going crazy!

Originally Posted by weaverx9x9

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

Re: new [] / delete [] errors, going crazy!

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

Re: new [] / delete [] errors, going crazy!

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?

Re: new [] / delete [] errors, going crazy!

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

Re: new [] / delete [] errors, going crazy!

Originally Posted by weaverx9x9

I haven't overloaded the += operator.

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.

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.

Re: new [] / delete [] errors, going crazy!

Originally Posted by JohnW@Wessex

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!

Re: new [] / delete [] errors, going crazy!

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.

Re: new [] / delete [] errors, going crazy!

Originally Posted by weaverx9x9

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).

Re: new [] / delete [] errors, going crazy!

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.

Re: new [] / delete [] errors, going crazy!

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).

Re: new [] / delete [] errors, going crazy!

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.