lchian@yahoo.com (luigi) wrote in message news:<27fb1738.0311241829.3c2e41cc@posting.google. com>...
Please don't top-post. Put you reply below the previous message. It
makes the thread easier to follow. Thanks.
<rearranged>
[color=blue]
>
deane_gavin@hotmail.com (Gavin Deane) wrote in message news:<6d8002d0.0311240416.2e89cf5b@posting.google. com>...[color=green]
> >
lchian@yahoo.com (luigi) wrote in message news:<27fb1738.0311232347.47d46e0b@posting.google. com>...[color=darkred]
> > > Hi,
> > >
> > > I am trying to speed up the perfomance of stl vector by
> > > allocating/deallocating blocks of memory manually.[/color]
> >
> > That won't stop the vector allocating and deallocating memory. Every
> > time you call push_back your vector might allocate another chunck of
> > memory. If you know how big you want your vector to be, just make it
> > that big when you create it. For example
> >
> > std::vector<double> vec(5);
> >
> > vec is 5 elements long and each element contains 0.0 (the
> > default-initialisation value for double).
> >
> > std::vector<double> vec2(5, 42.42);
> >
> > vec is 5 elements long and each element contains 42.42.
> >
> > This does everything I think you were trying to do. Also look at the
> > vector member functions reserve, capacity and resize.
> >
> > <snip>
> >
> > hth
> > GJD[/color]
> Hi,
>
> Thanks again for the good comments.
>
> These are just made-up test cases. Please conside the case when
> elements are pointers to huge structures, with their own dynamic
> allocation. The vector size could also vary widely. Would your
> comments on the performance still hold?[/color]
I hope I've understood you question and what I've written is relevant
to you.
There is nothing to be gained by trying to take low-level control of
the vector's internal storage. Use of the appropriate vector
constructors, as well as member functions like resize, reserve and
capacity will give you all the control you need. Very simple example
(look up the other vector member functions for further reference):
#include <vector>
int main()
{
std::vector<int> vi;
vi.reserve(10); // vi can hold ten ints before needing to
// allocate more memory.
for (int i = 0; i < 5; ++i)
{
vi.push_back(i); // 5 push back calls but no further dynamic
// allocation inside the vector is necessary.
}
return 0;
}
Now, you have a huge structure, instances of which will be created
dynamically, something like
struct my_struct { /* huge */ };
my_struct* s = new my_struct;
// later ...
delete s;
This is a completely separate issue from the memory management going
on inside the vector. If you have a std::vector<my_struct*>, it is
that vector's responsibility to manage dynamic storage for enough
pointers-to-my_struct. And you have some control over that through the
vector class interface.
It is your responsibility to remember to delete any my_struct objects
you allocated with new. Another simple example:
#include <vector>
struct my_struct { /* huge */ };
int main()
{
std::vector<my_struct*> vec;
my_struct* s0 = new my_struct;
my_struct* s1 = new my_struct;
// vec is responsible for managing the dynamic storage for
// enough pointers-to-my_struct here.
vec.push_back(s0);
vec.push_back(s1);
// You have to do this bit.
delete vec[0]; // or delete s0;
delete vec[1]; // or delete s1;
return 0;
// At the end of the function, vec is destroyed. During destruction
// it cleans up its dynamic pointers-to-my_struct storage.
}
The vector can't help you with deleting the actual my_struct objects
though, because it has no way of knowing whether the contained
pointers were created in a new expression or not. For example, you
could store pointers to dynamically allocated objects in the same
vector as pointers to local objects. I would not recommend this at
all, it's just to illustrate why the vector cannot be responsible for
deleting objects it holds pointers to.
#include <vector>
struct my_struct { /* huge */ };
int main()
{
std::vector<my_struct*> vec;
my_struct* s0 = new my_struct;
my_struct local_object;
my_struct* s1 = &local_object;
// vec is responsible for managing the dynamic storage for
// enough pointers-to-my_struct here.
vec.push_back(s0);
vec.push_back(s1);
// The vector can not do this bit because it can not know
// which pointers point to objects allocated with new.
// You have to do this.
delete vec[0];
// You must not do this.
//delete vec[1];
return 0;
// At the end of the function, vec is destroyed. During destruction
// it cleans up its dynamic pointers-to-my_struct storage.
}
--
hth
GJD