Topic: placement new syntax


Author: Anupam Kapoor <akapoor@cisco.com>
Date: 2000/11/17
Raw View
James Kuyper wrote:

> Anupam Kapoor wrote:
> >
> > hi all,
> >
> > probably this question has been asked before, but i cannot find any
> > references to it on
> > deja, so here goes :
> >
> > why does the placement new syntax require a size_t param ? on some
> > implementations,
>
> I suspect you've misstated your question. The placement new syntax does
> not require a size_t parameter; any implementation where it is required
> (rather than optional) is non-conforming.

the standard says otherwise, imho. to quote 5.3.4 para 11, the new-placement
operator syntax is used to supply additional arguments to an allocation
function.
if used, overload resolution is performed on a function call created by
assembling
an argument list consisting of the amount of space requested(the first
argument)
and the expressions in the new-placement part of the new-expression(the
second
succeeding arguments). the first of these arguments has type size_t ...

> Any operator new() function
> must have a size_t parameter, but that parameter does not play any part
> in the syntax. The required semantics, not the syntax, involve filling
> it in with the size of the object being allocated.
>
> syntax: new(p) T()
> semantics:
>         T* temp = operator new(sizeof(T),p);
>         call default constructor for *temp
>         return temp;
>
> Note that user code and standard library templates can call operator
> new() explicitly, and might do so in a way such that T::operator new()
> is the overload selected, even though the object being allocated isn't a
> T. Thus, the size_t parameter will not necessarily be sizeof(T), and is
> therefore not redundant:
>
>         X* px = T::operator new(sizeof(X),hint);
>         new (px) X();
>         // use *px.
>         px->~X();
>         T::operator delete(px);
>
> > i have seen the placement new operator just returning the buffer that
> > was passed to it.
>
> That's perfectly normal. That's all that placement new needs to do. The
> syntax is created to allow additional arguments if you want to provide
> an overload that uses them. It also triggers the call to the
> constructor, which cannot be called explicitly, since the constructor
> doesn't have a name.
>
> > why aren't there any interfaces to the underlying os, requesting size_t
> > amount of memory
> > from the heap ?
>
> There are: std::malloc(), std::calloc(), and std::realloc().

yes i know that. i was under the impression that once you pass the size_t
param with the location of the buffer where you want the object to be
"placed", the implementation must request the os to get at least size_t
amount of memory. but the user is responsible for doing that, plus
proper alignment etc. right ?

thanks

anupam

>
> ---
> [ 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://www.research.att.com/~austern/csc/faq.html                ]


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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              ]





Author: Anupam Kapoor <akapoor@cisco.com>
Date: 14 Nov 00 04:22:11 GMT
Raw View
hi all,

probably this question has been asked before, but i cannot find any
references to it on
deja, so here goes :

why does the placement new syntax require a size_t param ? on some
implementations,
i have seen the placement new operator just returning the buffer that
was passed to it.
why aren't there any interfaces to the underlying os, requesting size_t
amount of memory
from the heap ?

maybe it's just dumb, but i _have_ to ask this.

tia.

anupam


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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              ]




Author: James Kanze <James.Kanze@dresdner-bank.de>
Date: 2000/11/14
Raw View
Anupam Kapoor wrote:

> probably this question has been asked before, but i cannot find any
> references to it on deja, so here goes :

> why does the placement new syntax require a size_t param ?

So that the implementation of the operator new function can know the
requested size.

> on some
> implementations, i have seen the placement new operator just
> returning the buffer that was passed to it.

This is the required behavior of the placement new which takes void*,
according to the standard.

Obviously, in this case, there is no need of the size_t parameter.
But the compiler cannot, in principle, know this, since it will be
used for most placement news.

> why aren't there any
> interfaces to the underlying os, requesting size_t amount of memory
> from the heap ?

Because the semantics of placement new with a void* are specified to
use the buffer given it.  A placement new with a void* is really just
a trick to call the constructor on a raw block of memory.  (You can
consider it built-in obfuscation, if you like.)

--
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 2000/11/14
Raw View
Anupam Kapoor wrote:
>
> hi all,
>
> probably this question has been asked before, but i cannot find any
> references to it on
> deja, so here goes :
>
> why does the placement new syntax require a size_t param ? on some
> implementations,

I suspect you've misstated your question. The placement new syntax does
not require a size_t parameter; any implementation where it is required
(rather than optional) is non-conforming. Any operator new() function
must have a size_t parameter, but that parameter does not play any part
in the syntax. The required semantics, not the syntax, involve filling
it in with the size of the object being allocated.

syntax: new(p) T()
semantics:
 T* temp = operator new(sizeof(T),p);
 call default constructor for *temp
 return temp;

Note that user code and standard library templates can call operator
new() explicitly, and might do so in a way such that T::operator new()
is the overload selected, even though the object being allocated isn't a
T. Thus, the size_t parameter will not necessarily be sizeof(T), and is
therefore not redundant:

 X* px = T::operator new(sizeof(X),hint);
 new (px) X();
 // use *px.
 px->~X();
 T::operator delete(px);

> i have seen the placement new operator just returning the buffer that
> was passed to it.

That's perfectly normal. That's all that placement new needs to do. The
syntax is created to allow additional arguments if you want to provide
an overload that uses them. It also triggers the call to the
constructor, which cannot be called explicitly, since the constructor
doesn't have a name.

> why aren't there any interfaces to the underlying os, requesting size_t
> amount of memory
> from the heap ?

There are: std::malloc(), std::calloc(), and std::realloc().
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]






Author: Herb Sutter <hsutter@peerdirect.com>
Date: 2000/11/18
Raw View
James Kanze <James.Kanze@dresdner-bank.de> writes:
>Anupam Kapoor wrote:
>> on some
>> implementations, i have seen the placement new operator just
>> returning the buffer that was passed to it.
>
>This is the required behavior of the placement new which takes void*,
>according to the standard.
>
>Obviously, in this case, there is no need of the size_t parameter.
>But the compiler cannot, in principle, know this, since it will be
>used for most placement news.

It's also worth pointing out again that "placement new" is an
unfortunately misleading term. The term is used to refer to any new
taking additional parameters. It just so happens that the first (and
only standardized) use of this facility happens to be a "place it
_there_ and don't actually bother allocating anything" operation which
can naturally be described as "placement" -- most other uses of this
facility wouldn't as naturally be described as "placement."

Herb

---
Herb Sutter (mailto:hsutter@peerdirect.com)

CTO, PeerDirect Inc. (http://www.peerdirect.com)
Contributing Editor, C/C++ Users Journal (http://www.cuj.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://www.research.att.com/~austern/csc/faq.html                ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/11/18
Raw View
Anupam Kapoor wrote:
>
> James Kuyper wrote:
...
> > I suspect you've misstated your question. The placement new syntax does
> > not require a size_t parameter; any implementation where it is required
> > (rather than optional) is non-conforming.
>
> the standard says otherwise, imho. to quote 5.3.4 para 11, the new-placement
> operator syntax is used to supply additional arguments to an allocation
> function.
> if used, overload resolution is performed on a function call created by
> assembling
> an argument list consisting of the amount of space requested(the first
> argument)
> and the expressions in the new-placement part of the new-expression(the
> second
> succeeding arguments). the first of these arguments has type size_t ...

Agreed - operator new(), in all forms, takes a size_t argument. However,
that argument is completely invisible in the syntax of placement new (or
any other new-expression, for that matter);
It appears only in the syntax of operator new(). You can, of course,
call operator new() explicitly, in which case the argument is visible.

...
> > > why aren't there any interfaces to the underlying os, requesting size_t
> > > amount of memory
> > > from the heap ?
> >
> > There are: std::malloc(), std::calloc(), and std::realloc().
>
> yes i know that. i was under the impression that once you pass the size_t
> param with the location of the buffer where you want the object to be
> "placed", the implementation must request the os to get at least size_t
> amount of memory.  but the user is responsible for doing that, plus
> proper alignment etc. right ?

I'm thoroughly confused by that comment. If you give placement new the
location of the buffer where you want the object to be placed, it places
it there. It doesn't go out and request a certain amount of memory -
you've already given it the memory, it doesn't need any more. You're
correct, the user is responsible for the memory; there's nothing that
the implementation needs to do other than calling the constructor and
then returning the pointer argument. In fact, for std::operator
new(size_t, void*), that's the only thing it's allowed by the standard
to do. However, other placement new functions can have an arbitrarily
long list of additional arguments, and do with those arguments whatever
they please.

What connection does this have to the heap? If you allocate memory from
the heap, you can use placement new to construct objects in it. Since
the placement functions use memory that's already allocated, I'm
confused about the connection you're making between malloc() and
placement new.
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]






Author: Matthew Austern <austern@research.att.com>
Date: 21 Nov 00 03:04:23 GMT
Raw View
Herb Sutter <hsutter@peerdirect.com> writes:

> >Obviously, in this case, there is no need of the size_t parameter.
> >But the compiler cannot, in principle, know this, since it will be
> >used for most placement news.
>
> It's also worth pointing out again that "placement new" is an
> unfortunately misleading term. The term is used to refer to any new
> taking additional parameters. It just so happens that the first (and
> only standardized) use of this facility happens to be a "place it
> _there_ and don't actually bother allocating anything" operation which
> can naturally be described as "placement" -- most other uses of this
> facility wouldn't as naturally be described as "placement."

Almost true.  Placement was indeed the first use for "placement new",
but it's not the only standardized use.  The other use in the standard
library is nothrow new.

  {Yup, forgot about that one! Thanks. Of course that makes nothrow new
  the best example of why "placement" doesn't always really mean "place
  it in this predetermined spot." -mod/hps}

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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              ]