3

If I have a class Vector which is aligned to 16 bytes. Is it safe to have another struct containing Vector?

struct alignas(16) Vector {
  //...
  //overloaded operators that ensure alignment
  void* operator new(size_t size){ return _aligned_malloc(size, 16); }
  void operator delete(void* ptr) { if (ptr) _aligned_free(ptr); }
  void* operator new[](size_t size) { return _aligned_malloc(size, 16); }
  void operator delete[](void* ptr) { if (ptr) _aligned_free(ptr); }
};

struct someStruct {
  Vector vector;
  //some other data
};

Is the Vectorin someStructproperly aligned? What if I create someStructwith the newoperator?

Philipp Neufeld
  • 1,031
  • 10
  • 22
  • Related: http://stackoverflow.com/questions/15511909/alignas-specifier-and-new-c11 (the answer appears to be that it's not safe, unless you also overload `someStruct::operator`s) – eerorika Jun 12 '16 at 10:48

0 Answers0