block allocation

Discussion in 'C++' started by Philipp Kraus, Sep 8, 2011.

  1. 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
     
    Philipp Kraus, Sep 8, 2011
    #1
    1. Advertising

  2. Philipp Kraus

    Goran Guest

    On Sep 8, 3:57 pm, 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;
    > 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.
     
    Goran, Sep 8, 2011
    #2
    1. Advertising

  3. Philipp Kraus

    Asger-P Guest

    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
     
    Asger-P, Sep 8, 2011
    #3
  4. Thanks a lot of your answers

    Phil
     
    Philipp Kraus, Sep 8, 2011
    #4
  5. Philipp Kraus

    Noah Roberts Guest

    On Sep 8, 6:57 am, 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;
    > 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.
     
    Noah Roberts, Sep 8, 2011
    #5
  6. On 2011-09-08 17:09:01 +0200, Noah Roberts said:

    > On Sep 8, 6:57 am, 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;
    >> 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
     
    Philipp Kraus, Sep 8, 2011
    #6
  7. 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 <> 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
     
    Victor Bazarov, Sep 8, 2011
    #7
  8. Philipp Kraus

    Asger-P Guest

    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
     
    Asger-P, Sep 8, 2011
    #8
  9. 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
     
    Philipp Kraus, Sep 8, 2011
    #9
  10. Philipp Kraus

    Asger-P Guest

    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
     
    Asger-P, Sep 8, 2011
    #10
  11. 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
     
    Philipp Kraus, Sep 8, 2011
    #11
  12. Philipp Kraus

    Jorgen Grahn Guest

    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 .
     
    Jorgen Grahn, Sep 8, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morrell
    Replies:
    1
    Views:
    975
    roy axenov
    Oct 10, 2006
  2. Ken
    Replies:
    24
    Views:
    3,896
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,006
    chris
    Oct 28, 2005
  4. Replies:
    4
    Views:
    1,389
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,207
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page