Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Louis Delacroix Newsgroups: comp.std.c++ Subject: Defect Report: Default Array-placement-new unusable due to unknowable memory requirement Date: Thu, 5 Jan 2012 13:54:41 -0800 (PST) Organization: unknown Lines: 59 Sender: std-cpp-request@vandevoorde.com Approved: stephen.clamage@oracle.com Message-ID: <4F05231E.2050400@googlemail.com> NNTP-Posting-Host: FxR7AhRjxiMggCH8jn4M2XKzbOi2AKlV34WsWReyo3A= Content-Type: text/plain; charset=ISO-8859-1 X-Trace: news.albasani.net KjDj21t6yX7Edr2GZDRldGDoPsxKHwSFQUuzVNt590swPCKaTTSgD5VX7gnxoaFHP4p4qPxXVBVCyztLvlqKsA== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Thu, 5 Jan 2012 21:54:43 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: std-cpp-submit@vandevoorde.com Cancel-Lock: sha1:D3rFCsSsZ5CIl89JtSwso0Nt4D4= X-Original-Date: Thu, 05 Jan 2012 04:12:14 +0000 Xref: x330-a1.tempe.blueboxinc.net comp.std.c++:406 Summary: The following code can never be correct: #include void * addr = ??? T * p = ::new (addr) T[N]; The problem is that it is impossible to know how much memory to allocate for addr at ???. Details: This problem was most recently highlighted by StackOverflow user MooingDuck, and adherent community discussion, in this question: http://stackoverflow.com/q/8720425/596781 The problem stems from two requirements of the standard. The first is that the default placement-new expression for arrays calls the default placement-array-new allocation function: void * operator new[](std::size_t n, void * ptr); Now 18.6.1.3/1 requires that this simply return ptr (i.e. "addr" in our example). But the core problem comes from 5.3.4/12, which says that any conforming implementation is free to turn the "new" expression from the example into a call to: p1 = ::operator new[](sizeof(T) * N + y, addr); Here "y" is allowed to be any non-negative number which may differ for every invocation! Finally, p will be p1 + y, and p1 == addr by the previous clause. Thus construction of the T-objects starts at an *unspecified* and *unknowable* offset further down the allocated memory. Since it is impossible to know the size of the offset y, it is also impossible to allocate the correct amount of memory, and thus to use array-placement-new. Note: This *only* affects the *default* version of the *global* array-placement-new. It is understood that other placement versions may be endowed with additional size parameters that allow for safe checking. The defect and its resolution: The defect is that "y" is allowed to be non-zero for the global default array-placement-new expression. This should be resolved by adding sentence to the standard that guarantees that the default global array-placement-new expression calls ::operator new[](sizeof(T) * N, ptr) without any addition. I look forward to your opinions and feedback! Best wishes, Louis -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]