Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::shared_ptr preallocation memory

I want to preallocate memory both for control block and value_type for shared_ptr in one heap request (like std::make_shared), but do not construct immediately any object in it. And when I actually need to construct object use placement new. Is it possible? Neither std::make_shared or std::allocate_shared seem to solve my problem.

like image 580
user1641854 Avatar asked Oct 31 '22 08:10

user1641854


1 Answers

I would propose to create postponed initialization wrapper class that contains enough memory within itself for object you want to initialize later. This wrapper could even have special method for invoking placement new and delete initialized object in destructor.

like image 199
George Sovetov Avatar answered Nov 15 '22 06:11

George Sovetov