Topic: Defect Report: Alignment requirement for allocation functions.


Author: James Kuyper <kuyper@wizard.net>
Date: 05 Dec 00 04:53:10 GMT
Raw View
There seems to be some agreement that this is a real problem, though a
minor one, but there doesn't seem to be a defect report filed against
it. I guess I'll do the honors. For the sake of making a proper defect
report, I'll be repeating things said by others earlier in this thread.

 [Moderator's note: the defect report below has been
 forwarded to the C++ committee. -moderator(fjh).]

Section 5.3.4 [expr.new] paragraph 10 currently says:
"... For arrays of char and unsigned char, the difference between the
result of the new-expression and the address returned by the allocation
function shall be an integral multiple of the most stringent alignment
requirement (3.9) of any object type whose size is no greater than the
size of the array being created. ..."
(3.9 is [basic.types]).

The intent of this clause seems to be that the memory allocated should
be properly aligned for any object type that can fit in it (and perhaps
that would be the best way to word the requirement). However, that's not
what it actually says. The actual wording makes sense if there's an
implicit assumption that each alignment requirement is an integral
multiple of all smaller alignment requirements. While that assumption is
almost certainly true for virtually every existing implementation of
C++, there's no compelling reason why the standard should require it,
and therefore no good reason why this clause should assume it.

Example: assume that int has an alignment requirement of 3 bytes, and
double has an alignment requirement of 4 bytes, and there are no other
relevant alignment requirements. Then, in order to be free to allocate
either an int or a double in allocated memory, any allocation of 4 or
more bytes must be aligned to a multiple of 12 bytes. However, as
Section 5.3.4 is currently written, it only requires that the allocation
be aligned to a multiple of 4 bytes.

Suggested improvement:
"... For arrays of char and unsigned char, the difference between the
result of the new-expression and the address returned by the allocation
function shall be an integer multiple of the alignment requirements
(3.9) of every object type whose size is no greater than the size of the
array being created. ..."

Section 5.2.10 [expr.reinterpret.cast] paragraph 7 also seems to be
making the same assumption:
"... converting an rvalue of type "pointer to T1" to the type "pointer
to T2" ... where the alignment requirements of T2 are no stricter than
those of T1 ... and back to its original type yields the original
pointer value ..."

The assumption can be removed by rewriting it as follows (note the
reversal of T2 and T1):
"... converting an rvalue of type "pointer to T1" to the type "pointer
to T2" ... where the alignment requirements of T1 are an integer
multiple of those of T2 ... and back to its original type yields the
original pointer value ..."

The same change would also be needed in paragraph 9.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]