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.

Struct Packing in TMS320F280335



I am using CCS 4.2 and i have declared one struct as fallows

typedef struct
{
   Uint16 FuncCode;
   int32 Data;
   Uint16 Crc;
   int16 Resv[16];
}READ_COMMAND;

READ_COMMAND ReadCmd;

The "ReadCmd" variable declared above allocates more memory than the actual size. 

The actual size is 20 words but it allocates 22 words. 

how is this possible?

how can i do packing the above struct?

thanks

  • Bhausaheb choudhari said:

    The actual size is 20 words but it allocates 22 words. 

    how is this possible?

    The type int32 requires 2 words that are aligned on an even word boundary.  So, there is a padding word between FuncCode and Data.  There is also a padding word at the very end of the structure, to make the overall length even.  The second padding word is needed in case you form an array of READ_COMMAND structures.  That is the only way to assure that each Data field in the array is aligned.

    Bhausaheb choudhari said:
    how can i do packing the above struct?

    If it is possible, change the struct so Data is the first member.  If that is not possible, then nothing can be done.

    Thanks and regards,

    -George

  • Thanks George

    I can't change sequence because the structure used in the communication interface.
    I changed Int32 Data to int16 Data[2] as fallows and this allocates the exact size and there was no padding.

    typedef struct
    {
    Uint16 FuncCode;
    int16 Data[2];
    Uint16 Crc;
    int16 Resv[16];
    }READ_COMMAND;

    One thing I Need to take care is, converting Data[2] to Int32 Data and from int32 to 2 int16 while transmission and reception over communication channel.

    Thanks again
  • Bhausaheb choudhari said:
    One thing I Need to take care is, converting Data[2] to Int32 Data and from int32 to 2 int16 while transmission and reception over communication channel.

    The best way to do that is by directly copying the data, one 16-bit word at a time, from the READ_COMMAND structure to the int32 variable, or vice versa.  Please do not get into trouble by casting the address of the READ_COMMAND "Data" to a pointer to a int32 type.  The compiler is not required to account for the misalignment that occurs in that case.  When that occurs, there are no diagnostics, and the code silently does the wrong thing.  It can be very difficult to debug.

    Thanks and regards,

    -George

  • Yes, you are correct.
    will follow the same.

    Thanks & Regards,
    Bhausaheb