Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > structure padding

Reply
Thread Tools

structure padding

 
 
fdmfdmfdm@gmail.com
Guest
Posts: n/a
 
      02-07-2007
code like:

struct pid_tag{
unsigned int inactive : 1;
unsigned int : 1; /* 1 bit of padding */
unsigned int refcount : 6;
unsigned int : 0; /* pad to next word boundary */
short pid_id;
struct pid_tag *link;
}


My questions are:
1. What is this 1 bit padding? Does it mean that pad 1 bit after
integer inactive? Will this 1 bit pad have the same effect as the 0
pad below?
2. What is "pad to next word boundary"? I guess after this 0 bit
applies, the compiler will align the structure into a word boudary
according to the machine?
3. Are the word boundary alignment stops at bit-field, i.e., short int
pid_id and struct pid_tag *link are not aligned?

Thank you.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-07-2007
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

>code like:


>struct pid_tag{
> unsigned int inactive : 1;
> unsigned int : 1; /* 1 bit of padding */
> unsigned int refcount : 6;
> unsigned int : 0; /* pad to next word boundary */
> short pid_id;
> struct pid_tag *link;
>}


>My questions are:
>1. What is this 1 bit padding? Does it mean that pad 1 bit after
>integer inactive?


The order of the bits within a word is not defined, so the
bitfield named inactive could end up being either the first or
the last bit in the word. All we can say with certainty is that
the one bit of padding will be adjacent to the bit used for
inactive, and that inactive will be at one end of the word or
the other. We cannot say whether the padding bit will be
"before" or "after" the other bit, because no direction is
defined for bitfields.

>Will this 1 bit pad have the same effect as the 0
>pad below?


No.

>2. What is "pad to next word boundary"? I guess after this 0 bit
>applies, the compiler will align the structure into a word boudary
>according to the machine?


A bit width of 0 requires that the compiler proceed to the next
(implementation-defined) word boundary and not place more bitfields
in the previous word even if they would fit there. There is
an automatic alignment to an appropriate boundary anyhow as soon
as you finish the run of bitfields, so the :0 is not going to have
any effect in that particular context. If there had been another
bitfield after the :0 then there would have been a potential difference
compared to there not being a :0 .

If a short int is routinely 8-bit aligned on an implementation,
then at the location of the :0 the short that comes next would already
be at the right position for alignment and the :0 wouldn't have
any effect. If a short int is not 8-bit aligned on the implementation,
then the :0 would tell it to proceed to the alignment point for
the short int pid_id -- which is exactly the same thing that would
happen in this case if the :0 were not there. :0 only matters in
the middle of lists of bitfields.

>3. Are the word boundary alignment stops at bit-field, i.e., short int
>pid_id and struct pid_tag *link are not aligned?


No, pid_id and link will always be aligned (unless compiler-specific
mechanisms such as "#pragma pack" are present to force non-alignment.)

--
Programming is what happens while you're busy making other plans.
 
Reply With Quote
 
 
 
 
clinuxpro clinuxpro is offline
Junior Member
Join Date: Jul 2011
Posts: 4
 
      07-13-2011
Structure padding is done by the compilers and this depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it.
http://clinuxpro.com/structure-padding
 
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
Size of a structure : Structure Padding Kislay C Programming 15 07-13-2011 04:24 AM
structure padding Stephen Mayes C Programming 5 05-31-2005 03:49 AM
padding between variables in a structure junky_fellow@yahoo.co.in C Programming 6 05-18-2005 08:06 AM
structure padding phoenix C Programming 1 03-11-2005 04:21 AM
Structure padding. Amarendra C Programming 13 06-22-2004 07:59 AM



Advertisments