weak_ptr, make_shared and memory deallocation

Is my understanding correct?

Yes. If your weak_ptrs significantly outlive the (large) object and you are tight on memory, it may be beneficial to avoid make_shared.

However, “large” here is measured by sizeof, and many conceptually “large” objects (for example, most standard containers, except std::array) are quite small by that metric, because they allocate additional memory to store their contents, which will be freed as soon as the object is destroyed.

Leave a Comment