<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Structure size &amp; alignment problems in C compiler in Classic/Legacy CodeWarrior</title>
    <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/CodeWarrior-Tools-for-StarCore-Processors/m-p/125888#M141</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;FONT color="#ff0000"&gt;To help you find solutions to problems that have already been solved, we have posted this message. It contains an entire topic ported from a separate forum. The original message and all replies are in this single message.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;STRONG&gt;ahunanyan&lt;BR /&gt;Posted: Mar 02, 2005 - 01:47 AM&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Hi all.&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;According to&lt;/DIV&gt;&lt;DIV&gt;“Metrowerks\CodeWarrior\Help\PDF\MW_Enterprise_C_Compiler_Manual.pdf”&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;struct s {char a; int; char b[2];} v;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;sizeof(v) is 3, but I got the size of that structure is 4 and in general all structures have 4 bytes aligned size.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Other issue: all structures have at least 4 bytes alignment regardless of structure content. Even if structure has one char member only, it is aligned to 4 bytes again.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;For example:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;struct z {char a; };&lt;BR /&gt;z- aligned to 4.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have searched the solution in compiler options but haven’t found anything.&lt;/DIV&gt;&lt;DIV&gt;How I can avoid this 4 byte alignment problem ?&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;mw_pirrle&lt;BR /&gt;Posted: Mar 03, 2005 - 07:45 AM&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Dear Customer,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;By default the Compiler uses this alignment.&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;Some documentation (key) are available in the release note folder (&amp;lt;install_path&amp;gt;Release_Notes\StarCore_Notes\Compiler_Notes)&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;Extract of the SCC_COBJ_ICODE_undocumented_features.txt file&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-I- Controlling data layout rules&lt;BR /&gt;=================================&lt;/DIV&gt;&lt;DIV&gt;By default the compiler follows the rules defined in the ABI when it has&lt;BR /&gt;create the layout of a given C variable.&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;In some cases these layout rules can be problematic when migrating existing code or when willing to minimize data size. The two most strigent points are:&lt;BR /&gt;structure alignment rules: the ABI states that a structure should be aligned at least on 4 byte boundary. This rule may induce unneccessary padding space&lt;BR /&gt;enumerated type mapping: the ABI states that enum are strictly equivalent to plain int disregrading the actual content of the enum.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;bit field layout: the ABI states that a bit field is always considered as being part of a plain int&amp;nbsp;container meaning that the compiler has to pad from the last bitfield field till the next 4 byte aligned container&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;These three rules can now be changed through options.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-I.1- Changing minimum structure alignment:&lt;BR /&gt;-------------------------------------------&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;scc -Xcfe "--min_struct_align=Value" ....&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Where Value is &amp;gt;= 0&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The effect is assuming we have a structure S containing fields f1, .. fn&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Alignment(S) = Max ( Value, Max on f1 .. fn (Alignment (fi))&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;It should be noticed that the ABI rule is equivalent to scc -Xcfe "--min_struct_align=0"&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;For example: pad.c&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/DIV&gt;&lt;DIV&gt;typedef struct {&lt;BR /&gt;short f1;&lt;BR /&gt;} T1;&lt;/DIV&gt;&lt;DIV&gt;typedef struct {&lt;BR /&gt;char f1;&lt;BR /&gt;struct {&lt;BR /&gt;char f21;&lt;BR /&gt;char f22;&lt;BR /&gt;} f2;&lt;BR /&gt;char f3;&lt;BR /&gt;} T2;&lt;/DIV&gt;&lt;DIV&gt;void main()&lt;/DIV&gt;&lt;DIV&gt;{&lt;BR /&gt;printf("Sizeof(T1) = %d\n", sizeof(T1));&lt;BR /&gt;printf("Sizeof(T2) = %d\n", sizeof(T2));&lt;BR /&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;scc -O3 pad.c&lt;/DIV&gt;&lt;DIV&gt;Sizeof(T1) = 4&lt;BR /&gt;Sizeof(T2) = 12&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;scc -Xcfe "--min_struct_align=0" -O3 pad.c&lt;BR /&gt;Sizeof(T1) = 2&lt;BR /&gt;Sizeof(T2) = 4&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The paths and files names are for the Starcore V2.6.&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you're using an older version all these information are available too in the same folder but may be under other file names.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;BR /&gt;Pascal Irrle&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;STRONG&gt;ahunanyan&lt;BR /&gt;Posted: Mar 04, 2005 - 04:52 AM&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thank You.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;With best regards&lt;BR /&gt;Armen&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 04 Feb 2006 03:08:33 GMT</pubDate>
    <dc:creator>marc_paquette</dc:creator>
    <dc:date>2006-02-04T03:08:33Z</dc:date>
    <item>
      <title>CodeWarrior Tools for StarCore Processors</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/CodeWarrior-Tools-for-StarCore-Processors/m-p/125887#M140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;To help you find solutions to problems that have already been solved, we have posted this thread. Each message in this thread contains an entire topic ported from a separate forum. The original message and all replies are in a single message.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Feb 2006 03:05:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/CodeWarrior-Tools-for-StarCore-Processors/m-p/125887#M140</guid>
      <dc:creator>marc_paquette</dc:creator>
      <dc:date>2006-02-04T03:05:09Z</dc:date>
    </item>
    <item>
      <title>Structure size &amp; alignment problems in C compiler</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/CodeWarrior-Tools-for-StarCore-Processors/m-p/125888#M141</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;FONT color="#ff0000"&gt;To help you find solutions to problems that have already been solved, we have posted this message. It contains an entire topic ported from a separate forum. The original message and all replies are in this single message.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;STRONG&gt;ahunanyan&lt;BR /&gt;Posted: Mar 02, 2005 - 01:47 AM&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Hi all.&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;According to&lt;/DIV&gt;&lt;DIV&gt;“Metrowerks\CodeWarrior\Help\PDF\MW_Enterprise_C_Compiler_Manual.pdf”&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;struct s {char a; int; char b[2];} v;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;sizeof(v) is 3, but I got the size of that structure is 4 and in general all structures have 4 bytes aligned size.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Other issue: all structures have at least 4 bytes alignment regardless of structure content. Even if structure has one char member only, it is aligned to 4 bytes again.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;For example:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;struct z {char a; };&lt;BR /&gt;z- aligned to 4.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have searched the solution in compiler options but haven’t found anything.&lt;/DIV&gt;&lt;DIV&gt;How I can avoid this 4 byte alignment problem ?&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;mw_pirrle&lt;BR /&gt;Posted: Mar 03, 2005 - 07:45 AM&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Dear Customer,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;By default the Compiler uses this alignment.&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;Some documentation (key) are available in the release note folder (&amp;lt;install_path&amp;gt;Release_Notes\StarCore_Notes\Compiler_Notes)&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;Extract of the SCC_COBJ_ICODE_undocumented_features.txt file&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-I- Controlling data layout rules&lt;BR /&gt;=================================&lt;/DIV&gt;&lt;DIV&gt;By default the compiler follows the rules defined in the ABI when it has&lt;BR /&gt;create the layout of a given C variable.&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;In some cases these layout rules can be problematic when migrating existing code or when willing to minimize data size. The two most strigent points are:&lt;BR /&gt;structure alignment rules: the ABI states that a structure should be aligned at least on 4 byte boundary. This rule may induce unneccessary padding space&lt;BR /&gt;enumerated type mapping: the ABI states that enum are strictly equivalent to plain int disregrading the actual content of the enum.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;bit field layout: the ABI states that a bit field is always considered as being part of a plain int&amp;nbsp;container meaning that the compiler has to pad from the last bitfield field till the next 4 byte aligned container&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;These three rules can now be changed through options.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;-I.1- Changing minimum structure alignment:&lt;BR /&gt;-------------------------------------------&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;scc -Xcfe "--min_struct_align=Value" ....&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Where Value is &amp;gt;= 0&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The effect is assuming we have a structure S containing fields f1, .. fn&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Alignment(S) = Max ( Value, Max on f1 .. fn (Alignment (fi))&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;It should be noticed that the ABI rule is equivalent to scc -Xcfe "--min_struct_align=0"&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;For example: pad.c&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/DIV&gt;&lt;DIV&gt;typedef struct {&lt;BR /&gt;short f1;&lt;BR /&gt;} T1;&lt;/DIV&gt;&lt;DIV&gt;typedef struct {&lt;BR /&gt;char f1;&lt;BR /&gt;struct {&lt;BR /&gt;char f21;&lt;BR /&gt;char f22;&lt;BR /&gt;} f2;&lt;BR /&gt;char f3;&lt;BR /&gt;} T2;&lt;/DIV&gt;&lt;DIV&gt;void main()&lt;/DIV&gt;&lt;DIV&gt;{&lt;BR /&gt;printf("Sizeof(T1) = %d\n", sizeof(T1));&lt;BR /&gt;printf("Sizeof(T2) = %d\n", sizeof(T2));&lt;BR /&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;scc -O3 pad.c&lt;/DIV&gt;&lt;DIV&gt;Sizeof(T1) = 4&lt;BR /&gt;Sizeof(T2) = 12&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;scc -Xcfe "--min_struct_align=0" -O3 pad.c&lt;BR /&gt;Sizeof(T1) = 2&lt;BR /&gt;Sizeof(T2) = 4&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The paths and files names are for the Starcore V2.6.&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you're using an older version all these information are available too in the same folder but may be under other file names.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;BR /&gt;Pascal Irrle&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;STRONG&gt;ahunanyan&lt;BR /&gt;Posted: Mar 04, 2005 - 04:52 AM&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thank You.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;With best regards&lt;BR /&gt;Armen&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Feb 2006 03:08:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/CodeWarrior-Tools-for-StarCore-Processors/m-p/125888#M141</guid>
      <dc:creator>marc_paquette</dc:creator>
      <dc:date>2006-02-04T03:08:33Z</dc:date>
    </item>
  </channel>
</rss>

