I have a vector of Foo*. What I put inside is always Foo*, there is no polymorphism.
I use a vector of pointers because I tried a list and it kept crashing. I need to be able to use the pointers in other places so a regular vector of Foo would not work.
ex: b.add(&myRegularVec[6]); //when I add more this pointer wont be valid.
The problem is that I would need the reserve capability of vector. Right now, I have a function like this:
void addRange(int quantity)
{
for (int i = 0; i < quantity; ++i)
{
Foo* obj = new Foo(i);
m_theVector.push_back(obj);
b.add(obj);
}
}
Unfortunately, this calls new a whole lot, and the profiler says it is the bottleneck.
I can not invalidate pointers ex: to grow I would need to redo new[].
What could I do to avoid so many calls to new and make it faster?
Thanks
You could use a
deque<Foo>instead of avector<Foo*>, since a deque won’t invalidate the pointers as it grows the container.