Array placement new

Matt Austern austern at isolde.engr.sgi.com
Thu Jan 20 05:33:05 UTC 2000


Sorry about sending this so late.  As usual, I didn't remember to
write it up until Jim prodded me.

			--Matt

WHAT THE STANDARD SAYS. (3.7.3.1, 5.3.4, and 18.4.1.3)

Array placement new has the form "new(ARGS) T[n]".  After finding
the appropriate operation new, it obtains storage with
    void* p = operator new[](n1, ARGS),
where n1 >= n * sizeof(T).  It then constructs n objects of type
T starting at position p1, where p1 = p + delta.  The return
value is p1.

If T is "char" or "unsigned char" we are guaranteed that delta is
a nonnegative multiple of the most stringent alignment constraint
for objects of size less than or equal to n (5.3.4/10).
Otherwise the only restriction is that delta is nonnegative.

Some implementations store the number of elements in the array at
a negative offset from p1.  The standard neither requires nor
forbids it.

There's a predefined array operator new,
    ::operator new[](size_t n1, void* p),
that does nothing but return p.  p must be a pointer to the
beginning of some array of size at least n1.  The standard
doesn't tell users how large an array they need.  Many users
probably assume that it's sufficient for the array to be of size
n * sizeof(T), but there's no basis in the standard for that
assumption.

WHAT THE ABI NEEDS TO SPECIFY

(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?)
    (b) Is ::operator new[](size_t, void*) a special case?
(2) Do we store n at a negative offset from the return value of
    operator new[]? (This affects the answer to question 1.)
    If so, we need to specify precisely what that offset is.
    Note that (3.7.3.1/2) we are guaranteed that the return value
    of ::operator new[] is suitably aligned for objects of any
    type.





More information about the cxx-abi-dev mailing list