How does a reference-counting smart pointer’s reference counting work?

I’ve seen two different non-intrusive approaches to this: The smart pointer allocates a small block of memory to contain the reference counter. Each copy of the smart pointer then receives a pointer to the actual object and a pointer to the reference count. In addition to an object pointer, each smart pointer contains a previous … Read more

Is the Rule of 5 (for constructors and destructors) outdated, if smart pointers can take care of resource management?

The full name of the rule is the rule of 3/5/0. It doesn’t say “Always provide all five”. It says that you have to either provide the three, the five, or none of them. Indeed, more often than not the smartest move is to not provide any of the five. But you can’t do that … Read more

What is the difference between std::shared_ptr and std::atomic aka. std::experimental::atomic_shared_ptr?

The atomic “thing” in shared_ptr is not the shared pointer itself, but the control block it points to. meaning that as long as you don’t mutate the shared_ptr across multiple threads, you are ok. do note that copying a shared_ptr only mutates the control block, and not the shared_ptr itself. std::shared_ptr<int> ptr = std::make_shared<int>(4); for … Read more

How to check memory allocation failures with new operator?

Well, you call new that throws bad_alloc, so you must catch it: try { scoped_array<char> buf(new char[MAX_BUF]); … } catch(std::bad_alloc&) { … } or scoped_array<char> buf(new(nothrow) char[MAX_BUF]); if(!buf) { //allocation failed } What I mean by my answer is that smart pointers propagate exceptions. So if you’re allocating memory with ordinary throwing new, you must … Read more

How to avoid memory leak with shared_ptr?

If you have circular references like this, one object should hold a weak_ptr to the other, not a shared_ptr. From the shared_ptr introduction: Because the implementation uses reference counting, cycles of shared_ptr instances will not be reclaimed. For example, if main() holds a shared_ptr to A, which directly or indirectly holds a shared_ptr back to … Read more

Set shared_ptr with new_pointer that is old_pointer + offset

Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr // make sure you use an array deleter std::shared_ptr<char> osp(new char[1024], std::default_delete<char[]>()); // load the data into your buffer at osp.get() // Find the offset in the data by parsing auto const offset = parse_buffer_for_offset(osp.get()); // Now set a … Read more

Is it possible to use a C++ smart pointers together with C’s malloc?

If you are using shared_ptr or unique_ptr, you can specify a custom deleter. For example, struct free_delete { void operator()(void* x) { free(x); } }; This can be used with shared_ptr like so: std::shared_ptr<int> sp((int*)malloc(sizeof(int)), free_delete()); If you are using unique_ptr, the deleter is a part of the unique_ptr‘s type, so the deleter needs to … Read more

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