What is the correct usage/syntax for the c++17 alignas() specifier for dynamically allocated arrays of fundamental types?

  alignas, alignment, c++, c++17

This must be a repeat question, but I have not found it after searching for 2 days …

I’m using MSVC with /std:c17 /std:c++17 and trying to get alignas(64) to work with arrays of doubles. The syntax in the code below is the only one I have found that compiles, but it’s not aligning … typically, the array is unaligned about 75% of the time. I know there are many ways to do this with more complicated syntax, but isn’t there a way that "just works" with alignas(), as it would for a structure or class?

double* AR;
int count=0, asize=10;
for (int i = 0; i < 1000; i++)
{
    AR = new double alignas(64)[asize];
    if (((uintptr_t)AR & 63) != 0) count++;
    //if (((uintptr_t)AR % 64) != 0) count++;
    delete[] AR;
}

Source: Windows Questions C++

LEAVE A COMMENT