A
Alf P. Steinbach /Usenet
* Ian Collins, on 20.08.2010 22:59:
I'd miss it too, since alignment requirement /is/ a function of size.
That goes down to the hardware level, it's not a matter of formalism.
Regarding the formalism, specific alignment for a given object can be specified,
by language extensions or in C++0x within the standard language; such a specific
alignment must conform to the alignment requirement for the type; the alignment
requirement for the type in turn must conform to the alignment requirement for
the type's size, which is what matter to the HW.
In theory yes the size is irrelevant to the new operator's guarantee, since it
guarantees to return a pointer suitably aligned for any item.
However, in practice there can be hardware support for items of larger size than
standard C++ supports, then with special alignment requirements.
E.g., for SIMD instructions there is a 16-byte alignment requirement, and most
likely operator new will use an 8-byte alignment (at best), not sufficient.
Cheers,
- Alf (picking nits)
OK, let's see if I got this straight. Since the size of those two
structs is the same, they will both fit correctly aligned in a space
allocated via "new char[sizeof(a)]" (or via "new char[sizeof(b)]", for
that matter).
I think you are still missing the point that size and alignment are
orthogonal.
I'd miss it too, since alignment requirement /is/ a function of size.
That goes down to the hardware level, it's not a matter of formalism.
Regarding the formalism, specific alignment for a given object can be specified,
by language extensions or in C++0x within the standard language; such a specific
alignment must conform to the alignment requirement for the type; the alignment
requirement for the type in turn must conform to the alignment requirement for
the type's size, which is what matter to the HW.
But "new short[sizeof(double) / sizeof(short)]", alone and by itself,
has to satisfy the alignment requirements of "short", regardless of the
size of the array itself.
So in some sense, both the type and the size of that type do play an
important role in the alignment of the address returned by dynamic
allocators - although that role is implementation defined within the
limits mandated by the standard.
No, the size is still irrelevant to the guarantee.
In theory yes the size is irrelevant to the new operator's guarantee, since it
guarantees to return a pointer suitably aligned for any item.
However, in practice there can be hardware support for items of larger size than
standard C++ supports, then with special alignment requirements.
E.g., for SIMD instructions there is a 16-byte alignment requirement, and most
likely operator new will use an 8-byte alignment (at best), not sufficient.
Whether you are
allocating a double or an array of sizeof(double) char or a single char,
the alignment of the pointer will be the same. The pointer returned will
always be appropriately aligned for objects of any type.
Cheers,
- Alf (picking nits)