Jump to content
  • Advertisement
Sign in to follow this  
nivlekio

Unity Whats better programming practice?

This topic is 3918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Ive been reading some networking tutorials online some most don't seem to be consistent on sending bit of winsock can't explain by word alone so ill give an example to make thnigs clear...

struct UnitInfo
{
stPacketHeader stHeader;
int iUnitID;
int iUnitType;
int iUnitHealth;
int iUnitFireRate;

int UnitXPos;
int UnitYPos;
}

struct UserInfo
{
stPacketHeader stHeader;
int UserID;
char szUserName[50];
char szUserIP[16];

}


struct UnitInfo g_stiSPacketUnitInfo;
struct UserInfo g_stiSPacketUserInfo;

char szSendPacketBuffer[2000];
int iSizeOfSentData = 0;


some where in code after i fill in the structs with info then memcpy the struct in to the szSendPacketBuffer...... Is it better to send it with the size of the struct prototype,
iSizeOfSentData = ClientSocObject.iSend(szSendBuffer,sizeof(UserInfo),0);

or send it with the sizeof the struct instance
iSizeOfSentData = ClientSocObject.iSend(szSendBuffer,sizeof(g_stiSPacketUserInitInfo),0);

or the size of the buffer
iSizeOfSentData = ClientSocObject.iSend(szSendBuffer,sizeof(szSendBuffer),0);

no suprise i get diffrent results for iSizeOfSentData but i was wondering which is better programming practice and safer? I think logically its better to send with size of the struct instance but i am a bit worried that be might bad and cause some data/memory error or problems?

Share this post


Link to post
Share on other sites
Advertisement
This isn't really a networking question...

I prefer sizeof(variable) because if you use sizeof(Type) you can accidentally specify the wrong Type, or the Type could change and you forget to change the sizeof call. sizeof(szSendBuffer) is just plain wrong unless you really want to send uninitialized stack garbage for no good reason.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!