Thread: Printf: somehow it changed a structure field.

  1. #1
    Registered User Artemiy's Avatar
    Join Date
    Apr 2009
    Posts
    2

    Printf: somehow it changed a structure field.

    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.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're not allocating enough memory:
    Code:
    gamestate *gs;
    gs=(gamestate *)malloc(sizeof(gs));
    gs is a pointer, so applying sizeof to it yields the size of a pointer, which is likely 4. You instead want something like:
    Code:
    gs = malloc(sizeof *gs);

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And since using memory that is outside your actual allocation is very dependant on what else is happening in the system, it's very likely that changing the code around will affect the actual behaviour of the code - you get different memory usage, which affects what memory gets overwritten and how/where.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User Artemiy's Avatar
    Join Date
    Apr 2009
    Posts
    2
    Quote Originally Posted by cas View Post
    You're not allocating enough memory.
    gs is a pointer, so applying sizeof to it yields the size of a pointer.
    Thank you very much, that solved it! I didn't notice that I wrote the pointer name instead of the type name at all...

    Again, thanks a lot,
    ~Artemiy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  2. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  3. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  4. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM