Running Program Problem
I wrote a program for class and when I execute it gives me a memory unreadable error. The problem is when I debug it to see where the problem is the program works perfectly fine. The kicker is if I go too fast in the debugger the program locks up (freezes). I'd prefer not to post my code but if you're willing to help I could just private message it to you.
The problem I thought I had was some pointer error and possibly me not clearing out the memory well enough before making a new object. IE I cleared part of the object out of the memory but not every value associated with that object. So when I made the next object it started where the last was deleted but couldn't write over all the memory that it needed because those values where still there. I hope that made sense. Anyways any help would be appreciated. I was happy that I got it working but this next problem is kinda out of my league and my classmates either aren't as far as I am in it or they have no idea how to help.
I don't mind helping, but from that description I have no idea what the problem is and therefore no idea if I can help. Secondly, I don't know how we can help if you're extra protective of your code.
I'm not sure that many of us would copy it and turn it in to your professor, although I could be wrong. :)
The reason why I'm being cautious is because I let someone see my code for the last project and they stole the code literally line for line only changing a few things. Also there's a lot more on the line other than just getting kicked out of the class if I get pinned for cheating. I'll message you my code then.
You left me MacGyver. :'(
Got your PM.... and wow, that is a lot of code for what I thought was a simple assignment. :p
Still looking through it.... Will post if I find anything.
[Edit 0] OK, compiled it and got a myriad of warnings about returning values... Looking at one of them, you definitely return a value so I have no idea what the compiler is doing.
Regardless, your program crashes at test 6. That is because in one of your constructors (copy one?), you forget to allocate memory for temp.
It then crashes at test 9. [/Edit 0]
[Edit 1] LOL. What a bad error, and I didn't catch it! You're doing this inside your == and != operator sections:
Totally wrong. Should be:
if (temp[i] == array[i])
Also, I rarely deal with C++ references (Yes, I know, that's bad for me), but I believe you should be passing const references (ie. const IntArray &) to those particular operators.
if (temp[i] == array.temp[i])
With those changes, (and fixing the warnings), the code compiles and runs until it hits test 17. I think I gave you an idea of where you should be at. At this point, the test program is trying to break/crash your class by giving incorrect values. Try to keep things going, even if incorrect data is given to you. For example, if they enter in a low that is greater than high, I would just swap them and print an error message to the user warning them that you're doing that, but continue executing.
Thank you so much :). I think sometimes it's just one of those things that you look at it so much that you're numb to the problems. I really appreciate it. THANKS!
BTW what compiler are you using?
I'm just getting to the end of 9, I think it has something to do with the == and != overloads. I did what you suggested but I'm still getting held up there. I'll let you know if I figure it out.
EDIT: well when I debug I get stuck at 9 and when I just run the program I get stuck at 12. I think the problem is when I overload == it for some reason is initializing another object. If you debug it at 9 and watch it from there you'll see that before it goes into the == function it initializes another object.
Specifically it goes here first:
And I'm using Dev-C++ 18.104.22.168
IntArray(const IntArray &);
Using Borland at the moment.
Cleaned up your == operator a bit... Make sure you're passing it a reference and not just normally, otherwise it'll try to make a copy of your object and that's probably not what you want to do.
The changes I made allow me to get past test 9 all the way to 17 as I stated. Also, make sure that your != operator has the same signature as the above.
int IntArray::operator==(const IntArray &array)
if (sizeOfArray == array.sizeOfArray)
for (i = 0; i < sizeOfArray; i++)
if(temp[i] != array.temp[i])
> I let someone see my code for the last project and they stole the code literally line for line only changing a few things
Talk to your tutor about this.
For instance, give them an early copy of the work you've done to date so they know in advance who's the most likely to have the original.
In fact, your tutor should get early copies (say 2 days before deadline for a week long assignment) off everybody. That would show who's doing real work and who's copying. If student 'A's code totally changes, and it looks like student 'B's all of a sudden, then that's a dead giveaway.
This is an open forum, which works best when everyone can see and correct (and learn from) what everyone else is doing. Now MacGyver may or may not really know his stuff, but you're sure taking a lot on trust by asking for self-selected experience.
I agree with this. :)
Originally Posted by Salem
The best level of help would be achieved if the code was posted publicly. I do not pretend I always know what I'm doing, and there are a lot of people on these forums that appear to have a significant amount of experience well beyond what I have.
Originally Posted by Salem
I will most definetly start sending my teacher some code to insure that doesn't happen. Thanks for the idea, I will feel a lot safer that way. I'd like to just lay all of my code out there but I feel a little awkard posting that much code (took like 600-1000 lines I think). Also I feel it would be kind of rude to just say someone help me, here's all my code without them saying it was ok first. I understand what you're saying though. I've tried a few times to try and explain my problem without posting the code but that usually doesn't work and I'm certain a classmate or two is using this forum because I've seen some posts that gave it away. Before I sent Macgyver the code I looked up his previous posts just to make sure ;).
Edit: Thanks again Macgyver. I really, really appreciate it. If I could I would give you a cookie. I think I should be able to figure it out from there. Also thanks for helping me understand why a new object was being created. It makes sense now.