Header Reference: <memory>
Header Reference: <memory>
Smart pointers, allocators, uninitialized-memory helpers, and the ownership vocabulary provided by the memory header.
Header Reference: <memory>
Smart pointers, allocators, uninitialized-memory helpers, and the ownership vocabulary provided by the memory header.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.
<memory>std::unique_ptrstd::shared_ptrstd::weak_ptrstd::make_unique, std::make_shared, std::allocate_sharedstd::addressof and std::to_addressstd::unique_ptr by default for exclusive ownershipstd::shared_ptr only when ownership is truly sharedstd::weak_ptr to observe shared state without extending lifetime#include <memory>
#include <string>
struct Widget {
explicit Widget(std::string name) : name(std::move(name)) {}
std::string name;
};
int main() {
auto widget = std::make_unique<Widget>("core");
auto shared = std::make_shared<Widget>(widget->name);
return shared->name == "core" ? 0 : 1;
}
#include <memory>
#include <string>
int main() {
auto owner = std::make_unique<std::string>("notes");
auto shared = std::make_shared<std::string>(*owner);
owner.reset();
return static_cast<int>(shared->size());
}