block allocation

P

Philipp Kraus

Hello,

I would like to allocate a lot of objects in a block. I have read that
I can use calloc (and free), but I think it is a C call and I would do
this in C++-style. I need around 100 till 100.000 objects in memory and
I would do this like:

std::vector<myclass> objects;
for(size_t i=0; i < maxsize; ++i)
objects.push_back( myclass(...) );

Is this a correct call in C++ or can I do this in a better way? Within
my algorithm I need iterate over the full vector (and call a method on
each object)

Thanks

Phil
 
G

Goran

Hello,

I would like to allocate a lot of objects in a block. I have read that
I can use calloc (and free), but I think it is a C call and I would do
this in C++-style. I need around 100 till 100.000 objects in memory and
I would do this like:

std::vector<myclass> objects;
for(size_t i=0; i < maxsize; ++i)
     objects.push_back( myclass(...) );

Is this a correct call in C++ or can I do this in a better way? Within
my algorithm I need iterate over the full vector (and call a method on
each object)

Thanks

Phil

If you only want to allocate storage, use vector.reserve (reserve does
not "put" anything in the vector, but subsequent calls to push_back
won't allocate memory). If you want to __construct__ many objects, use
vector(count). If you have initial value for all your object that is
not the default, try vector(count, initial_value). Finally, if you
already have a vector, use vector.resize().

Using C facilities in C++ code ranges from PITA (calling malloc
+placement new in lieu of new) to dangerous (e.g. using memcpy on
anything but plain-ol' data).

Goran.
 
A

Asger-P

Hi Philipp

Hello,

I would like to allocate a lot of objects in a block. I have read that I
can use calloc (and free), but I think it is a C call and I would do
this in C++-style. I need around 100 till 100.000 objects in memory and
I would do this like:

std::vector<myclass> objects;

objects.reserve( maxsize ); //this will allocate all memory at once
for(size_t i=0; i < maxsize; ++i)
objects.push_back( myclass(...) );

Is this a correct call in C++ or can I do this in a better way? Within
my algorithm I need iterate over the full vector (and call a method on
each object)

if each object is large I would consider using pointers
that makes things much faster, but then of course You
have to remember to delete the objects Your self.


Best regards
Asger-P
 
N

Noah Roberts

Hello,

I would like to allocate a lot of objects in a block. I have read that
I can use calloc (and free), but I think it is a C call and I would do
this in C++-style. I need around 100 till 100.000 objects in memory and
I would do this like:

std::vector<myclass> objects;
for(size_t i=0; i < maxsize; ++i)
     objects.push_back( myclass(...) );

If "..." is always the same then you can initialize the entire vector
on construction:

std::vector<myclass> objects(maxsize, myclass(...));

Otherwise the reserve call otherwise suggested would almost certainly
speed things up.
 
P

Philipp Kraus

If "..." is always the same then you can initialize the entire vector
on construction:

std::vector<myclass> objects(maxsize, myclass(...));

I can't call my constructor, I need call a method named "clone()". I
create a call like:

std::vector<myclass> vec(p_size);
for(std::size_t i=0; i < p_size; ++i)
vec.psuh_back( myclass.clone() );

can I do it like:

std::vector<myclass> vec(p_size, myclass.clone()) with the same result?

Thanks

Phil
 
V

Victor Bazarov

I can't call my constructor, I need call a method named "clone()". I
create a call like:

std::vector<myclass> vec(p_size);
for(std::size_t i=0; i < p_size; ++i)
vec.psuh_back( myclass.clone() );

can I do it like:

std::vector<myclass> vec(p_size, myclass.clone()) with the same result?

No. 'myclass.clone()' in that case would only be called once, and the
vector is going to be filled by copying that one clone. There is
probably a way to utilize a lambda in combination with std::fill or
std::copy.

V
 
A

Asger-P

Hi Philipp

I can't call my constructor, I need call a method named "clone()". I
create a call like:

std::vector<myclass> vec(p_size);
for(std::size_t i=0; i < p_size; ++i)
vec.psuh_back( myclass.clone() );

Others have much more knowledge about this then I, but I
just want to say that in order to use std::vector efficiently
You need to set up a copy constructor and the operator =
You can then call Your Clone function from those.
If You dont do it then the compiler will do it for You and it
will probably not have the same effect as if Clone was called.

P.s. how does Your clone function look exactly ?

Best regards
Asger-P
 
P

Philipp Kraus

Hi Philipp



Others have much more knowledge about this then I, but I
just want to say that in order to use std::vector efficiently
You need to set up a copy constructor and the operator =
You can then call Your Clone function from those.
If You dont do it then the compiler will do it for You and it
will probably not have the same effect as if Clone was called.

P.s. how does Your clone function look exactly ?

Sorry, I can't use a copy-constructor, because the clone function must
create some special unique data and I need a "default copy-constructor"
also.
So I would like to create both methods.

Phil
 
A

Asger-P

Hi Philipp

On 2011-09-08 17:46:31 +0200, Asger-P said:

Sorry, I can't use a copy-constructor, because the clone function must
create some special unique data and I need a "default copy-constructor"
also.
So I would like to create both methods.

Ok, I'm just saying it because the std::vector often copies its
objects and if they are stored by value the copy constructor will
be used for that, but You seem to know, since You have already
made one.


Best regards
Asger-P
 
P

Philipp Kraus

Hi Philipp



Ok, I'm just saying it because the std::vector often copies its
objects and if they are stored by value the copy constructor will
be used for that, but You seem to know, since You have already
made one.


Thanks for the answer.

Phil
 
J

Jorgen Grahn

On 2011-09-08 17:09:01 +0200, Noah Roberts said: ....

I can't call my constructor, I need call a method named "clone()". I
create a call like:

std::vector<myclass> vec(p_size);
for(std::size_t i=0; i < p_size; ++i)
vec.psuh_back( myclass.clone() );

That's a weird name -- clone *what*? Not the class, obviously.
This function should be renamed, IMHO.

/Jorgen
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top