Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > padding struct

Reply
Thread Tools

padding struct

 
 
luvraghu@gmail.com
Guest
Posts: n/a
 
      07-27-2012
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.
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      07-27-2012
On 27/07/2012 10:52, wrote:
> 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/
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      07-27-2012
On Friday, July 27, 2012 4:52:27 AM UTC-5, luvr...@gmail.com wrote:
> 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.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-28-2012
On Fri, 2012-07-27, wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct memory padding simonp@nospam.com C++ 11 01-22-2007 10:56 PM
Padding bits and struct assignment Hallvard B Furuseth C Programming 5 12-27-2006 10:19 PM
struct padding edware C Programming 9 04-27-2006 12:40 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments