Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query the alignment of a specific variable

C++11 introduced the alignas specifier to specify the alignment of a variable, and the alignof operator to query the default alignment of a type. However, I don't see any way to get the alignment of a specific variable. Let's take the following trivial example:

alignas(16) float* array; 

Here is what we can do about it:

  • alignof(float*) returns 8, which is obviously not what we want.
  • alignof(array) returns 16, which is exactly what we want, but that's a compiler extension; alignof as specified by the standard can't be used on a specific variable.
  • alignof(decltype(array)) returns 8, which was quite expected but not what we want.
  • std::alignment_of is implemented in terms of alignof, so it doesn't help much.

I would like a mechanism to confirm that the specific variable array is aligned on a 16 byte boundary. Is there anything in the standard to perform such a query?

like image 543
Morwenn Avatar asked Mar 25 '16 09:03

Morwenn


1 Answers

You can try with something like:

bool is_aligned(const volatile void *p, std::size_t n) {   return reinterpret_cast<std::uintptr_t>(p) % n == 0; }  assert(is_aligned(array, 16)); 

The above assumes a flat address space and that arithmetic on uintptr_t is equivalent to arithmetic on char *.

While these conditions prevail for the majority of modern platforms, neither of which is required by the standard.

It's entirely possible for an implementation to perform any transformation when casting void * to uintptr_t as long the transformation can be reversed when casting back from uintptr_t to void * (see What is uintptr_t data type).

Further details in N4201 (it proposes, among other things, the is_aligned() operation).


EDIT

is volatile necessary here?

It allows something like:

alignas(16) volatile float a;  assert(is_aligned(&a, 16)); 

Without volatile you get the error

no known conversion from 'volatile float *' to 'const void *' for 1st argument

Further references:

  • Why and when is cast to char volatile& needed?
  • Why is a point-to-volatile pointer, like "volatile int * p", useful?
like image 144
manlio Avatar answered Sep 23 '22 07:09

manlio