An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated.
That concept is a bit unclear. For instance:
struct B { long double d; };
struct D : virtual B { char c; }
When D is the type of a complete object, it will have a subobject of type B, so it must be aligned appropriately for a
long double
.
What does it mean? sizeof(long double)
is the number of bytes between what in that case??
Most CPU's have "preferences" about where data can be stored. When reading or writing to a memory address, the operation may be slower (or completely illegal) if the address doesn't match the data size you try to write. For example, it is common to require that 4-byte integers be allocated starting on an address that is divisible by 4.
That is, an int
stored on address 7
is either less efficient, or completely illegal, depending on your CPU. But if it is stored at address 8
, the CPU is happy.
That is what alignment expresses: for any object of type T
what must its address be divisible by, in order to satisfy the CPU's requirements?"
In C++, the alignment for an object is left implementation-defined (because, as said above, it depends on the CPU architecture). C++ merely says that every object has an alignment, and describes how to determine the alignment of compound objects.
Being "aligned for a long double
" simply means that the object must be allocated so that its first byte is placed in an address that is valid for a long double
. If the CPU architecture specifies the alignment of a long double
to be 10 (for example), then it means that every object with this alignment must be allocated on an address that is divisible by 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With