Image

Imageuncreativeness wrote in Imagecpp

I have a structure of moves that I made to hold the rows and colums of peices on a two dimentional board. I dynamically allocated the structure to be 10 moves long. If there is over 10 moves then I allocate a new set of moves that is 20 long and copy it over, in other words grow what I have by 10 more moves. The problem comes when I want to check the size of what I have. How can I index through my structure to see how long it is.

struct Moves
{
int row;
int col;
}

Move *moves = new Move[10];
int size = 0;

If I do:

while(moves)
{
size++;
moves++;
}

I get an infinate loop

If I do:

int index = 0;

while(moves[index++].row != NULL)
size++;

for some reason it always goes to 2 more than the actual size, (i get 12, or 22, or 32) and I forgot that I can actually have 0 as a row and column. So that doesn't work because if 0 is a real column move then it exits the loop before i get to the end.


Any suggestions as to how I could move through this to get the current size without having to keep track of it manually throughout the program. I know it starts off as 10 large and I could keep track of it if i really needed to. But I only need it in this one method, so it would be a lot easier if I could just find the size when i need it rather than carrying it all over the place. Thanks