Tuesday, October 17, 2017

C++17: Extensions on over-aligned memory allocation

C++17 allows memory alignment greater than std::max_align_t. Here is an example:

#include <iostream>

class A
{
};

class alignas(8) B
{
};

class alignas(256) C // Over-aligned
{
};

class alignas(1024) D // Over-aligned
{
};

int main()
{
  A a;
  B b;
  C c;
  C d;

  std::cout << sizeof(std::max_align_t) << " ";
  std::cout << &a << " ";
  std::cout << &b << " ";
  std::cout << &c << " ";
  std::cout << &d << " ";
  std::cout << std::endl;
  return 0;
}
// Output: 8 0050FCF7 0050FCE0 0050FB00 0050F900
References:
https://en.wikipedia.org/wiki/C%2B%2B17
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0035r4.html

No comments:

Post a Comment