L
luvraghu
Hello Experts,
suppose i have a packet of 9 bytes having fields: length(4bytes), type(1 byte), version(4bytes). Now i would write the packet structure as
struct mypack {
unsigned int;
unsigned char;
unsigned int;
};
when i allocate the structure as:
ptr = (struct mypack *)malloc(sizeof(struct mypack));
the amount of memory allocated is 12bytes(3 bytes extra due to structure padding). If this packet is sent out,how would the receiver receive the correct packet contents, I mean he would know the offset at which each field is present and when he tries to get those field value at that offset he might get it wrong.. for example if he tries to get the 3rd field version which is at offset 6 he would get the wrong value due to the structure padding after char member in struct. how can we solve this? I understand pragma directives avoid padding, but what if I dont want to use them?
Hope I'm clear. Please clarify.
Thanks for your time.
suppose i have a packet of 9 bytes having fields: length(4bytes), type(1 byte), version(4bytes). Now i would write the packet structure as
struct mypack {
unsigned int;
unsigned char;
unsigned int;
};
when i allocate the structure as:
ptr = (struct mypack *)malloc(sizeof(struct mypack));
the amount of memory allocated is 12bytes(3 bytes extra due to structure padding). If this packet is sent out,how would the receiver receive the correct packet contents, I mean he would know the offset at which each field is present and when he tries to get those field value at that offset he might get it wrong.. for example if he tries to get the 3rd field version which is at offset 6 he would get the wrong value due to the structure padding after char member in struct. how can we solve this? I understand pragma directives avoid padding, but what if I dont want to use them?
Hope I'm clear. Please clarify.
Thanks for your time.