Relationship between alignment of class fields and alignment of object instances in C++?

  alignment, c++, class, memory-management, oop

Given the following C++ class:

class X {
public:
    uint8_t a;
    uint32_t b[256] __attribute__ ((aligned(32)));
};

How can the compiler ensure that the storage for b is 32-byte aligned, when an instance of X is presumably word-aligned? Does the aligned attribute only specify alignment relative to the object instance start address? If so, what is the right way to make b 32-byte aligned relative to the address space? Should I simply specify object instance alignment using using class alignof(32) X {...}?

Source: Windows Questions C++

LEAVE A COMMENT