Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 20th, 2007, 01:15 PM
Anu
Guest
 
Posts: n/a
Default placement new called by vector<> during resizing.

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<AppClassinstance, when the vector is resized, our overloaded
operator new is called!. Here is the comment from the STL vector
implementation :-

template<class _T1,
class _T2inline
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.

  #2  
Old April 20th, 2007, 02:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: placement new called by vector<> during resizing.

Anu wrote:
Quote:
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<AppClassinstance, when the vector is resized, our overloaded
operator new is called!. Here is the comment from the STL vector
implementation :-
>
template<class _T1,
class _T2inline
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.
Quote:
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.
Quote:
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


  #3  
Old April 20th, 2007, 07:45 PM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: placement new called by vector<> during resizing.

On 20 Apr 2007 05:08:25 -0700, Anu wrote:
Quote:
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<AppClassinstance, when the vector is resized, our overloaded
>operator new is called!
[...]
Quote:
>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
  #4  
Old April 20th, 2007, 10:45 PM
James Kanze
Guest
 
Posts: n/a
Default Re: placement new called by vector<> during resizing.

On Apr 20, 3:04 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
Anu wrote:
Quote:
We have a class that has its own overloaded operator new and whose
prototype seems to correspond to the standard placement new :-
Quote:
Quote:
class AppClass
{
public:
operator new (size_t size, void *ctx)
.....
}
Quote:
Quote:
The ctx is important for us. Now we find that if we have a
vector<AppClassinstance, when the vector is resized, our overloaded
operator new is called!. Here is the comment from the STL vector
implementation :-
Quote:
Quote:
template<class _T1,
class _T2inline
void _Construct(_T1 _FARQ *_Ptr, const _T2& _Val)
{ // construct object at _Ptr with value _Val
new ((void _FARQ *)_Ptr) _T1(_Val);
}
Quote:
Quote:
Is this documented somewhere in the standard?
Quote:
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: james.kanze@gmail.com
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



  #5  
Old April 21st, 2007, 11:25 AM
Bo Persson
Guest
 
Posts: n/a
Default Re: placement new called by vector<> during resizing.

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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles