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.

How to define a float vector within a struct double word alignment



I have defined a struct type as follows:

#define MAXNUM  100

typedef struct tag_OBJ
{
   float val[MAXNUM];
}OBJ;

In my application program, a variable with the struct type OBJ is defined:
OBJ var1;

how to make sure the float vector val in the struct OBJ double word aligned?

Usually, there are two ways to define the float vector double word alignment. If a variable like 'float val[MAXNUM]' is defined in the included file,
and then a macro as '#pragma DATA_ALIGN(val,8)' could be effective to make sure the variable val is double word aligned.

If the dynamic memory allocation for float pointer like 'float *val', it is common to use the memalign function to realize the double word alignment as:
float *val = (float *)memalign(8, sizeof(float) * MAXNUM).

  • Jeremy,

    It looks like you have some solutions already.  Are you looking for other ways to align your variables?

    -Tommy

  • Tommy

    The two methods to double word align variables are specified for the variable definitions as follow:

    float val[100] or float *val;

     

    But i am confused with the double word alignment when float vector variables are within a struct likes:

    typedef struct tag_OBJ

    {

         float val[100];

    }OBJ;

    If i have a variable with the type of struct OBJ,  How can i make the var.val double word alignment?

  • The portable way to do this, if the target you using has a native type which requires double-word alignment, is to use a union:

    typedef struct tag_OBJ
    {
       union
       {
          double unused;
          float val[100];
       } u;
    } OBJ;

    You can also use DATA_ALIGN on variables of type "OBJ"; the C standard guarantees that the address of the first member of a struct is the same as the address of the struct itself, so val will be correctly aligned to the alignment specified by the DATA_ALIGN pragma.  Alignment of struct members other than the first element can be predicted based on size and alignment.