One of the std::vector
constructors is stipulated as, emphasis mine:
explicit vector(size_type n, const Allocator& = Allocator());
Effects: Constructs avector
withn
default-inserted elements using the specified allocator.
Requires:T
shall beDefaultInsertable
into*this.
Complexity: Linear inn
.
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');
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);
This is the initialization performed when an object is constructed with no initializer.
As default constructor initializes the data members of class to 0.
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.
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
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;
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;
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.
memset()
is appropriate.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With