473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

placement new called by vector<> during resizing.

Anu
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.

Apr 20 '07 #1
4 2326
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<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.
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
Apr 20 '07 #2
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<AppClassinstance, 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
Apr 20 '07 #3
On Apr 20, 3:04 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<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.
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: ja*********@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

Apr 20 '07 #4
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
Apr 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marc Schellens | last post by:
my dinkumware docu says, vector<...>::rbegin() returns an iterator which points just BEYOND the end of the controlled sequence. Is that true? so I cannot say: for( riter i=v.rbegin(); i !=...
2
by: john smith | last post by:
Hi, when there is a vector<> of pointers to some objects, does calling resize cause vector to call delete on each object, or is there a memory leak problem? for example: struct base {//some...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
5
by: Numeromancer | last post by:
From the C++-FAQ Lite: http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3 ---------------------------- 34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes. ...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
1
by: iammilind | last post by:
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear() throws compile error with that:...
4
by: iammilind | last post by:
I am not able to understand why the 2nd ~Test() is called with different 'this' but the same string value ?? Also why does it give error, if I want to declare Test::Str as a const string member ?? ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.