<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>sizeof of struct with  bit Field</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/21853/sizeof-of-struct-with-bit-field</link><description> 
please explain me why the sizeof (C51) returns 6 bytes for this
struct instead of 5: 

 
typedef struct DEKO_COMM_HEADER 
{ UINT16 m_uiMsgOpcode; 
UCHAR8 m_sblMsgPriority :2; 
UINT16 m_sblDataLength :11; 
UINT16 m_sblMsgTimeStamp :11; 
}DEKO_COMM_HEADER</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/50803?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 10:53:02 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fa66b457-3713-4f58-ac95-5ea45d0fe6a2</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;&lt;p&gt;
Strictly speaking, per C89, a bitfield always occupies an &amp;quot;int&amp;quot;.
Despite the signed-ness of that declaration, the actual value of the
bitfield may be signed or unsigned. (That is, an implementation may
sign-extend a bitfield when it&amp;#39;s used in comparisons, passed as a
parameter, and so on. The &amp;quot;1&amp;quot; you assigned to a 1-bit bitfield might
turn into a -1.) It&amp;#39;s up to the implementation what order bitfields
are allocated in those ints, and whether it uses bits MSB-to-LSB or
vice versa.&lt;/p&gt;

&lt;p&gt;
(In fact, bitfields are pretty much non-portable and
implementation-dependent. Add in the fact that most compilers
generate worse code for bitfield access than when you use
mask-and-shift operations, and I generally just recommend against
using them at all.)&lt;/p&gt;

&lt;p&gt;
Though it&amp;#39;s not standard, every compiler I&amp;#39;ve ever used allows you
to specify a type other than &amp;quot;int&amp;quot; to hold bitfields. Usually, this
is to conserve space.&lt;/p&gt;

&lt;p&gt;
Note that in your example you shift types from U8 to U16. This is
almost certainly guaranteed to prevent the compiler from putting the
bitfields in the same struct member, as it can do when the types
remain the same.&lt;/p&gt;

&lt;p&gt;
C compilers are not allowed to reorder fields in a structure. They
must occur in memory in the order of declaration. (C++ compilers are
allowed to reorder structs unless they&amp;#39;re declared &amp;#39;extern &amp;quot;C&amp;quot;&amp;#39;, but
I&amp;#39;ve never met a compiler that actually does so.)&lt;/p&gt;

&lt;p&gt;
So:&lt;/p&gt;

&lt;p&gt;
U8 :2 // two bits come out of the first byte&lt;br /&gt;
U16 :11 // can&amp;#39;t fit in the U8, move on to the next int&lt;br /&gt;
U16 : 11 // can&amp;#39;t fit in the previous U16, use another&lt;/p&gt;

&lt;p&gt;
I&amp;#39;d expect this structure to occupy 7 bytes.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/112922?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 10:02:16 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9d19f77d-7bf0-4c9b-ba25-f682791242fc</guid><dc:creator>Arthur Neagus</dc:creator><description>&lt;p&gt;&lt;p&gt;
sir eric&lt;/p&gt;

&lt;p&gt;
you are also right again you know!&lt;/p&gt;

&lt;p&gt;
thanks you for my mistake ;)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/99283?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 09:50:53 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:48566744-255e-440b-9337-fdf37fee000a</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;why you not write code to see asssembler of compiler
producing???&lt;br /&gt;
you use SRC option box&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I would not do that. It may be that you are so bright that you
&lt;b&gt;never&lt;/b&gt; forget to remove the SRC statement, I am not. For that
reason, &amp;quot;to see asssembler&amp;quot; I look at the .lst file which does not
affect anything.&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/48209?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 09:37:05 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:222d3970-5c0b-4f75-bb4f-6c852e3b58e0</guid><dc:creator>Gp F</dc:creator><description>&lt;p&gt;&lt;p&gt;
Which compiler are you using ? Compiling your example shows a size
of 7 which is correct since an UINT16 can hold a max. of 16 bits.
Please look at the symbol listing for details on size and layout of
the fields.&lt;/p&gt;

&lt;pre&gt;
C51 COMPILER V8.08a, COMPILATION OF MODULE Q
OBJECT MODULE PLACED IN q.OBJ
COMPILER INVOKED BY: c51.exe q.c CODE SB

line level    source

   1          typedef unsigned char  UCHAR8;
   2          typedef unsigned int   UINT16;
   3
   4          typedef struct DEKO_COMM_HEADER  {
   5            UINT16     m_uiMsgOpcode;
   6            UCHAR8  m_sblMsgPriority : 2;
   7            UINT16   m_sblDataLength :11;
   8            UINT16 m_sblMsgTimeStamp :11;
   9          } DEKO_COMM_HEADER;
  10
  11
  12          unsigned char  z;
  13
  14          void main (void)  {
  15   1        z = sizeof (DEKO_COMM_HEADER);
  16   1      }
ASSEMBLY LISTING OF GENERATED OBJECT CODE
             ; FUNCTION main (BEGIN)
                                           ; SOURCE LINE # 14
                                           ; SOURCE LINE # 15
0000 750007      R     MOV     z,#07H
                                           ; SOURCE LINE # 16
0003 22                RET
             ; FUNCTION main (END)


NAME                                    CLASS   MSPACE  TYPE    OFFSET  SIZE
====                                    =====   ======  ====    ======  ====
main . . . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
UINT16 . . . . . . . . . . . . . . . .  TYPEDEF  -----  U_INT    -----  2
UCHAR8 . . . . . . . . . . . . . . . .  TYPEDEF  -----  U_CHAR   -----  1
DEKO_COMM_HEADER . . . . . . . . . . .  TYPEDEF  -----  STRUCT   -----  7
  m_uiMsgOpcode. . . . . . . . . . . .  MEMBER   -----  U_INT    0000H  2
  m_sblMsgPriority . . . . . . . . . .  MEMBER   -----  FIELD    0002H  2.0
  m_sblDataLength. . . . . . . . . . .  MEMBER   -----  FIELD    0003H  11.0
  m_sblMsgTimeStamp. . . . . . . . . .  MEMBER   -----  FIELD    0005H  11.0
DEKO_COMM_HEADER . . . . . . . . . . .  * TAG *  -----  STRUCT   -----  7
  m_uiMsgOpcode. . . . . . . . . . . .  MEMBER   -----  U_INT    0000H  2
  m_sblMsgPriority . . . . . . . . . .  MEMBER   -----  FIELD    0002H  2.0
  m_sblDataLength. . . . . . . . . . .  MEMBER   -----  FIELD    0003H  11.0
  m_sblMsgTimeStamp. . . . . . . . . .  MEMBER   -----  FIELD    0005H  11.0
z. . . . . . . . . . . . . . . . . . .  PUBLIC   DATA   U_CHAR   0000H  1
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/112914?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 05:50:50 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:67eb8791-e68c-49e2-8134-9b91308a6403</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;thannk you for my mistake&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No mistake - just a clarification, perhaps!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/99293?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 05:35:04 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:992005cc-ce97-4f8b-a43b-3a06b4a2210d</guid><dc:creator>Kalib Rahib</dc:creator><description>&lt;p&gt;&lt;p&gt;
sir andy&lt;/p&gt;

&lt;p&gt;
you are right you know ;)&lt;/p&gt;

&lt;p&gt;
my book says &lt;i&gt;inplementation specifac&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
thannk you for my mistake&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/88362?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 05:23:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2a4f5a0c-9523-4883-9cdf-0705cd3e8fbf</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;it is undefineded in c standard &amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
To be precise, the &amp;#39;C&amp;#39; standard states that it is
&lt;i&gt;Implementation-Defined&lt;/i&gt; - this means that the implementation
can choose to do whatever it likes, but this &lt;b&gt;must&lt;/b&gt; be
&lt;b&gt;documented&lt;/b&gt;.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/75154?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 05:22:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f65583eb-16f5-46dc-82c4-4dcaac08c6a2</guid><dc:creator>Kalib Rahib</dc:creator><description>&lt;p&gt;&lt;p&gt;
mr dovalle&lt;/p&gt;

&lt;p&gt;
sorry for my mistake&lt;/p&gt;

&lt;p&gt;
why you not write code to see asssembler of compiler
producing???&lt;/p&gt;

&lt;p&gt;
you use &lt;b&gt;SRC&amp;lt;/b option box&lt;/b&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/48210?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 05:18:13 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3c2923a0-370c-4461-bb46-2611b990e828</guid><dc:creator>Kalib Rahib</dc:creator><description>&lt;p&gt;&lt;p&gt;
mr dovalle&lt;/p&gt;

&lt;p&gt;
you can n0t assumme how compiler alllocates fields in a structure
in a structure&lt;/p&gt;

&lt;p&gt;
it is undefineded in c standard you know is implermentation
specifac!!!!&lt;/p&gt;

&lt;p&gt;
you should use &lt;b&gt;sizeof&lt;/b&gt; so compiler says how much space&lt;/p&gt;

&lt;p&gt;
some compileer s use &lt;b&gt;pragma&lt;/b&gt; as control alignemenent&lt;/p&gt;

&lt;p&gt;
why you not write code to see asssemblery of compielr
producingh???&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sizeof of struct with  bit Field</title><link>https://community.arm.com/thread/50802?ContentTypeID=1</link><pubDate>Tue, 05 Jun 2007 04:37:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b5f8b60a-2458-4f8d-ad9f-9eb6bbf2cd80</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
I don&amp;#39;t know how you expected to get the size 5.&lt;/p&gt;

&lt;p&gt;
11+11+2 is 24 bits, but they can normally not be formed from 3
separate bytes, since a single byte can&amp;#39;t store a bitfield with 11
bits. The compiler really want all fields for a bit field to be
stored in the same byte/word/dword/xxx to allow all bits to be
assignable with a single assign.&lt;/p&gt;

&lt;p&gt;
The compiler may use a single 32-bit number for all bit fields
(giving a total size of 6).&lt;/p&gt;

&lt;p&gt;
The compiler may also allocate 2 16-bit numbers (one for each
11-bit value) and then decide to piggyback the 2-bit value into one
of these 16-bit integers. This gives a total size of 6.&lt;/p&gt;

&lt;p&gt;
The compiler may decide that the 2-bit field should not be
piggybacked, thereby consuming 5 bytes for the bit fields, and 2
bytes for the opcode. For a 8-bit processor, this may result in a 7
byte large struct. A 16 or 32-bit processor would then have to pad
the struct to make sure that an array of structs always places the
16-bit integer on an even address, i.e. making the struct 8 bytes
large.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>