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.

C6618 pragma STRUCT_ALIGN

Hello,

I want to use the #pragma STRUCT_ALIGN to allign a struct on a C6618.

suppose I use this code:

typedef struct alligned_struct
{
    uint8_t    tag;
    uint8_t    value;
} alligned;

#pragma STRUCT_ALIGN (alligned_struct, 2);
#pragma STRUCT_ALIGN (alligned, 2);

This struct require 2 byte, in fact the sizeof(alligned)=2.

With the pragma I expect that the struct will be alligned on 2 byte boundary, so the sizeof(alligned) should be 4, but I always get a sizeof()=2.

What's wrong?
  • Luca_fx,

    The TCI6618 (there is no C6618) device is not directly supported on the forums. If one of the numbers is incorrect instead of the prefix (did you mean C6678?), please let us know that. The TCI6618 device is sold to a limited market for which we provide alternate support models. You would contact your direct sales support channel to determine that support, as appropriate.

    Since this question is a compiler question and not about the device specifically, it will be best for it to be moved to the TI C/C++ Compiler Forum.

    The alignment of a struct will not directly affect the size of that struct. The size will still be 2 bytes and there may be a hole in memory after the struct, depending on the alignment of the next variable placed after it. Or something else may fill that hole.

    Regards,
    RandyP

     

    If you need more help, please reply back. If this answers the question, please click  Verify Answer  , below.

  • Simply looking at the size of a structure tells you nothing about the alignment.  Seeing the alignment is a bit tricky.  I'll show one way to do it.  

    I compiled the following code, which is based on your example above ...

    #include <stdint.h>
    
    #pragma STRUCT_ALIGN (alligned_struct, 2);
    typedef struct alligned_struct
    {
        uint8_t    tag;
        uint8_t    value;
    } alligned;
    
    typedef struct no_align
    {
        uint8_t    tag;
        uint8_t    value;
    } not_alligned;
    
    alligned al;           // is aligned
    not_alligned nal;      // is not aligned
    

    Then use these commands (or similar ones) ...

    % cl6x -s file.c
    % fgrep usect file.asm
    _al:    .usect  ".far",2,2
    _nal:   .usect  ".far",2,1
    

    You are looking at the usect directives issued by the compiler to create these two structures.  The structure al is aligned, and nal is not aligned.  Notice how the last number is 2 for al, and 1 for nal.  That last number is the alignment required for that data item.  Because of that 2, the assembler and linker work together to insure that al is placed on a 2 byte boundary.  By contrast, nal can end up at any address, even an odd byte address.

    Thanks and regards,

    -George