Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the vector.resize() method calls the default elements constructors when resizing?

Tags:

c++

stdvector

I am trying the following code:

struct _Struct2
{
    void   *ptr;
    double dval;
};

struct _Struct
{
    float fval;
    int   ival;
    std::vector<_Struct2>   data;
};

std::vector<_Struct>    vec;


int main()
{
    vec.resize( 9 );
    for ( int i = 0; i < vec.size(); i++ )
    {
        _Struct &elem = vec[i];
        int     len = elem.data.size(); // elem.data is [0]()
    }
}

The resize(9) should allocate 9 elements of type _Struct. And, in fact it works. But every element of type _Struct is not initialized, especially the data element, which is another std::vector. I would like it to be initialized to the empty std::vector. Have to do that manually? I thought that the resize() method would have called the default constructor of every _Struct element. Thx

Ps. The names of the structs used here are just the first things that come to my mind. This is just an example. My Visual Studio tells me that elem.data correspond, in the debug view, to [0]().

Ps. Forget the [0]().

like image 656
Luke Avatar asked Sep 14 '11 09:09

Luke


People also ask

What happens when you resize a vector?

vector::resize() The function alters the container's content in actual by inserting or deleting the elements from it. It happens so, If the given value of n is less than the size at present then extra elements are demolished.

How do I make a vector the default size?

We can also specify a random default value for the vector. In order to do so, below is the approach: Syntax: // For declaring vector v(size, default_value); // For Vector with a specific default value // here 5 is the size of the vector // and 10 is the default value vector v1(5, 10);

Does resizing a vector delete data?

Resizing a vector doesn't destroy the values stored in the vector (except for those beyond the new size when shrinking, of course), however growing a vector beyond its capacity will copy (or, in C++11, move) them to a new place, thus invalidating and iterators, pointers or references to those elements.

Does resize change capacity?

Calling resize() with a smaller size has no effect on the capacity of a vector . It will not free memory.


1 Answers

No it doesn't call default element constructor. std::vector never calls default constructors internally (it does in C++11, but not in earlier versions of the specification).

The full signature for vector::resize looks as follows

void resize(size_type sz, T c = T());

I.e. it has a second parameter (with default argument value). That second parameter is then used as a "source" object to initialize new elements by copy-constructor.

In other words, your call to resize is actually equivalent to

vec.resize( 9, _Struct() );

meaning that it is you who called the default constructor when you supplied that "source" object to vector::resize, even though you didn't notice that.

But every element of type _Struct is not initialized, especially the data element, which is another std::vector.

Huh? "Not initialized"? I don't know what that is supposed to mean, considering that in your sample code every new element created by resize is perfectly initialized as described above: it is copy-initialized from a _Struct() element you implicitly supplied to resize as the second argument. Each _Struct::fval and _Struct::ival is zero, and each _Struct::data is an empty vector.

(In the original C++98 _Struct::fval and _Struct::ival will remain uninitialized, since pre-TC1 C++98 didn't support value-initialization. But _Struct::data will be initialized to an empty vector even in the original C++98).

I would like it to be initialized to the empty std::vector.

Every _Struct::data vector is already initialized as an empty vector. What made you believe that it isn't?

P.S. Names that begin with _ followed by an uppercase letter are reserved by the implementation. You are not allowed to use them.

like image 134
AnT Avatar answered Sep 20 '22 05:09

AnT