Thread: Running Program Problem

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    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.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    You left me MacGyver. :'(

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Got your PM.... and wow, that is a lot of code for what I thought was a simple assignment.

    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:

    Code:
    if (temp[i] == array[i])
    Totally wrong. Should be:

    Code:
    if (temp[i] == array.temp[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.

    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.

    Have fun.

    [/Edit 1]
    Last edited by MacGyver; 03-27-2007 at 11:05 PM.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    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?
    Last edited by warfang; 03-27-2007 at 11:35 PM.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    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:
    Code:
    IntArray(const IntArray &);
    And I'm using Dev-C++ 4.9.9.2
    Last edited by warfang; 03-28-2007 at 01:08 AM.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

    Code:
    int IntArray::operator==(const IntArray &array)
    {
    	if (sizeOfArray == array.sizeOfArray)
    	{
    		int i;
    		for (i = 0; i < sizeOfArray; i++)
    		{
    			if(temp[i] != array.temp[i])
    			{
    				return 0;
    			}
    		}
    		return 1;
    	}
    	return 0;
    }
    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.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Salem View Post
    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.

    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.

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by Salem View Post
    > 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 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem running program
    By JOCAAN in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2007, 04:09 PM
  2. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  3. How to tell when internal program is done running
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 03-21-2005, 10:09 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem running program in Win98
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 10-19-2003, 12:08 PM