structure padding

F

fdmfdmfdm

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

Walter Roberson

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

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top