Create std::weak_ptr to an indexed value of an array-type std::shared_ptr?

Advertisements I have a large dataset which essentially loads like this: for(int i=0;i<count; i++){ auto ptr = std::make_shared<Object>(Object()); //do stuff with ptr } This isn’t great for performance, due to the memory allocation being piecemeal. What I want to do is this: std::shared_ptr<Object[]> allObjects; //this is stored elsewhere until the program ends allObjects.reset(new Object[count]); for(int… Read More Create std::weak_ptr to an indexed value of an array-type std::shared_ptr?

Calling `.lock()` on weak_ptr returns NULL shared_ptr

Advertisements I am somewhat confused by the behaviour of the .lock() call on a weak_ptr. My understanding is that .lock() will return a shared_ptr of the relevant type if it has not expired otherwise it will be a null pointer. From https://en.cppreference.com/w/cpp/memory/weak_ptr/lock : A shared_ptr which shares ownership of the owned object if std::weak_ptr::expired returns… Read More Calling `.lock()` on weak_ptr returns NULL shared_ptr