Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9923 > unrolled thread

block allocation

Started byPhilipp Kraus <philipp.kraus@flashpixx.de>
First post2011-09-08 15:57 +0200
Last post2011-09-08 20:27 +0000
Articles 12 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  block allocation Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-08 15:57 +0200
    Re: block allocation Goran <goran.pusic@gmail.com> - 2011-09-08 07:32 -0700
    Re: block allocation Asger-P <junk@asger-p.dk> - 2011-09-08 16:42 +0200
    Re: block allocation Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-08 16:50 +0200
    Re: block allocation Noah Roberts <roberts.noah@gmail.com> - 2011-09-08 08:09 -0700
      Re: block allocation Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-08 17:15 +0200
        Re: block allocation Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-08 11:27 -0400
        Re: block allocation Asger-P <junk@asger-p.dk> - 2011-09-08 17:46 +0200
          Re: block allocation Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-08 17:52 +0200
            Re: block allocation Asger-P <junk@asger-p.dk> - 2011-09-08 18:35 +0200
              Re: block allocation Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-08 20:04 +0200
        Re: block allocation Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-08 20:27 +0000

#9923 — block allocation

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-09-08 15:57 +0200
Subjectblock allocation
Message-ID<j4ahk9$ca9$1@online.de>
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

[toc] | [next] | [standalone]


#9924

FromGoran <goran.pusic@gmail.com>
Date2011-09-08 07:32 -0700
Message-ID<2cc98a18-040b-4f04-9294-3218804426ab@n11g2000yqh.googlegroups.com>
In reply to#9923
On Sep 8, 3:57 pm, Philipp Kraus <philipp.kr...@flashpixx.de> 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.

[toc] | [prev] | [next] | [standalone]


#9926

FromAsger-P <junk@asger-p.dk>
Date2011-09-08 16:42 +0200
Message-ID<op.v1hu4j2p2juju3@ajwin7>
In reply to#9923
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

[toc] | [prev] | [next] | [standalone]


#9928

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-09-08 16:50 +0200
Message-ID<j4akni$eor$1@online.de>
In reply to#9923
Thanks a lot of your answers

Phil

[toc] | [prev] | [next] | [standalone]


#9929

FromNoah Roberts <roberts.noah@gmail.com>
Date2011-09-08 08:09 -0700
Message-ID<3ebd2cd2-a517-459d-9f5b-c288ef172ef7@l4g2000vbz.googlegroups.com>
In reply to#9923
On Sep 8, 6:57 am, Philipp Kraus <philipp.kr...@flashpixx.de> 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.

[toc] | [prev] | [next] | [standalone]


#9930

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-09-08 17:15 +0200
Message-ID<j4am6n$g57$1@online.de>
In reply to#9929
On 2011-09-08 17:09:01 +0200, Noah Roberts said:

> On Sep 8, 6:57 am, Philipp Kraus <philipp.kr...@flashpixx.de> 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

[toc] | [prev] | [next] | [standalone]


#9931

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-09-08 11:27 -0400
Message-ID<j4amtu$1vm$1@dont-email.me>
In reply to#9930
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 <philipp.kr...@flashpixx.de> 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

[toc] | [prev] | [next] | [standalone]


#9932

FromAsger-P <junk@asger-p.dk>
Date2011-09-08 17:46 +0200
Message-ID<op.v1hx3tqo2juju3@ajwin7>
In reply to#9930
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

[toc] | [prev] | [next] | [standalone]


#9933

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-09-08 17:52 +0200
Message-ID<j4aocl$i0j$1@online.de>
In reply to#9932
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

[toc] | [prev] | [next] | [standalone]


#9936

FromAsger-P <junk@asger-p.dk>
Date2011-09-08 18:35 +0200
Message-ID<op.v1h0cydt2juju3@ajwin7>
In reply to#9933
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

[toc] | [prev] | [next] | [standalone]


#9939

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-09-08 20:04 +0200
Message-ID<j4b032$otj$1@online.de>
In reply to#9936
On 2011-09-08 18:35:12 +0200, Asger-P said:

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


Thanks for the answer.

Phil

[toc] | [prev] | [next] | [standalone]


#9942

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-09-08 20:27 +0000
Message-ID<slrnj6i98n.gtj.grahn+nntp@frailea.sa.invalid>
In reply to#9930
On Thu, 2011-09-08, Philipp Kraus wrote:
> On 2011-09-08 17:09:01 +0200, Noah Roberts said:
...
>> 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() );

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

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web