padding struct

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.
 
M

Mark Bluemel

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.
I think you should read section 2 of the C FAQ.

Questions 2.11 and 2.12 (including the supplementary information) would
probably help you.

You'll find the FAQ at http://c-faq.com/
 
B

Barry Schwarz

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 whichis 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.

There is no portable way to eliminate the padding. Your compiler may provide a system specific method of doing so. But does the recipient's?

Furthermore, you have no idea if the padding generated by your compiler will match the padding generated by the recipient's compiler.

You also don't know if the sizeof(int) on your system is the same as the recipient's. The same is true for endianness.

That being said, in my experience most compilers will not generate padding if each of the members is a char or an array of char. Once you construct the values for each member is a set of working variables, you can use a combination of shift operators and bitwise-and operators to construct the members in a documented format. The recipient can use a similar approach to reconstructing the values.
 
J

Jorgen Grahn

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;
};

My standard answer: don't use structs for I/O. Instead define the I/O
data format in terms of octets (e.g. if this is something UDP-based)
and write functions for filling in/reading from an uint8_t buffer
according to that format.

You can still have a struct, but it won't neccessarily match,
bit-by-bit, the data in your I/O.

Doing the struct-mapping thing IME leads to all kinds of problems:
compiler dependencies, endianness bugs, buffer overflows, weakly
defined data formats, lots of casting, valgrind warnings ...

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top