Is this valid buffer code?

Started by
2 comments, last by Westeria 19 years, 4 months ago
char bufInternet[300];
unsigned short intLocalPlayer;
unsigned short intMonster;
/* Objects */
struct strObject
{
	unsigned char bytIdentity;
	unsigned short intX;
	unsigned short intY;
	unsigned short intGridX;
	unsigned short intGridY;
        ....... 107 bytes ......
}



	if(n>0)
	{
		/* Each packet is an individual creature. */
		memcpy(&intLocalPlayer, &bufInternet[n-(2+2)], 2);
		memcpy(&intMonster, &bufInternet[n-2], 2);
		if(intLocalPlayer == intMonster) bytRecievedGameState = 1;
		memcpy(&arrObjects, &bufInternet, n-4);
	}



Normally I wouldn't ask such a question, but accessing two computers isn't easy from where I am. "Fleebow.exe - 0 error(s), 0 warning(s)" Edit: It looks right to me, but it felt too ez.
Advertisement
If n is the size of bufInternet, it looks like it would work. I just skimmed it over so there could be something I missed. Print out the buffer and look at it to make sure it's what you want (also remember that any weird characters will be printed in ascii).

Edit: to be sure you're compatible, you could do this instead:
if(n>0)	{		/* Each packet is an individual creature. */		memcpy(&intLocalPlayer, &bufInternet[n-(sizeof(unsigned short)*2)], sizeof(unsigned short));		memcpy(&intMonster, &bufInternet[n-sizeof(unsigned short)], sizeof(unsigned short));		if(intLocalPlayer == intMonster) bytRecievedGameState = 1;		memcpy(&arrObjects, &bufInternet, n-(sizeof(unsigned short)*2));	}
	memcpy(&arrObjects, &bufInternet, n-4);

&bufInternet is a pointer to the address of your character array, not a pointer to your character array...
	memcpy( &arrObjects, bufInternet, n-4 );
or
	memcpy( &arrObjects, &bufInternet[0], n-4 );
would copy out of the buffer properly...(as long as n is less then the size of hte buffer :)
Thanks a million, lucky_monkey.

I would not have figured that out. [smile]

This topic is closed to new replies.

Advertisement