This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Structure Padding

Hi,

 I am using TMS570 processor and TMS470 compiler. Is there any option in the compiler to disable the padding in struture ?

If i create a structre with elements as int,char,int then size should be 5 not the 6. Let me know the option availability.

  • Be aware of speed vs size (code and data alignment).

    Looking at the compiler UG http://www.ti.com/lit/ug/spnu151g/spnu151g.pdf - section 5.16.3 Variable Attributes

    The following variable attributes are supported: aligned, deprecated, mode, packed, section, transparent_union, unused, and used.

    The packed attribute for structure and union types is available only when there is hardware support for unaligned accesses. This means when --unaligned_access=on, which it is by default for the Cortex devices (A8, R4, M3, M4) - your TMS570 processor

    How to use packed attribute is described in GCC 4.2 (see 

    http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Variable-Attributes.html#Variable-Attributes).

    The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

    Here is a structure in which the field x is packed, so that it immediately follows a:

              struct foo
              {
                char a;
                int x[2] __attribute__ ((packed));
              };

    Not tested/verified, Hope this helps.


  • Hi Joson,

    Thanks for the information. I have tried the same, but getting following error when i declare the structure that you have given as example.

    "semicolon (;) is expected ". Please try if you can and let me know. I am using CCS V4 and TMS570 processor. Do i need to change any project seetings ?

  • Bindu,

    You are Correct, need to Enable support for GCC Extensions (command line option "--gcc").

    OR in CCS:  Project->Properties->C/C++ Build->TMS470 Compiler->Language Options->Enable support for GCC Extensions.

     

  •  

    I enabled the GCC Extenstions as you suggested. Now the error is removed . But i get an wanring saying "invalid attribute for variable x"

  • Hi Joson,

    Can you update on this. I see that padding is happening after removing error.

  • Hi Bindu,

    If you are getting a warning "Invalid attribute" - packing is not applied to the field and hence the padding. You will get this warning when you apply the pack attribute to a field that is not a "primitive" data type (something that you derived like a struct).

    If you get no warning - you should not see any padding between the field with pack attribute and the field immed. above.

  • Hi Joson,

    I have used the same structure that you have given as example. The datatype of the structure element that i have used packed attribute is int. 

    What do you mean by primitive data type ?

    My actual structure consists of 170 K byes of data. In one of the post i see that there is a way to apply this attribute to entire structure instead for individual elements. I tried the same. Same warning i can observe. 

    __attribute__ (__packed__)  - Is this correct for entire structure ?

    Let me know what is the issue.

  • Please let me know the update on this status

  • Bindu,

    What version of the compiler are you using? The support for packed structures was added in the 4.6.1 release. Are you still seeing the warning about an invalid attribute? You can specify the packed attribute on either a field in the struct or you can specify it for an entire struct using the syntax below:

    struct foo 
              {
                char a;
                int x[2];
              } __attribute__((packed));

    Also, notice that two sets of parenthesis are required. When you asked about the syntax you only used one set.

  • I was using as you specified only i.e. used two sets of parenthesis. But the warning occurs. I found in forum that packed attribute is not supported for Cortex-R4 processor for the compiler versions below 4.9. Mine is 4.6.1 only. Please confirm me.

  • Below are details of my application. I am using 4.6.3 version not 4.6.1. In onother post, i got a reply saying that packed support for cortex r4 is above 4.7.1.

     

     

    Processor : TMS570LS20216, Code Composer Studio : 4.2.0.10018,  TMS470 C/C++ CODE GENERATION TOOLSRelease Notes4.6.3


     

  • Bindu,

    You are correct about packed support for Cortex-R4 being available starting with the 4.7 release of the compiler. I apologize for overlooking that. The 4.7 release has been made inactive. The current active release supporting packed for Cortex-R4 is the 4.9 release. You will need to upgrade to that version in order to use the packed attribute.