Vector and push_back() behavior

Mikko-Pentti Einari Eronen

I'm studying to understand class constructor and destructor. I have written a small console code to add one class instance in to a vector. All is nice and dandy, but what I fail to understand is, that adding one Object in to the vector triggers destructor twice. Why does this happen?

If I don't add any object, the vector doesn't trigger constructor or destructor on its own, so why it happens twice?

Can anyone explain why this happens?

#include <cstdio>
#include <vector>
class Test
{
    private:
        int value;

    public:
        Test()
        {
            printf("\nClass constructor triggered.");
        };
        ~Test()
        {
            printf("\nClass desctructor triggered.");
        }
};

int main()
{
    std::vector<Test> container;

    container.push_back( Test() );
    return 0;
}

UPDATE: I added some more information to the class so that I get more specific output, however now I noticed that with each addition to the vector the move-construction and destructor calls increase. Are the amount of these calls tied to the amount of objects within the vector or what is happening? Am I having a leak? Sorry if too stupid questions. Below is the added code:

#include <cstdio>
#include <vector>

class Test
{
    private:
        int value;

    public:
        // Constructor
        Test(int v=0)
        {
            value = v;
            printf("\n\n%i", value);
            printf("\nClass constructor triggered.");
        };

        // Copy-move constructor
        Test(Test&&)
        {
            printf("\nClass move-constructor triggered.");
        };

        // Destructor
        ~Test() 
        {
            value = 0;
            printf("\nClass desctructor triggered.");
        }
};

int main()
{
    std::vector<Test> container;

    container.push_back( Test(1) );
    container.push_back( Test(2) );
    container.push_back( Test(3) );
    container.push_back( Test(4) );

    printf("\n\nPushback complete!");
    return 0;
}
Bartek Banachewicz

Because you don't print every constructor invocation, you're missing out on move-constructor call. Your class, apart from the default constructor you've provided, has also implicitly generated move and copy constructors.

The vector stores a value, and that value has to be initialized in some way. Typically, this happens either via a move c-tor or copy c-tor, altough an object might also be created directly inside of the vector using e.g. emplace_back.

Try adding this:

Test(Test&&)
{
    printf("\nClass move constructor triggered.");
};

to your class, it should change the output to something that makes more sense (I've also added a print at the end of main):

Live On Coliru

Class constructor triggered.
Class moveconstructor triggered.
Class desctructor triggered.
Out of main scope.
Class desctructor triggered.

The first destructor call destroys moved-out "empty" instance of your class, while the second one fires when the vector itself is destroyed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

vector push_back in STL?

From Dev

Unable to push_back to vector

From Dev

vector push_back zero into an empty vector

From Dev

push_back a vector of vectors into a vector

From Java

Insert or push_back to end of a std::vector?

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

vector::push_back and std::move

From Dev

Why are there two overloads for vector::push_back?

From Dev

string Vector push_back failing in class

From Dev

garbage values for vector push_back

From Dev

push_back new element to vector

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

garbage values for vector push_back

From Dev

Vector push_back incredibly slow

From Dev

Vector push_back Array of doubles

From Dev

Push_back variadic function parameters into a vector?

From Dev

c++ push_back() a struct into a vector

From Dev

vector of reference wrapper, push_back failure?

From Dev

push_back a pointer to a vector of pointers

From Dev

How to use vector iterators when using vector<>::push_back()

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

Calling std::vector::push_back() is changing previous elements in vector?

From Dev

How to use vector iterators when using vector<>::push_back()

From Dev

C++ Vector: push_back Objects vs push_back Pointers performance

From Dev

vector push_back causes assertion error but list push_back works

From Dev

c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

From Dev

Thread safety std::vector push_back and reserve

From Dev

Cost of std::vector::push_back either succeeding or having no effect?

From Dev

Why push_back is slower than operator[] for a previously allocated vector

Related Related

  1. 1

    vector push_back in STL?

  2. 2

    Unable to push_back to vector

  3. 3

    vector push_back zero into an empty vector

  4. 4

    push_back a vector of vectors into a vector

  5. 5

    Insert or push_back to end of a std::vector?

  6. 6

    Implementatnion of std::vector::push_back in MSVC

  7. 7

    vector::push_back and std::move

  8. 8

    Why are there two overloads for vector::push_back?

  9. 9

    string Vector push_back failing in class

  10. 10

    garbage values for vector push_back

  11. 11

    push_back new element to vector

  12. 12

    Implementatnion of std::vector::push_back in MSVC

  13. 13

    garbage values for vector push_back

  14. 14

    Vector push_back incredibly slow

  15. 15

    Vector push_back Array of doubles

  16. 16

    Push_back variadic function parameters into a vector?

  17. 17

    c++ push_back() a struct into a vector

  18. 18

    vector of reference wrapper, push_back failure?

  19. 19

    push_back a pointer to a vector of pointers

  20. 20

    How to use vector iterators when using vector<>::push_back()

  21. 21

    Calling std::vector::push_back() is changing previous elements in vector?

  22. 22

    Calling std::vector::push_back() is changing previous elements in vector?

  23. 23

    How to use vector iterators when using vector<>::push_back()

  24. 24

    C++ Vector: push_back Objects vs push_back Pointers performance

  25. 25

    vector push_back causes assertion error but list push_back works

  26. 26

    c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

  27. 27

    Thread safety std::vector push_back and reserve

  28. 28

    Cost of std::vector::push_back either succeeding or having no effect?

  29. 29

    Why push_back is slower than operator[] for a previously allocated vector

HotTag

Archive