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.

Compiler/TMS320F28375D: How to prevent 16 bit padding

Part Number: TMS320F28375D

Tool/software: TI C/C++ Compiler

Hi,

Our customer wants to know how to prevent 16 bit padding in the code as shown below.

    struct {
        unsigned short x;
        unsigned long y;
        unsigned short z;
        unsigned long w;
    } c = { 0x1234, 0x3456789a, 0xabcd, 0xcdef1234 };

Could you please help to get the answer to the request?

Regards,


  • Hi Yoshita,

    I believe the reason is you have a short variable followed by a long variable. A long variable should be always aligned by 2 bytes. That is, the start address needs to even (not odd) That is the reason you see 16 bit padding in between the short and long variables.
    Could you rearrange the struct as shown below?

    struct {
    short x;
    short z;
    long y;
    long w;
    }
    This makes sure that x and z occupies the first 2 bytes nad the next available address is aligned by 2 bytes

    Regards,
    Veena
  • Hi Veena,

    Thanks for your quick reply.

    Yes, your suggestion can be one of method.
    I'll ask customer it.

    But could you please investigate whether there is a compile option or pragma such like "pack"?
    Our customer uses compiler v17.9.0.STS.
    If a pragma "DATA_ALIGN" can be another way to prevent the padding, could you give some sample code how to use please?

    Regards,
  • Hi Veena,

    Our customer is requesting ;
    1. As for the sentence "A long variable should be always aligned by 2 bytes.",
    in what document file the description can be seen?
    2. Since they will re-use the source codes already they have, they need to avoid a lot of work to change the structure.
    Is there any alternative idea for it?

    Regards,
  • Hi Yoshita,

    Since a long variable is of size 2 bytes, I assume the compiler auto-aligns the variable to get an even address. I am not sure if there is a way to have long variable with unaligned address.
    I have re-directed your query to compiler team.

    Regards,
    Veena
  • Unfortunately, the C2000 compiler has no method for packing the members of a structure.  

    Thanks and regards,

    -George