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 align a class member variable?

Example:

class CTest {

....  

#pragma DATA_ALIGN ( 8 );
    NewStruct_t testStruct;

....

}

 

fails with:

"pragma DATA_ALIGN can only be applied to a file level symbol definition"

Compile details:

"C:/Program Files/Texas Instruments/ccsv4/tools/compiler/c6000/bin/cl6x" -mv67p --symdebug:none -O3

 

  • There is no way to align a class member variable.  BTW, the same thing is true of members of a C struct.  C++ class members and C struct members are really the same under the covers.  Why do you need such an alignment?  What overarching problem are you trying to solve?

    Thanks and regards,

    -George

     

  • Hi George,

    I am trying to optimize a routine which accepts a pointer to an array of floats (which happens to be a class member variable).  I want to take advantage of double word operations using intrinsics, but must guarantee double word alignment for the input float data.

    I guess I could use a 64bit dummy variable.  I understand that "__attribute__ ((aligned (8)))" is not supported, is this true?

    Thanks.

  • The traditional way to increase the alignment of a variable is to place it in a union with something that is naturally more strictly aligned. This is not as direct as an alignment pragma or attribute, but it is a bit more portable.

    struct S {
       union U {
          double unused;
          float data[N];
       };
    };
  • Archaeologist is correct, of course.  In C++ such anonymous unions are part of the language.  In C, you have to enable the GCC extensions with the --gcc switch.  To make the example a bit more complete ...

    class tc {
    public:
       union {                 // anonymous union
         float user;
         double never_used;
       };
       int other_field;
    };

    class tc t;

    void f()
    {
        t.user = 0.0;           // access is the same
        t.other_field = 5;      // as any other member
    }