7 Replies Latest reply on May 11, 2013 3:00 PM by helic_263931

    Structure packing


      Does Creator 2.2 have any control of structure packing of data?  For example I create a structure like:



      uint8 LoadedFlag;

      uint16 Version;

      struct {

      uint16 OutRef;

      } ConfigData;


      When I look in memory I see two bytes used for LoadedFlag, rather than one.  Is there any way to force the structure to be packed?  In MS VC++ it would be "pragma pack (1)", but I cannot find any reference to packing control in the docs...


      Thanks, Russ

        • 1. Re: Structure packing

          Boy the web page scrambled my structure example.  Let me try again, if it still does not work I guess you can figure out what I mean.

          typedef struct {     uint8  LoadedFlag;     uint16 Version;     uint16 OutRef; } ConfigData;  Maybe that will work better...  Russ
          • 2. Re: Structure packing

            Very curious about this, I did not realize compilers did this.


            I found this for Keil, but does not apply to PSOC as GCC


            used for ARM, PSOC 4/5.








            I did find this - axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/




            On page 309/310 in Creator, GCC doc, there is a discussion of alignment and packling.




            Regards, Dana.

            • 3. Re: Structure packing

               standard GCC: __attribute__ (__packed__) will do it BUT ...




              care needs to be taken. packed structure will slow down memory fetches or possibly cause alignment exceptions on some processors. standard practice is to only use it for interprocessor communication or possible external storage. in memory execution is usually best if left unpacked.





              • 4. Re: Structure packing

                The ARM core works best when values are stored on even addresses same rules for byte-values. Probably some optimization settings are hindering a ram-space optimization. The question is: can you spend that byte..?





                • 5. Re: Structure packing

                  Yes, I can spend the byte, this time.  It is a packet of data being sent over the serial interface, and it did not match what the receiving program was expecting.  THIS TIME I was able to modify the receiving program, but what if I could not?  Then it would be necessary to pack the structure.


                  Unpacked structures are organized so that each item starts on an even address boundary that matches the native integer size of the processor.  This makes data fetches and stores much more efficient, thus increasing speed.  But with the fast processors of today I do not think it makes all that much difference.  I guess in a real time process maybe it could be useful...


                  Well I now have the answer in case I need to pack a struct in the future.


                  Thanks, Russ

                  • 6. Re: Structure packing

                     that's one of the issue when needs check when needs to port project from one cpu to another. such as from PSoC3 to/from PSoC5.

                    • 7. Re: Structure packing

                      Memory access times don't care about the CPU speed, but about memory access times. And the latter ones haven't improved that much in the last 20 years...


                      Essentially the problem is when a data element larger than 1 byte is places around a word (or double- or quad-word, depending on the architecture) boundary. Then every operation on the element requires now 2 memory accesses, which will slow it down tremendously. It gets even more problematic when the operation is supposed to be atomic.


                      See also Wikipedia: http://en.wikipedia.org/wiki/Data_structure_alignment