Hello!
It may be possible that I made a stupid mistake somewhere in the code, but I just can't find it. I can bypass this problem but I'm afraid that it will arise in a different part of the program.
I'm using cygwin & gcc 3.4.4.
With the help of gdb I've managed to localize the problem to the following segment:
Code:
// Disregard some unimportant silliness - it is only for debugging purposes.
// maxcrd=36
// N=6
struct player
{
int afd;
char name[maxname];
int ncards;
int hand[maxcrd];
int beat;
int con;
int out;
};
struct gamestate
{
int started;
int nplayers;
struct player player[N+1];
int current;
int previous;
int trump;
int top;
int deck[maxcrd];
int field[2][maxcrd];
};
void printstate(gamestate *gs)
{
printf("Gamestate;\n");
printf("Cur: %d Prv: %d\n", gs->current, gs->previous);
printf("Trump: %d", gs->trump);
printf("\n");
printf("Top: %d\n", gs->top);
printf("\n");
int i;
for(i=0; i<6; i++)
{
if(gs->field[1][i]==0)
printf(" | ");
else
printf("%s | ", cid(gs->field[1][i]));
}
printf("\n");
for(i=0; i<6; i++)
{
if(gs->field[0][i]==0)
printf(" | ");
else
printf("%s | ", cid(gs->field[0][i]));
}
printf("\n");
}
// ...
int main(void)
{
gamestate *gs;
gs=(gamestate *)malloc(sizeof(gs));
// ...
}
The situation's the following: there is a 'gamestate' struct. In a function (namely, 'printstate') using 'printf' I print out some data. At some point for a reason I can't yet understand the function changes the value of (gamestate *) 'gs->player[0].ncards' either to some close value or just to a random byte.
For example, if 'gs->player[0].ncards' is 9, at some line in the function (after several 'for' iterations) it turns into 545005600.
If I change the function as follows:
Code:
void printstate(gamestate *gs)
{
printf("Gamestate;\n");
printf("Cur: %d Prv: %d\n", gs->current, gs->previous);
printf("Trump: %d ", gs->trump);
printf("\n");
printf("Top: %d\n", gs->top);
printf("\n");
int i;
for(i=0; i<6; i++)
{
if(gs->field[1][i]==0)
write(1, " | ", strlen(" | "));//printf(" | ");
else
printf("%s | ", cid(gs->field[1][i]));
}
printf("\n");
for(i=0; i<6; i++)
{
if(gs->field[0][i]==0)
write(1, " | ", strlen(" | "));//printf(" | ");
else
printf("%s | ", cid(gs->field[0][i]));
}
printf("\n");
}
...everything is fine, no sudden variable changes.
Please help me understand the issue, thank you in advance,
~Artemiy.