Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > block allocation

Reply
Thread Tools

block allocation

 
 
Philipp Kraus
Guest
Posts: n/a
 
      09-08-2011
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

 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      09-08-2011
On Sep 8, 3:57*pm, Philipp Kraus <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Asger-P
Guest
Posts: n/a
 
      09-08-2011

Hi Philipp

On the: 08. of september-2011 At: 15:57 Philipp Kraus wrote:

> 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
 
Reply With Quote
 
Philipp Kraus
Guest
Posts: n/a
 
      09-08-2011
Thanks a lot of your answers

Phil

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      09-08-2011
On Sep 8, 6:57*am, Philipp Kraus <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Philipp Kraus
Guest
Posts: n/a
 
      09-08-2011
On 2011-09-08 17:09:01 +0200, Noah Roberts said:

> On Sep 8, 6:57*am, Philipp Kraus <(E-Mail Removed)> wrote:
>> 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(...));


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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2011
On 9/8/2011 11:15 AM, Philipp Kraus wrote:
> On 2011-09-08 17:09:01 +0200, Noah Roberts said:
>
>> On Sep 8, 6:57 am, Philipp Kraus <(E-Mail Removed)> wrote:
>>> 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(...));

>
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Asger-P
Guest
Posts: n/a
 
      09-08-2011

Hi Philipp

On the: 08. of september-2011 At: 17:15 Philipp Kraus wrote:

> 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
 
Reply With Quote
 
Philipp Kraus
Guest
Posts: n/a
 
      09-08-2011
On 2011-09-08 17:46:31 +0200, Asger-P said:

> Hi Philipp
>
> On the: 08. of september-2011 At: 17:15 Philipp Kraus wrote:
>
>> 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 ?


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

 
Reply With Quote
 
Asger-P
Guest
Posts: n/a
 
      09-08-2011

Hi Philipp

On the: 08. of september-2011 At: 17:52 Philipp Kraus wrote:

> 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
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM



Advertisments