I am working on a project where a couple of the classes overload operator new and delete to utilize free-lists and I tried to use make_shared to have my allocations managed by smart pointers when I realized that make_shared does not use the overloaded versions but makes an explicit call the global ::new. But according to this make_unique does use the overloaded versions. Which is quite baffling to me. Why does make_shared choose to ignore operator overloading but make_unique doesn't?
make_shared has to allocate two things: the object being constructed and the shared_ptr's control block. To improve performance, it allocates one chunk of memory big enough for both and then placement-news them.
make_unique doesn't need to do that since a unique_ptr doesn't need a control block.
If you want to control how memory is allocated for an object to be managed by a shared_ptr, create an appropriate allocator class and use allocate_shared instead of make_shared.
Related
Is it possible to use both a custom allocator and a custom deleter at the same time for std::shared_ptr? It seems to me that there is no way of doing so, since std::allocate_shared doesn't take a deleter. And also, the only sensible signature of the deleter would be something like void deleter(T*, const Alloc&), instead of just void deleter(T*).
Is there any way to work around this limitation?
Yes, you can do that, but make sure you understand what's going on. The deleter's purpose is to destroy the object. The allocator is used for internal book-keeping structures.
std::shared_ptr<T> sp(new T(args...), std::default_delete<T>(), myalloc);
The point of make_shared and allocate_shared is that they take care of constructing the object for you, so you don't specify a deleter — they use their own deleter that's appropriate for the way they obtained resources (i.e. respectively via operator new and the provided allocator).
You can of course create your own allocator deleter (like this one or the one proposed here) to pass into the above constructor to go along with an allocator-allocated object, and then also use the allocator (or a different one!) for the book-keeping.
There's something to be said for not using make_shared/allocate_shared in situations where either you have long-lived weak pointers or where binary size matters.
You can use a separate allocator and deleter; there are shared_ptr constructors that take both.
But you cannot do this through allocate_shared. The reason being that the allocator is for allocating/deallocating the shared storage. The deleter is for destroying the object being managed and freeing its storage.
Since allocate_shared allocates the object being managed in the same storage as the shared storage itself, it no longer makes sense for the two operations to be separate. And therefore, you must use the same object for both processes. The allocator allocates and deallocates the single allocation, and it takes care of construction/destruction duties for the T being created and destroyed.
Stated that there is no C++ equivalent of the C realloc function, I've found in another question that such a thing is automatically managed by std::vector and we should use it instead.
I'm fine with it. I guess that, since there is no other way of do reallocation, the std::vector will just call realloc for me.
However, the question is: if I'm overriding the new and the delete operators for managing of tracking memory usage globally, they will not be called in the case someone calls old C functions (malloc, calloc, realloc, free).
How can it be done? Is it correct that std::vector replaces realloc?
std::vector won't call realloc; it will use its allocator to achieve something similar: allocating new memory, moving objects into it, then deallocating the old memory. The default allocator uses operator new and operator delete, and so will use your replacements if you provide them.
realloc would be entirely the wrong thing to do if the vector contains non-trivial objects; it copies raw data, while C++ objects generally have to be copied or moved by calling their special functions.
Nothing in the C++ library (except perhaps the default implementations of operator new and operator delete) will call the C allocation functions directly. Since you shouldn't be calling them yourself, you only need to worry about them if you're using a C library.
From the research I have done, it sounds like std::make_shared is the preferred way of constructing a std::shared_ptr. Specifically because:
It performs only one memory allocation, compared with using new, which performs at least two.
If the ctor passed to make_shared throws, then it won't leak, as it will with new.
My question is, assuming that I want a shared_ptr, should I always use make_shared, or are there cases where new is preferred?
As the counter and the object share the same allocation, they also share the same deallocation.
The counter has to persist until the last shared_ptr and weak_ptr go away. If you have a large object (or many small objects) with long-lasting weak_ptrs, this can cause memory contention if you allocate the shared_ptrs via make_shared.
Second, if you have a 3rd party API that hands you a pointer or a resource handle, and possibly has its own dispose functionality, make_shared is neither appropriate nor possible to use in every case. Creating your own make_ functions can keep the messy details out of the way lets you deal with this problem, and deals with the exception corner case as well.
Finally, while shared pointers are awesome, they are also overly powerful. Quite often I want a unique_ptr or even a boost::scoped_ptr, or an intrusive reference counting pointer, or the like to represent ownership. shared_ptr should be used only when the situation actually involves shared ownership of the resource: using it willy nilly because it is "easy" tends to end up with the resource equivalent of spaghetti code.
You may have to deal with legacy code which returns a dynamically allocated object. In which case, you would need to use the std::shared_ptr<T> ctor with the pointer parameter. It's not preferable to using std::make_shared but it does allow you to use all the std::shared_ptr<T> goodness with legacy code.
I know that this is not strictly equivalent to using the std::shared_ptr<T> ctor with new directly but it is a valid use case of std::shared_ptr<T> where make_shared cannot be utilised.
I am a bit uncertain about the interpretation of your question. I am assuming that it is justified to use a shared_ptr<T>; I can only second Yakk on the reasons why you wouldn't want to use shared_ptr in the first place.
There is one situation where you cannot use make_shared or allocate_shared to construct the shared_ptr but you need to use the corresponding ctor: If you need to pass in a custom deleter, see (3) and (4) at the ctors of shared_ptr.
I ran into problems using make_shared on a class with a private constructor (from a static factory method). I don't think there's an easy solution to this.
should I always used make_shared, or are there cases where new is
preferred
make_shared is not allowed when we are storing a naked pointer in shared_ptr allocated by someone else. and it can only call public constructors. However there are some reports in some compiler about accessing protected constructor using make_shared like this.
Instead of using try/catch blocks everytime I need to construct any objects after allocating a block of memory for them with an allocator I use internally in my container, I would like to use a class derived from std::auto_ptr (I have no c++11 here) with an overridden destructor, so that it could deallocate the pointed memory and release the pointer afterwards. Are there any disadvantages in doing this?
If you don't mind using boost and boost::shared_ptr<>, you can provide a custom deleter. See the top 2 answers to this question.
I am using few library functions that return a pointer created either using malloc or new.
So, I have my own customer deallocator based on what type of allocation was used.
E.g
shared_ptr<int> ptr1(LibFunctA(), &MallocDeleter); //LibFunctA returns pointer created using malloc
shared_ptr<int> ptr2(LibFunctB(), &newDeleter); //LibFunctB returns pointer created using new
Now, I understand this is a very naive use of deallocator above but what other scenarios is it heavily used for ?
Also, how can one use a customer allocator ? I tried to assign a custom allocator as below but now how do I actually get it called ? Where does this kind of feature help ?
shared_ptr<int> ptr3(nullptr_t, &CustomDeleter, &CustomAllocator); //assume both functs are defined somewhere.
I don't see anything "naive" about using deleters that way. It is the main purpose of the feature after all; to destroy pointer objects that aren't allocated using the standard C++ methods.
Allocators are for when you need control of how the shared_ptr's control block of memory is allocated and deleted. For example, you might have a pool of memory that you want these things to come from, or if you're in a memory-limited situation where allocation of memory via new is simply not acceptable. And since the type of the control block is up to shared_ptr, there's no other way to be able to control how it is allocated except with some kind of allocator.
Custom deleters for shared_ptr are very useful for wrapping some (usually) C resource that you need to later call a freeing function on. For example, you might do something like:
shared_ptr<void> file(::CreateFileW(...), ::CloseHandle);
Examples like this abound in C libraries. This saves from having to manually free the resource later and take care of possible exceptions and other nasties.
I think the custom allocator will be used to allocate space for the "shared count" object, that stores a copy of the deallocator (deleter) and the reference counter.
As for what a custom deleter can be used for...
One use was already mentioned: make shared_ptr compatible with objects that must be deleted by some special function (like FILE which is deleted by fclose), without having to wrap it into a helper-class that takes care of the proper deletion.
Another use for a custom deleter is pools. The pool can hand out shared_ptr<T> that were initialized with a "special" deleter, which doesn't really delete anything, but returns the object to the pool instead.
And one other thing: the deleter is already necessary to implement some shared_ptr features. E.g. the type that's deleted is always fixed at creation time, and independent of the type of the shared_ptr that's being initialized.
Vou can create a shared_ptr<Base> by actually initializing it with a Derived. shared_ptr guarantees that when the object is deleted, it will be deleted as a Derived, even if Base does not have a virtual dtor. To make this possible, shared_ptr already has to store some information about how the object shall be deleted. So allowing the user to specify a completely custom deleter doesn't cost anything (in terms of runtime performance), and doesn't require much additional code either.
There are probably dozens of other scenarios where one can make good use of the custom deleter, that's just what I have come up with so far.