Ownership transfer changes which object is responsible for managing a resource. A raw-pointer copy does not transfer ownership: it creates another pointer to the same resource. Deleting through both pointers therefore causes undefined behavior. section 2.8
The double-deletion mistake
In the problematic example, b2 = b1 copies the pointer. b1 then deletes the Buffer, leaving b2 pointing at an already-deleted object; deleting b2 again produces undefined behavior. The intended transfer must be represented explicitly rather than inferred from pointer flow. section 2.8
Listing 2.16 Implementing exclusive ownership using unique_ptr
struct Buffer {
private:
std::unique_ptr<int[]> data; #1
public:
Buffer() : data(std::make_unique<int[]>(10)) {} #2
};
int main() {
auto b1 = std::make_unique<Buffer>(); #3
auto b2 = std::move(b1); #4
return 0;
}
Use std::unique_ptr for exclusive ownership: make_unique creates the Buffer, and std::move transfers responsibility from b1 to b2. The same pattern manages the Buffer array without a user-written destructor. section 2.8


