How does vector construct/destruct objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lasse Skyum

    How does vector construct/destruct objects?

    I'm currently learning STL and I hate not knowing what is gooing on "inside"
    STL... not because I really _need_ to know it to develop my game project,
    but that's just my nature... like most of you at this grounp I suspect.

    So the question is:

    How does a std::vector construct and destruct the elements in it? I know it
    has something to do with an allocator...

    Suppose I wrote:
    CMyClass *pDynArray = (CMyClass*)mall oc(sizeof(CMyCl ass)*100);

    Would it then be posible to construct/destruct the elements one by one in
    that array?


    I know that this constructs 100 elements at once:
    CMyClass *pArray = new CMyClass[100];

    .... but acording to my tests std::vector doesn't seem to use that aproach.


    --
    Lasse


  • Rolf Magnus

    #2
    Re: How does vector construct/destruct objects?

    "Lasse Skyum" <lskyum(AT)mail .dk> wrote:
    [color=blue]
    > I'm currently learning STL and I hate not knowing what is gooing on
    > "inside" STL... not because I really _need_ to know it to develop my
    > game project, but that's just my nature... like most of you at this
    > grounp I suspect.
    >
    > So the question is:
    >
    > How does a std::vector construct and destruct the elements in it? I
    > know it has something to do with an allocator...[/color]

    Right. It uses the specified (or default) allocator to do it. And the
    default one has to use placement new and pseudo destructor calls.
    [color=blue]
    > Suppose I wrote:
    > CMyClass *pDynArray = (CMyClass*)mall oc(sizeof(CMyCl ass)*100);
    >
    > Would it then be posible to construct/destruct the elements one by one
    > in that array?[/color]

    Yes. You can do:

    #include <new>

    // ...

    for (int i = 0; i < 100; ++i)
    {
    new(pDynArray + i) CMyClass();
    }

    And later for destroying them:

    for (int i = 0; i < 100; ++i)
    {
    pDynArray[i].~CMyClass();
    }

    Using an allocator, it looks like this:

    std::allocator< CMyClass> a;

    CMyClass* pDynArray = a.allocate(100) ;
    CMyClass original;
    for (int i = 0; i < 100; ++i)
    a.construct(pDy nArray + i, original);

    This will copy construct the array objects from the original. And for
    deletion:

    for (int i = 0; i < 100; ++i)
    a.destroy(pDynA rray + i);
    a.deallocate(pD ynArray, 100);

    [color=blue]
    > I know that this constructs 100 elements at once:
    > CMyClass *pArray = new CMyClass[100];
    >
    > ... but acording to my tests std::vector doesn't seem to use that
    > aproach.[/color]

    Comment

    • Lasse Skyum

      #3
      Re: How does vector construct/destruct objects?

      Thanks a lot! I'm getting somewhere now :-)

      --
      Lasse
      [color=blue]
      > Right. It uses the specified (or default) allocator to do it. And the
      > default one has to use placement new and pseudo destructor calls.
      >[color=green]
      > > Suppose I wrote:
      > > CMyClass *pDynArray = (CMyClass*)mall oc(sizeof(CMyCl ass)*100);
      > >
      > > Would it then be posible to construct/destruct the elements one by one
      > > in that array?[/color]
      >
      > Yes. You can do:
      >
      > #include <new>
      >
      > // ...
      >
      > for (int i = 0; i < 100; ++i)
      > {
      > new(pDynArray + i) CMyClass();
      > }
      >
      > And later for destroying them:
      >
      > for (int i = 0; i < 100; ++i)
      > {
      > pDynArray[i].~CMyClass();
      > }
      >
      > Using an allocator, it looks like this:
      >
      > std::allocator< CMyClass> a;
      >
      > CMyClass* pDynArray = a.allocate(100) ;
      > CMyClass original;
      > for (int i = 0; i < 100; ++i)
      > a.construct(pDy nArray + i, original);
      >
      > This will copy construct the array objects from the original. And for
      > deletion:
      >
      > for (int i = 0; i < 100; ++i)
      > a.destroy(pDynA rray + i);
      > a.deallocate(pD ynArray, 100);
      >
      >[color=green]
      > > I know that this constructs 100 elements at once:
      > > CMyClass *pArray = new CMyClass[100];
      > >
      > > ... but acording to my tests std::vector doesn't seem to use that
      > > aproach.[/color]
      >[/color]


      Comment

      • tom_usenet

        #4
        Re: How does vector construct/destruct objects?

        On Tue, 28 Oct 2003 10:12:00 -0000, "Lasse Skyum" <lskyum(AT)mail .dk>
        wrote:
        [color=blue]
        >I'm currently learning STL and I hate not knowing what is gooing on "inside"
        >STL... not because I really _need_ to know it to develop my game project,
        >but that's just my nature... like most of you at this grounp I suspect.
        >
        >So the question is:
        >
        >How does a std::vector construct and destruct the elements in it? I know it
        >has something to do with an allocator...
        >
        >Suppose I wrote:
        >CMyClass *pDynArray = (CMyClass*)mall oc(sizeof(CMyCl ass)*100);
        >
        >Would it then be posible to construct/destruct the elements one by one in
        >that array?[/color]

        Yes:

        CMyClass initialValue(wh atever);
        for (int i = 0; i < 100; ++i)
        {
        //placement new just constructs at the location passed.
        //Here we are copying an initialValue, just as vector does.
        new(pDynArray + i) CMyClass(initia lValue);
        }

        and destruction (order reversed for fun):

        for (int i = 99; i >= 0; --i)
        {
        //direct destructor calls:
        pDynArray[i].~CMyClass();
        }

        //and of course (unless you want to reuse the memory)
        free(pDynArray) ;

        std::allocator uses operator new and operator delete rather than
        malloc and free.

        That's essentially what goes on inside std::vector, although it
        sometimes delegates to the functions allocator::cons truct (which does
        placement new with a copy as above) and allocator::dest roy (which just
        calls the destructor as above). There is also the algorithm
        uninitialized_f ill, which essentially performs the loop above on an
        iterator range.

        Tom

        Comment

        • Lasse Skyum

          #5
          Re: How does vector construct/destruct objects?

          Thanks Tom,

          Just one thing left I don't understand then (about this subject, that is)
          .... what happens then, when you use the basic types int,float,doubl e,...
          they don't have constructors/destructors(rig ht?) so why don't vector<int>
          make compile errors?.

          --
          Lasse


          Comment

          • Rolf Magnus

            #6
            Re: How does vector construct/destruct objects?

            "Lasse Skyum" <lskyum(AT)mail .dk> wrote:
            [color=blue]
            > Thanks Tom,
            >
            > Just one thing left I don't understand then (about this subject, that
            > is) ... what happens then, when you use the basic types
            > int,float,doubl e,... they don't have constructors/destructors(rig ht?)
            > so why don't vector<int> make compile errors?.[/color]

            The compiler knows what to do to initialize the memory, since you gave
            it the type. If the type is a class type, it means calling the
            constructor, and for builtin types, it means just writing a value to
            it. After all, you can write:

            MyClass a(something);
            int i(3);

            even though int doesn't have a constructor. It just will initialize i
            with the value 3. Doing that with dynamically allocated memory isn't
            different:

            MyClass* a = new MyClass(soemthi ng);
            int* i = new int(3);

            well, and with placement new, it's is of course also the same.

            Comment

            • Lasse Skyum

              #7
              Re: How does vector construct/destruct objects?

              Damn I learning stuff today, thanks again!

              I've been programming C++ for (say) 5 years now always using my custom
              containers and making quite advanced 3D games... seems like I don't even
              know the language I've been using! :-o

              --
              Lasse
              [color=blue]
              > The compiler knows what to do to initialize the memory, since you gave
              > it the type. If the type is a class type, it means calling the
              > constructor, and for builtin types, it means just writing a value to
              > it. After all, you can write:
              >
              > MyClass a(something);
              > int i(3);
              >
              > even though int doesn't have a constructor. It just will initialize i
              > with the value 3. Doing that with dynamically allocated memory isn't
              > different:
              >
              > MyClass* a = new MyClass(soemthi ng);
              > int* i = new int(3);
              >
              > well, and with placement new, it's is of course also the same.
              >[/color]


              Comment

              Working...