Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Random Crashing
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
crossley7




PostPosted: Thu Jun 23, 2011 3:32 pm   Post subject: Random Crashing

MY program seems to be crashing randomly on me and after a few days of looking at the same code over and over I still don't know why. This is from my chess program and I am trying to call functions for my ai but they seem to crash every time they are called.

c++:

Sfile = ai.get_coord (0, ai.get_attempt())+'A';
Srow = ai.get_coord (1, ai.get_attempt())+1;
file = ai.get_coord (2, ai.get_attempt())+'A';
row = ai.get_coord (3, ai.get_attempt())+1;


That is the code in my main function where they are called and below is where they are declared

c++:

int AI::get_coord (int spot, int attempt)
{
    return move_coords [move_options[attempt]][spot];
}

int AI::get_attempt ()
{
    return att;
}


I have issues with calling another function, but if i figure out what is wrong here, that might help with the other parts. |All the variables called in the 2 ai functions are either private class variables or parameters passed in.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Jun 23, 2011 3:52 pm   Post subject: RE:Random Crashing

What is the error you're getting?
Nick




PostPosted: Thu Jun 23, 2011 5:48 pm   Post subject: RE:Random Crashing

I'm not too sure about C++ but can you add 'A' to an int and still make it an int?
Tony




PostPosted: Thu Jun 23, 2011 6:19 pm   Post subject: RE:Random Crashing

That would be a compile-time error, no?

OP's most likely going outside of array's range and attempts to access an illegal memory address; but I can't be sure of that from the code sample.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Thu Jun 23, 2011 6:22 pm   Post subject: RE:Random Crashing

@Nick- 'A' is a character, not a string. It is stored in RAM as a 1-byte integer (iirc) representing its ASCII value. It is, for all intents and purposes, an integer.
crossley7




PostPosted: Thu Jun 23, 2011 8:45 pm   Post subject: RE:Random Crashing

The program doesn't have an error with compiling, but crashes whenever it runs through that code for some reason. And the + 'A' is stored into a char variable which is actually just an integer value represented by something else as insectoid stated. I can try setting it so that it can never go outside the range (as in debug mode it does loop run the code fine but ends up looping through too far). I wouldn't think that would be the error though as the program would at least run a bit before reaching that point and closing i think

UPDATE: It seems to have fixed that issue, now to debug why my program isn't actually making the moves when it should with a legal move
crossley7




PostPosted: Thu Jun 23, 2011 9:43 pm   Post subject: Re: Random Crashing

Well, I found my problem. Looks like my issue was a vector isn't assigning properly or I'm messing something up. I was checking values and they all assign properly into the temp_coords vector but they don't seem to want to move into the move_coords and hold their values. I believe I have had this issue before but I can't remember how i solved it

c++:

 for (int i = 0; i < 8; i++) // going through chess board
         for (int j = 0; j < 8; j++)
             if (simulation [i][j]/10 == colour-1) // piece is the colour of the ai
             {
                temp_coords[0] = j; // temp coords is vector of integers of size 4
                temp_coords[1] = i; // it holds the 4 coordinates of a move
                for (int k = 0; k < 8; k++)
                    for (int h = 0; h < 8; h++)
                    {
                        temp_coords[2] = h;
                        temp_coords[3] = k;
                           move_coords.push_back(temp_coords);// vector <vector <int> >
                           move_values.push_back(moveV (temp_coords)); // vector <int> stores integers passed by moveV function
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: