<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>create a packed struct</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/20392/create-a-packed-struct</link><description>Long story short, I need to create a packed struct. 
 I knew the pragma way, to change aligment, didn&amp;#39;t work.
I looked up and found the __packed (I&amp;#39;m using Keil &amp;#181;Vision) here: www.keil.com/.../armcc_chr1359124229695.htm 
 So in the end, here&amp;#39;s my theoritically</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 22 Mar 2017 13:57:20 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/20392/create-a-packed-struct" /><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79502?ContentTypeID=1</link><pubDate>Wed, 22 Mar 2017 13:57:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60fd649a-1fb5-42c8-8700-c8940839cf05</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;Left that problem aside for a little while, and now when getting back to it, packed structures are packed. No clue what solved it, besides that as shown by the answer I&amp;#39;m commenting, it should not have happened in first place.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79501?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 14:20:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6fafc35-77f8-4fb6-8e1d-f39e1d6f3188</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;@Keton Just created a new project, and uint16_t is 2, uint16_t + uint_8t is 4, the same packed is 3. All good. I should have mentionned I was running an ARM mbed exported project, I start thinking it plays a role here... Will search more in that direction.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79500?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 13:42:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b8c59c2c-e5ef-487b-b21f-89349f9a8e56</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;I had tried the various optimization myself without any success.
And of course, that&amp;#39;s right, if it was thumb, you wouldn&amp;#39;t get 2 bytes alignment on x86-64. Thanks for your time!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79505?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 11:05:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0dafac46-e77d-4a4a-b0fe-271dc70ab2b9</guid><dc:creator>Keton</dc:creator><description>&lt;p&gt;@Jonathan I&amp;#39;m using default Keil project settings. I&amp;#39;ve also checked various optimization modes and results are the same. I may be wrong but if this was arm/thumb issue why this also happens on x86 in 64 bit mode?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79504?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 10:36:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d947af8e-0828-4d94-8229-4283b9c046e0</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;The one thing that comes to my mind, but I don&amp;#39;t know enough about it to be sure, is that for some reason, your code could be compiled to Thumb instructions, which I assume forces 16 bits aligment. Complete guess, I don&amp;#39;t know what would cause Thumb vs ARM compilation, and I&amp;#39;m unsure Thumb uses 16 bits alignments.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79503?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 10:34:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45460264-0165-4297-be39-0821152accd2</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;Thank you very much for your test. I have some other softwares in which I use #pragma pack(1), compiled with GCC and executed on RPi, and I have no problem with that (ie, packing occurs).
And now you said that with Keil 5.22, and various other compilers, you get the proper result. So far, my conclusion is there&amp;#39;s an issue with my Keil/compiler configuration at some point.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79499?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 09:57:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:de831083-c59d-47a4-9181-2d6b2cbb198b</guid><dc:creator>Keton</dc:creator><description>&lt;p&gt;@Jonathan I&amp;#39;ve checked this on x64 using gcc, nRF52 using both Keil and gcc and Raspberry pi using gcc. Each time following code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;typedef struct
{
	uint16_t test;
} short_single_t;

typedef struct
{
	uint16_t test;
	uint16_t test2;
} short_double_t;

struct PackedStructure
{
  uint16_t packedValue;
};

#ifdef NRF52
#define printf NRF_LOG_INFO
#endif

static void struct_size_test(void)
{
	printf(&amp;quot;sizeof(short_single_t)==%zu\n&amp;quot;,sizeof(short_single_t));
	printf(&amp;quot;sizeof(short_double_t)==%zu\n&amp;quot;,sizeof(short_double_t));
	printf(&amp;quot;sizeof(struct PackedStructure)==%zu\n&amp;quot;,sizeof(struct PackedStructure));
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;gives me this result:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sizeof(short_single_t)==2
sizeof(short_double_t)==4
sizeof(struct PackedStructure)==2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;According to &lt;a href="http://www.catb.org/esr/structure-packing/"&gt;this guide&lt;/a&gt; behavior I see is mandated by C standard. Shorts (uint16_t is short) are self-aligned to 2 byte boundary so struct containing only shorts will have 2 byte alignment and no padding.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79496?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 07:14:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:14190c91-08ba-4b6a-a390-23162dcee24d</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;@Keton this is very interesting, using Keil 5.22 too, and the unpacked structure gives me 4 bytes. Now, could your platform be 16 bits aligned?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79495?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 07:13:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3a50f3c-b288-4672-9da9-711b6c0b952a</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;@awneil, right, I&amp;#39;ll ask on Keil&amp;#39;s forum too. As to why I &lt;strong&gt;need&lt;/strong&gt; it... you never &lt;strong&gt;need&lt;/strong&gt; packed structures, it&amp;#39;s a convenience, what I need is to save space (not using 32 bits for a boolean), and what I want on top is a convenient way of doing it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79498?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 07:09:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a07849e2-31f3-44bb-bed1-bd16a6e65487</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;Thanks, unfortunately it didn&amp;#39;t help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79497?ContentTypeID=1</link><pubDate>Sun, 12 Mar 2017 00:36:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6eae2253-adae-4c58-8205-e4e57b8b6528</guid><dc:creator>Alex</dc:creator><description>&lt;p&gt;Try&lt;/p&gt;
&lt;p&gt;typedef struct
{
uint16_t var;
} &lt;strong&gt;attribute&lt;/strong&gt;((&lt;strong&gt;packed&lt;/strong&gt;)) name_of_struct;&lt;/p&gt;
&lt;p&gt;Hope this helps&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79494?ContentTypeID=1</link><pubDate>Sat, 11 Mar 2017 19:45:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:536c2c31-3c05-4a95-b5b9-63e95c8d008c</guid><dc:creator>Keton</dc:creator><description>&lt;p&gt;Get rid of all this __packed stuff and problem will go away. I use structs a lot describing memory mapped IO and I never observed additional padding. In fact I&amp;#39;ve checked in Keil 5.22 and this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;typedef struct 
{ 
   uint16_t test_field;
} test;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;has sizeof() of 2&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79493?ContentTypeID=1</link><pubDate>Sat, 11 Mar 2017 19:03:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d553a19d-1de2-48bb-bd60-9a4fca4cbdeb</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;But why do you &amp;quot;need&amp;quot; to create a packed struct?&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.catb.org/~esr/faqs/smart-questions.html#goal"&gt;www.catb.org/.../smart-questions.html&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: create a packed struct</title><link>https://devzone.nordicsemi.com/thread/79492?ContentTypeID=1</link><pubDate>Sat, 11 Mar 2017 19:02:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a0b5af9c-0d5e-4918-b8fe-4e7e3ce65b20</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;As this is specific to &lt;strong&gt;Keil&lt;/strong&gt;, it&amp;#39;s probably best to ask on the &lt;strong&gt;Keil&lt;/strong&gt; forum...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>