From -7814802472500117440
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,839fa42e97baea86
X-Google-Attributes: gidf78e5,public
From: "Larry Brasfield" <larry_br@sea_net.com>
Subject: Re: Placement new[] Compiler Depenent?
Date: 1998/11/20
Message-ID: <XDj52.5415$0h.18619883@news.rdc1.wa.home.com>#1/1
X-Deja-AN: 413976755
X-NNTP-Posting-Host: 24.0.239.100
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <eDcSHHgdulIC-pn2-e3Hcgbrpl1Vl@eir.stiscan.com> <3653144D.8A863861@acm.org> <lgH42.5353$0h.17515725@news.rdc1.wa.home.com> <365395A5.CABB481B@acm.org>
X-UID: 0000000001
X-Status: $$$T
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0
Organization: Serendipitous Endeavors
NNTP-Posting-Date: Fri, 20 Nov 1998 11:43:51 PDT
Newsgroups: comp.std.c++
Originator: clamage@taumet


Pete Becker wrote in message <365395A5.CABB481B@acm.org>...
>Larry Brasfield wrote:
>>
>> Pete Becker wrote in message <3653144D.8A863861@acm.org>...
>> >Ken Walter wrote:
>> >>
>> >> Placement new[] on my compiler requires particular implementation
>> >> knowledge.
>> >
>> >The standard prohibits this. operator new[](size_t) is supposed to be
>> >called with an argument that gives the total number of bytes required by
>> >the implementation.

Please note that the new overload referred to above is
NOT "placement new".  It is ordinary, unadorned new.

>> I believe you have missed Mr. Walter's point.  "Placement new[]"
>> is used when allocation is handled outside the automatice C++
>> mechanisms, ("Over the covers", we might say.), typically using
>> the overload, "void * new(size_t, void *)".  To permit that allocation
>> to be performed before the placement new statement is executed,
>> some user-written code needs to know how much memory to set
>> aside, (address of which is then passed as the void * argument.)

An important point here, (perhaps not yet made sufficiently
explicit), is that the user's allocation *must* be performed
before using placement new to make efficient use of the
placement new mechanism.  It might be possible to do an
allocation, try the placement new construction statement,
then deallocate, reallocate and retry upon discovering
that the guessed overhead was insufficient, but that is a
lot of bother for what should be a simple operation.

>> The problem to which Mr. Walters alludes is that he cannot tell
>> how much extra memory to allocate beyond what the objects
>> themselves will occupy without either making assumptions about
>> the implementation or doing experiments for the implementations
>> to be targeted.  Either choice violates the notion that it is possible
>> to write portable C++ code if only C++ features are relied upon.
>
>He doesn't need to know. The runtime system asks for the memory that it
>needs, including any "extra" storage space. Be sceptical when testing
>this: if the class that you're allocating doesn't have a destructor then
>no extra space is needed.

But Pete, when placement new is invoked, the memory
has already been allocated.  If more is asked for than
what was allocated by the user, then there is no way
for the placement new function to know what to do.
(And of course, the standard placement new merely
returns the void * it was given, implying that this was
not intended to be an exercise of much intelligence.)

I suspect, from your mention of operator new[](size_t)
above when operator new[](size_t, void *) is the topic
of this discussion, that you have no real disagreement
with Mr. Walter's or my perception of the problem.

--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]




