placement new called by vector<> during resizing.

Discussion in 'C++' started by Anu, Apr 20, 2007.

  1. Anu

    Anu Guest

    Hi,

    We have a class that has its own overloaded operator new and whose
    prototype seems to correspond to the standard placement new :-


    class AppClass
    {
    public:
    operator new (size_t size, void *ctx)

    ......
    }

    The ctx is important for us. Now we find that if we have a
    vector<AppClass> instance, when the vector is resized, our overloaded
    operator new is called!. Here is the comment from the STL vector
    implementation :-

    template<class _T1,
    class _T2> inline
    void _Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
    { // construct object at _Ptr with value _Val
    new ((void _FARQ *)_Ptr) _T1(_Val);
    }

    Is this documented somewhere in the standard? Or should we have
    chosen our operator new's signature more carefully? Can I do some easy
    thing to get around this issue?

    Thanks in advance,
    anu.
    Anu, Apr 20, 2007
    #1
    1. Advertising

  2. Anu wrote:
    > We have a class that has its own overloaded operator new and whose
    > prototype seems to correspond to the standard placement new :-
    >
    >
    > class AppClass
    > {
    > public:
    > operator new (size_t size, void *ctx)
    >
    > .....
    > }
    >
    > The ctx is important for us. Now we find that if we have a
    > vector<AppClass> instance, when the vector is resized, our overloaded
    > operator new is called!. Here is the comment from the STL vector
    > implementation :-
    >
    > template<class _T1,
    > class _T2> inline
    > void _Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
    > { // construct object at _Ptr with value _Val
    > new ((void _FARQ *)_Ptr) _T1(_Val);
    > }
    >
    > Is this documented somewhere in the standard?


    No, it's an implementation detail.

    > Or should we have
    > chosen our operator new's signature more carefully?


    I can't speak to that. You provided no information about the problem
    you were solving by having your overloaded operator new with your
    particular signature.

    > Can I do some easy
    > thing to get around this issue?


    Probably.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 20, 2007
    #2
    1. Advertising

  3. On 20 Apr 2007 05:08:25 -0700, Anu wrote:
    > We have a class that has its own overloaded operator new and whose
    >prototype seems to correspond to the standard placement new :-
    >
    >class AppClass
    >{
    > public:
    > operator new (size_t size, void *ctx)
    >.....
    >}
    > The ctx is important for us. Now we find that if we have a
    >vector<AppClass> instance, when the vector is resized, our overloaded
    >operator new is called!

    [...]
    >Or should we have
    >chosen our operator new's signature more carefully? Can I do some easy
    >thing to get around this issue?


    class AppClass
    {
    //...
    operator new (size_t size, MyPool* ctx);
    };


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Apr 20, 2007
    #3
  4. Anu

    James Kanze Guest

    On Apr 20, 3:04 pm, "Victor Bazarov" <> wrote:
    > Anu wrote:
    > > We have a class that has its own overloaded operator new and whose
    > > prototype seems to correspond to the standard placement new :-


    > > class AppClass
    > > {
    > > public:
    > > operator new (size_t size, void *ctx)
    > > .....
    > > }


    > > The ctx is important for us. Now we find that if we have a
    > > vector<AppClass> instance, when the vector is resized, our overloaded
    > > operator new is called!. Here is the comment from the STL vector
    > > implementation :-


    > > template<class _T1,
    > > class _T2> inline
    > > void _Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
    > > { // construct object at _Ptr with value _Val
    > > new ((void _FARQ *)_Ptr) _T1(_Val);
    > > }


    > > Is this documented somewhere in the standard?


    > No, it's an implementation detail.


    Not really. The standard does require that the implementation
    of vector separate allocation and construction, and the only way
    to call a constructor is by using placement new.

    I would consider this a bug in the library; the statement in
    question should be:
    ::new ((void _FARQ *)_Ptr) _T1(_Val);
    , since this is the only way to guarantee that you get the
    standard placement new. I think a bug report is justified. (On
    the other hand, I'm not really too surprised about the bug.
    It's the sort of thing that's easy to overlook.)

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 20, 2007
    #4
  5. Anu

    Bo Persson Guest

    James Kanze wrote:
    :
    : I would consider this a bug in the library; the statement in
    : question should be:
    : ::new ((void _FARQ *)_Ptr) _T1(_Val);
    : , since this is the only way to guarantee that you get the
    : standard placement new. I think a bug report is justified. (On
    : the other hand, I'm not really too surprised about the bug.
    : It's the sort of thing that's easy to overlook.)

    This is the way it looks like in the current release of the library.

    The OP uses an older version of the compiler.


    Bo Persson
    Bo Persson, Apr 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Dettrick
    Replies:
    2
    Views:
    387
    Sean Dettrick
    Jul 31, 2003
  2. Replies:
    8
    Views:
    1,898
    Csaba
    Feb 18, 2006
  3. jamieson
    Replies:
    1
    Views:
    391
    John Posner
    May 8, 2009
  4. Pil (Trustworthy from Experience)

    Resizing a div by resizing its borders

    Pil (Trustworthy from Experience), Apr 18, 2009, in forum: Javascript
    Replies:
    9
    Views:
    340
    Proper
    Apr 21, 2009
  5. Proper
    Replies:
    0
    Views:
    191
    Proper
    Apr 18, 2009
Loading...

Share This Page