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.

Handling of structures with elements of different size

What are the guidelines for defining structures with elements of variable element sizes ( 32 bit, 16 bit 8 bit and boolean)?

Should any specific alignment needs to be taken care? There is a #pragma directive called STRUCT_ALIGN available in the help, but Iam not sure what purpose does it serve.

  • Hi Sivan,

    if you're using CCE or CCS have a look a the C compiler Users Guide (http://focus.ti.com/lit/ug/slau132c/slau132c.pdf) - par. 6.2.1.2 and ongoing. There is an excelent explaination.

    Rgds
    aBUGSworstnightmare

  • It depends upon the device family you are using. Another answer pointed to a discussion of how structures are handled by the MSP430. If, on the other hand, you were using a device which uses the C6000 CPU, then I would recommend looking at two sections from the C Compiler Users Guide (spru187o.pdf); specifically:

    • 7.2.1.8 Structures and Arrays
    • 6.8.19 The STRUCT_ALIGN pragma

    As with the other response, the Users Guide provides a pretty good discussion of how structures are handled by the compiler. Here are a few notes regarding structure alignment:

    • If you create a C structure using the compiler, it should have no problem reading that structure. (You have to be much more careful when creating data structures in assembly.)
    • Structures are automatically aligned on the largest datatype they contain.
    • The start of Arrays of chars and shorts (outside of structures) are aligned on 8-byte boundaries on most C6000-based devices, which helps with optimization. This allows the processor hardware to better utilize the 64-bit data buses.
    • Arrays (of chars and shorts) contained within structures are not automatically aligned to 8-byte boundaries. If this is desirable (for optimization purposes), then you would need to force the alignment of the array by preceding the array element with a 'double' or 'long long'; or, alternatively you could force alignment using a union. 

    Hope this helps!