How do smart pointers choose between delete and delete[]?

Unfortunately, they don’t know what delete to use therefore they use delete. That’s why for each smart pointer we have a smart array counterpart. std::shared_ptr uses delete std::shared_array uses delete[] So, your line std :: unique_ptr <int> x (new int [2]); actually causes undefined behavior. Incidentally, if you write std :: unique_ptr<int[]> p(new int[2]); ^^ … Read more

Should I use C++11 emplace_back with pointers containers?

Don’t use raw pointers, use std::unique_ptr like this: std::vector<std::unique_ptr<Fruit>> m_fruits; And as you can’t copy construct a std::unique_ptr you must use emplace_back (although you can use push_back with std::move). m_fruits.emplace_back(new Pear()); m_fruits.emplace_back(new Tomato()); Edit: As it appears that using std::vector<std::unique_ptr<T>>::emplace_back and new can leak if the std::vector needs and fails to reallocate memory, my recommended … Read more

How can you efficiently check whether two std::weak_ptr pointers are pointing to the same object?

Completely rewriting this answer because I totally misunderstood. This is a tricky thing to get right! The usual implementation of std::weak_ptr and std::shared_ptr that is consistent with the standard is to have two heap objects: the managed object, and a control block. Each shared pointer that refers to the same object contains a pointer to … Read more

To what degree does std::shared_ptr ensure thread-safety?

As others have pointed out, you’ve got it figured out correctly regarding your original 3 questions. But the ending part of your edit Calling reset() in thread IV will delete previous instance of A class created in first thread and replace it with new instance? Moreover after calling reset() in IV thread other threads will … Read more

Can you make a std::shared_ptr manage an array allocated with new T[]?

With C++17, shared_ptr can be used to manage a dynamically allocated array. The shared_ptr template argument in this case must be T[N] or T[]. So you may write shared_ptr<int[]> sp(new int[10]); From n4659, [util.smartptr.shared.const] template<class Y> explicit shared_ptr(Y* p); Requires: Y shall be a complete type. The expression delete[] p, when T is an array … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)