Consider the following code
std::vector<std::array<double,10>> a(10);
If I understand the standard correctly a will not be zero initialized, because
en.cppreference.com on std::vector constructors says
- Constructs the container with count default-inserted instances of T. No copies are made.
So because default initializing std::array<double, 10> does not fill it with zeros, a will also not contain zeros.
Is this true?
How can I enforce zero initialization?
Will a.data() point to 100 continuous double values?
Edit:
This is the output from godbolt on gcc 10.2 with -O2
main:
mov edi, 800
sub rsp, 8
call operator new(unsigned long)
mov rdi, rax
lea rdx, [rax+800]
.L2:
mov QWORD PTR [rax], 0x000000000
add rax, 80
mov QWORD PTR [rax-72], 0x000000000
mov QWORD PTR [rax-64], 0x000000000
mov QWORD PTR [rax-56], 0x000000000
mov QWORD PTR [rax-48], 0x000000000
mov QWORD PTR [rax-40], 0x000000000
mov QWORD PTR [rax-32], 0x000000000
mov QWORD PTR [rax-24], 0x000000000
mov QWORD PTR [rax-16], 0x000000000
mov QWORD PTR [rax-8], 0x000000000
cmp rdx, rax
jne .L2
mov esi, 800
call operator delete(void*, unsigned long)
xor eax, eax
add rsp, 8
ret
So it does seems to be zero initialized. Then the question remains why.
std::vector<std::array<double,10>> a(10, {0});should zero-initialize, I think. Treatinga.data()as an array of 100 doubles would exhibit undefined behavior, I believe.std::vector<std::array<double,10>> a(10, {0,0,0,0,0,0,0,0,0,0});?