jeudi 22 mars 2018

C++ - make_shared when custom new and delete operators are defined

Regarding the book "Effective Modern C++" from Scot Meyers, and the 21st item: "Prefer std::make_unique and std::make_shared to direct use of new":

"Some classes define their own versions of operator new and operator delete. Often, class-specific routines are designed only to allocate and deallocate chunks of memory of precisely the size of objects of the class. Such routines are a poor fit for std::shared_ptr’s support for custom allocation (via std::allocate_shared) and deallocation (via custom deleters), because the amount of memory that std::allocate_shared requests isn’t the size of the dynamically allocated object, it’s the size of that object plus the size of a control block. Consequently, using make functions to create objects of types with class-specific versions of operator new and operator delete is typically a poor idea."

Why is this a problem for allocate_shared/make_shared, if custom new and delete are called on same places as standard new and delete?

Construction: Operator new is used just to construct the resource object, but make_shared/allocate_shared construct the constrol block.

Destruction: With or without a custom deleter function specified, when delete is called, just the resource object should be removed. Cntrol block depends on reference and weak counts.

Why then the sentence: "Such routines are a poor fit for std::shared_ptr’s support for custom allocation (via std::allocate_shared) and deallocation (via custom deleters), because the amount of memory that std::allocate_shared requests isn’t the size of the dynamically allocated object, it’s the size of that object plus the size of a control block."?

Aucun commentaire:

Enregistrer un commentaire