Can I initialize std::unique_ptr of array without default initialization ( I want just let it have dummy value )
I'm trying to use unique_ptr of array for preventing memory leak.
but It looks always initializing values with 0 ( look "movups XMMWORD PTR [rax+48], xmm0" )
I don't need this. I just want vector2 have dummy value.
Can I do this??
#include <memory>
struct Vector2
{
float x, y;
};
struct A
{
std::unique_ptr<Vector2[]> vector2;
};
int main()
{
A a;
a.vector2 = std::make_unique<Vector2[]>(10);
}
sub rsp, 40 ; 00000028H
mov QWORD PTR a$[rsp], 0
mov ecx, 80 ; 00000050H
call void * operator new[](unsigned __int64) ; operator new[]
test rax, rax
je SHORT $LN15@main
xorps xmm0, xmm0
movups XMMWORD PTR [rax], xmm0
movups XMMWORD PTR [rax+16], xmm0
movups XMMWORD PTR [rax+32], xmm0
movups XMMWORD PTR [rax+48], xmm0
movups XMMWORD PTR [rax+64], xmm0
make_unique
performs value-initialization on the array. You can use make_unique_for_overwrite
(since C++20) instead, which performs default-initialization.
a.vector2 = std::make_unique_for_overwrite<Vector2[]>(10);
Before C++20, you can new
the array by yourself.
a.vector2 = std::unique_ptr<Vector2[]>(new Vector2[10]);
// or
a.vector2.reset(new Vector2[10]);