<?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>Alignment of bit fields problem</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/26256/alignment-of-bit-fields-problem</link><description> 
Hi, 

 
I&amp;#39;m working with at91sam7s256. Of course i&amp;#39;m using Keil. 
I want to align struct with bitfields one by one because a want to
cast buffer on my struct and read data using that struct. the problem
is that: 

 
i made struct with attribute packed</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Alignment of bit fields problem</title><link>https://community.arm.com/thread/90727?ContentTypeID=1</link><pubDate>Sun, 14 Nov 2010 03:05:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0d59df85-4980-4b43-8cbc-4bf1d8c39b30</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;the structure should have 16 bytes&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No, it shouldn&amp;#39;t. You &lt;b&gt;want&lt;/b&gt; it to have that size, but
there&amp;#39;s no particularly compelling reason why this wish should be
granted. Bitfields are meant for internal work of a C program,
&lt;b&gt;not&lt;/b&gt; for duplicating external storage patterns.&lt;/p&gt;

&lt;p&gt;
The lesson to be learned is: &lt;b&gt;Never&lt;/b&gt; make any assumptions
about the storage representation of bitfields. Each individual
bitfield will hold values within its defined range and signedness.
All other aspects of bitfields are outside your control, and you
should never rely on them. If you really need to control the storage
layout of objects of fractional size (in bytes), you have to use
shifting and masking on arrays of unsigned char.&lt;/p&gt;

&lt;p&gt;
This issue is one of the typical mistakes just about every C
programmer makes at some point on their learning curve. The only way
they could be avoided would be good textbooks, skilled teachers ---
both of which are dangerously rare.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Alignment of bit fields problem</title><link>https://community.arm.com/thread/90731?ContentTypeID=1</link><pubDate>Sat, 13 Nov 2010 11:15:37 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:cc69ad11-b3e4-4054-85d9-ebee465006de</guid><dc:creator>ashley madison</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;a href="http://www.keil.com/support/man/docs/armccref/armccref_babjddhe.htm"&gt;
&lt;a href="http://www.keil.com/support/man/docs/armccref/armccref_babjddhe.htm"&gt;www.keil.com/.../armccref_babjddhe.htm&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Alignment of bit fields problem</title><link>https://community.arm.com/thread/58873?ContentTypeID=1</link><pubDate>Sat, 13 Nov 2010 09:46:13 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8cc7e773-a189-4ac4-85af-f7e0a23c365b</guid><dc:creator>Grzegorz Rogon</dc:creator><description>&lt;p&gt;&lt;p&gt;
It was a trap for fairy :)&lt;br /&gt;
Ok, and now seriously. Its a struct i want to use. I need to cast
buffer (16 bytes) on this struct and the structure should have 16
bytes but compiler (Keil) returns that the structure have 18 bytes. I
checked in Devcpp, the structure have 16 bytes.&lt;/p&gt;

&lt;pre&gt;
struct __attribute__ ((packed)) Structure{
    unsigned char field1:2;
    unsigned char field2:6;
    unsigned char field3:8;
    unsigned char field4:8;
    unsigned char field5:8;
    unsigned int  field6:12;
    unsigned char field7:4;
    unsigned char field8:1;
    unsigned char field9:1;
    unsigned char field10:1;
    unsigned char field11:1;
    unsigned char field12:2;
    unsigned int  field13:12;
    unsigned char field14:3;
    unsigned char field15:3;
    unsigned char field16:3;
    unsigned char field17:3;
    unsigned char field18:3;
    unsigned char field19:1;
    unsigned char field20:7;
    unsigned char field21:7;
    unsigned char field22:1;
    unsigned char field23:2;
    unsigned char field24:3;
    unsigned char field25:4;
    unsigned char field26:1;
    unsigned char field27:5;
    unsigned char field28:1;
    unsigned char field29:1;
    unsigned char field30:1;
    unsigned char field31:1;
    unsigned char field32:2;
    unsigned char field33:2;
    unsigned char field34:7;
    unsigned char field35:1;
};
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Alignment of bit fields problem</title><link>https://community.arm.com/thread/58872?ContentTypeID=1</link><pubDate>Sat, 13 Nov 2010 09:08:32 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:956bafed-bcf6-43a7-8f79-279cdd5329f2</guid><dc:creator>Per Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Gee - any reason why you didn&amp;#39;t see a need to post the struct
before asking if anyone can help you? Do you expect us to hack your
computer to get access to the source code?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>