Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default-inserting into a vector isn't default initialization?

Tags:

c++

c++11

vector

One of the std::vector constructors is stipulated as, emphasis mine:

explicit vector(size_type n, const Allocator& = Allocator());
Effects: Constructs a vector with n default-inserted elements using the specified allocator.
Requires: T shall be DefaultInsertable into *this.
Complexity: Linear in n.

Is default-insertion in any way related to default initialization? On this code:

std::vector<char> v(66000);

gcc 5.2 optimized produces:

  400d18:   bf d0 01 01 00          mov    $0x101d0,%edi
  400d1d:   48 83 c5 01             add    $0x1,%rbp
  400d21:   e8 1a fd ff ff          callq  400a40 <operator new(unsigned long)@plt>
  400d26:   31 f6                   xor    %esi,%esi
  400d28:   48 89 c3                mov    %rax,%rbx
  400d2b:   ba d0 01 01 00          mov    $0x101d0,%edx
  400d30:   48 89 c7                mov    %rax,%rdi
  400d33:   e8 38 fc ff ff          callq  400970 <memset@plt>

memset? What are you doing here? I thought this should simply do the equivalent of new char[66000]... that is, no initialization. clang 3.7 also emits a memset.

Why is there a memset here? Is this correct with respect to the standard? After all, if I wanted 66000 value-initialized chars I already have this constructor:

std::vector<char> v(66000, '\0');
like image 509
Barry Avatar asked Mar 09 '16 19:03

Barry


People also ask

How do I initialize a vector to default?

Specifying a 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);

What is default initialisation?

This is the initialization performed when an object is constructed with no initializer.

Does default constructor initialize to zero?

As default constructor initializes the data members of class to 0.

What is Default initialization of non-class variables?

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized ) If T is a const-qualified type, it must be a class type with a user-provided default constructor.

Which types are not initialized by default in C++?

Scalars and POD types with dynamic storage duration were considered to be not initialized (since C++11, this situation was reclassified as a form of default initialization). each direct non-static data member M of T is of class type X (or array thereof), X is const-default-constructible, and

What are the effects of Default initialization in C++?

The effects of default initialization are: if T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. if T is an array type, every element of the array is default-initialized;

How do you initialize a vector in C++?

Initialize a vector in C++ (5 different ways) Following are different ways to create and initialize a vector in C++ STL. Initializing by one by one pushing values : // CPP program to create an empty vector. // and one by one push values. #include <bits/stdc++.h>. using namespace std;


1 Answers

This is correct behaviour. See 23.2.1:

An element of X is default-inserted if it is initialized by evaluation of the expression allocator_traits<A>::construct(m, p)

Than, allocator_traits<A>::construct will call a.construct(p, std::forward<Args>(args)...). Which, in turn, calls ::new((void *)p) U(std::forward<Args>(args)...), which in effect calls new(), which does value-initialization.

Conclusion:

memset() is appropriate.

Conclusion #2

Absent custom allocators, std::vector does not allow one an option to access uninitialized storage. Every object which is legitimately in vector was value-initialized.

like image 173
SergeyA Avatar answered Nov 14 '22 23:11

SergeyA