Whats better programming practice?

Started by
2 comments, last by nivlekio 16 years, 1 month ago
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?
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.
-Mike
There was a recent discussion.
ok kool thanks sorry for posting in the wrong place

This topic is closed to new replies.

Advertisement