Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`make_unique_for_overwrite` still initializes `std::pair` elements

Tags:

c++

memory

c++20

I was hoping that

auto myPairs = make_unique_for_overwrite<pair<uint64_t, void*>[]>(arraySize);

would give me uninitialized memory for my pairs. I am overwriting those later anyway and the (unnecessary) initialization is currently responsible for 120ms out of 600ms overall runtime for my algorithm.

What is the most idiomatic way to avoid this initialization?

like image 974
Vogelsgesang Avatar asked Aug 25 '21 21:08

Vogelsgesang


1 Answers

According to cppreference, the default constructor of std::pair always value-initializes (aka zeroes) its elements.

The solution is to get rid of pair. You can replace it with a structure with two members.

I know I could still just allocate ... and then reinterpret_cast

Attempt to reinterpret_cast such structure to std::pair would cause undefined behavior.

like image 68
HolyBlackCat Avatar answered Sep 27 '22 23:09

HolyBlackCat