Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows.
According to the first answer in this article: Explicitly deleting a shared_ptrIs it possible to force delete a std::shared_ptr and the object it manages like below code?
For test purpose, I need to construct an object on non-zero memory. This could be done with:Since this is tedious and made multiple times, I'd like to provide a function returning a smart pointer to such a Type instance. I came up with the following, but I fear undefined behavior...
I was reading this article by Herb Sutter about passing smart pointers to functions. He doesn't mention std::weak_ptr and honestly I can't find a good scenario where passing such smart pointer is useful.
This is my allocation:And this is my deletion :Is the code above OK? I probably should just use smart pointers, but I don't know how the code will look like with them? So if you throw in a smart-pointer code it would be greatly appreciated. Those pointers are used locally...
Check following contrived program:From std::function constructor manual, I think constructing std::function object may throw exception, even the ratio is very low:
I commonly come across the need to create arrays or vectors of polymorphic objects. I'd usually prefer to use references, rather than smart pointers, to the base class because they tend to be simpler.
Both as int, why 0 but 1 can be converted to std::shared_ptr<T>?How the disability of conversion from 1 to std::shared_ptr<T> be checked when compiling?
Here is a code snippet which i want to get a tree structure with smart pointer.But i got c3646('parent': unknown override specifier) and c4430(missing type specifier - int assumed) in vs.Does anybody know what's going on and how do i fix it>?
Why and when do I need to supply my own deleter? Isn't keyword delete sufficient enough? If you use a smart pointer to manage a resource other than memory allocated by new, remember to pass a deleter.