Question

I want to have a variable for example as of type int (aligned as 8 bytes) and so I also want to have pointer operations on it in 8 bytes. ( target pointer alignment)

I did the following but couldnt make it.

typedef int int_align_double __attribute__ ((aligned(sizeof(double))));
typedef int_align_double* pint_align_double;
pint_align_double pint;

or also

typedef int_align_double* pint_align_double __attribute__ ((aligned(sizeof(double))));

So that

(int)&pint[1]-(int)&pint[0] == 8

but it is equal to 4. Am I missing something? I've also found this discussion : http://gcc.gnu.org/ml/gcc/2010-01/msg00005.html but didn't provide a solution or whether it is a bug or not.

Was it helpful?

Solution

You should use attribute vector_size, which specifies the vector size for the variable in bytes, instead of aligned.

So try this:

typedef int int_align_double __attribute__ ((vector_size(sizeof(double))));

OTHER TIPS

The alignment specifier is going to affect the alignment of the first element of an array, but it won't affect the spacing of elements within the array.

If you need to control the spacing of the elements, use a union.

union spaced_int
{
    int    i;
    double d;
};

Edit: Disregard the following, I didn't see that the pointers were being cast to int before the subtraction.

Also note that subtracting two pointers will always yield the difference in terms of the size of the elements, in your example the answer will always be 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top