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');