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.

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
{
int iUnitID;
int iUnitType;
int iUnitHealth;
int iUnitFireRate;

int UnitXPos;
int UnitYPos;
}

struct UserInfo
{
int UserID;
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 on other sites
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 on other sites
There was a recent discussion.

Share on other sites
ok kool thanks sorry for posting in the wrong place

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633695
• Total Posts
3013377
×