Array placement-new requires unspecified overhead in the buffer?

前端 未结 6 1198
甜味超标
甜味超标 2020-11-22 15:09

5.3.4 [expr.new] of the C++11 Feb draft gives the example:

new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+

6条回答
  •  死守一世寂寞
    2020-11-22 15:53

    As mentioned by Kerrek SB in comments, this defect was first reported in 2004, and it was resolved in 2012 as:

    The CWG agreed that EWG is the appropriate venue for dealing with this issue.

    Then the defect was reported to EWG in 2013, but closed as NAD (presumably means "Not A Defect") with the comment:

    The problem is in trying to use array new to put an array into pre-existing storage. We don't need to use array new for that; just construct them.

    which presumably means that the suggested workaround is to use a loop with a call to non-array placement new once for each object being constructed.


    A corollary not mentioned elsewhere on the thread is that this code causes undefined behaviour for all T:

    T *ptr = new T[N];
    ::operator delete[](ptr);
    

    Even if we comply with the lifetime rules (i.e. T either has trivial destruction, or the program does not depend on the destructor's side-effects), the problem is that ptr has been adjusted for this unspecified cookie, so it is the wrong value to pass to operator delete[].

提交回复
热议问题