Reputation: 309
struct A
{
int a;
int b;
};
char data[10];
struct A a;
If i use memcpy()
function to convert a
into char array data
,and transfer it with socket.Should i consider byte order now?
Upvotes: 1
Views: 184
Reputation: 4943
If it's a real project (something that will be deployed in the real world) then yes, you absolutely need to care about byte ordering. You'll also need to care about other serialization issues including data type sizes and structure field alignment.
You can eliminate some headaches by using types explicit sizes, for example use int32_t
rather than int
.
The structure field alignment issue is much more involved. The short answer: don't send()
a raw structure from one machine and recv()
it on another. Doing so assumes the apps running on both systems lay out their structure fields in precisely the same way (with identical padding).
Upvotes: 2
Reputation: 156
You probably should, everyone expect network bytes order when receiving data. Another problem that you have is the padding. The compiler is allowed to put empty space between struct members, cause of required alignment. You have to send just one, network-byteordered, struct member at the time to work around this problems. You could look up serialize functions.
Upvotes: 0