Array placement new
Martin von Loewis
loewis at informatik.hu-berlin.de
Thu Jan 20 11:18:42 UTC 2000
> (2) Do we store n at a negative offset from the return value of
> operator new[]?
I believe the answer to this question must be 'no'. operator new[] is
a replaceable signature (18.4.1). If there is a user-provided
definition of operator new[], there is no guarantee that anything at
negative offsets is accessible.
I just noticed there is a terminology problem. I'd call the outcome of
::operator new[](size)
the return value of operator new[], and the outcome of
new T[size]
the return value of the new-expression.
> Some implementations store the number of elements in the array at
> a negative offset from p1. The standard neither requires nor
> forbids it.
The standard requires that, if T is a class type, the appropriate
number of constructors are invoked when the array is
deleted. Therefore, delete[] has to have knowledge about the number of
elements in the array.
I believe g++ uses the following strategy: If T has a destructor, the
array size is stored within the array. If T is primitive, or POD, only
the total size of all objects is requested.
> (1) Given n, T, sizeof(T), and alignof(T), what are n1 and delta?
> (a) Are T=char and T=unsigned char special cases? (Or,
> perhaps, is sizeof(T)=1 a special case?)
No. Types without destructor are a special case. That gives the
special behaviour for new char[], as well.
<proposal>
If T has an explicitly declared destructor (either directly or in a
base), then
delta = max(sizeof(size_t),alignof(T))
n1 = n*sizeof(T) + delta
If result of the operator-new-call is p, then the result of the
new-expression is
p1 = p+delta
If T has no explicit destructor, then delta=0, and a delete-expression
only invokes operator delete[] with the given address.
</proposal>
I believe this differs from the current g++ solution, which always
uses delta = biggest_alignment for destructable types.
> (b) Is ::operator new[](size_t, void*) a special case?
No. The standard clearly says that it must return its argument. So if
people allocate only n*sizeof(T) memory in a placement-new for an
array of destructable elements, then it will overwrite some random
memory.
Regards,
Martin
More information about the cxx-abi-dev
mailing list