smart-pointers

Is there any use for unique_ptr with array?

房东的猫 提交于 2019-11-26 03:29:24
问题 std::unique_ptr has support for arrays, for instance: std::unique_ptr<int[]> p(new int[10]); but is it needed? probably it is more convenient to use std::vector or std::array . Do you find any use for that construct? 回答1: Some people do not have the luxury of using std::vector , even with allocators. Some people need a dynamically sized array, so std::array is out. And some people get their arrays from other code that is known to return an array; and that code isn't going to be rewritten to

smart pointers (boost) explained

独自空忆成欢 提交于 2019-11-26 03:26:08
问题 What is the difference between the following set of pointers? When do you use each pointer in production code, if at all? Examples would be appreciated! scoped_ptr shared_ptr weak_ptr intrusive_ptr Do you use boost in production code? 回答1: Basic properties of smart pointers It's easy when you have properties that you can assign each smart pointer. There are three important properties. no ownership at all transfer of ownership share of ownership The first means that a smart pointer cannot

What C++ Smart Pointer Implementations are available?

戏子无情 提交于 2019-11-26 03:23:20
问题 Comparisons, Pros, Cons, and When to Use? This is a spin-off from a garbage collection thread where what I thought was a simple answer generated a lot of comments about some specific smart pointer implementations so it seemed worth starting a new post. Ultimately the question is what are the various implementations of smart pointers in C++ out there and how do they compare? Just simple pros and cons or exceptions and gotchas to something you might otherwise think should work. I\'ve posted

Smart pointers: who owns the object? [closed]

断了今生、忘了曾经 提交于 2019-11-26 02:26:22
问题 C++ is all about memory ownership - aka ownership semantics . It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented by the type a raw pointer is wrapped inside thus in a good (IMO) C++ program it is very rare ( rare , not never ) to see raw pointers passed around (as raw pointers have no inferred ownership thus we can not tell who owns the memory and thus

shared_ptr magic :)

心不动则不痛 提交于 2019-11-26 02:06:22
问题 Mr. Lidström and I had an argument :) Mr. Lidström\'s claim is that a construct shared_ptr<Base> p(new Derived); doesn\'t require Base to have a virtual destructor: Armen Tsirunyan : \"Really? Will the shared_ptr clean up correctly? Could you please in this case demonstrate how that effect could be implemented?\" Daniel Lidström : \"The shared_ptr uses its own destructor to delete the Concrete instance. This is known as RAII within the C++ community. My advice is that you learn all you can

Why can I not push_back a unique_ptr into a vector?

北城以北 提交于 2019-11-25 23:28:08
问题 What is wrong with this program? #include <memory> #include <vector> int main() { std::vector<std::unique_ptr<int>> vec; int x(1); std::unique_ptr<int> ptr2x(&x); vec.push_back(ptr2x); //This tiny command has a vicious error. return 0; } The error: In file included from c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0, from c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48, from c:\\mingw\\bin\\../lib/gcc/mingw32/4.5.0/include/c++

Which kind of pointer do I use when?

巧了我就是萌 提交于 2019-11-25 22:59:45
问题 Ok, so the last time I wrote C++ for a living, std::auto_ptr was all the std lib had available, and boost::shared_ptr was all the rage. I never really looked into the other smart pointer types boost provided. I understand that C++11 now provides some of the types boost came up with, but not all of them. So does someone have a simple algorithm to determine when to use which smart pointer? Preferably including advice regarding dumb pointers (raw pointers like T* ) and the rest of the boost

RAII and smart pointers in C++

旧时模样 提交于 2019-11-25 22:24:42
问题 In practice with C++, what is RAII, what are smart pointers, how are these implemented in a program and what are the benefits of using RAII with smart pointers? 回答1: A simple (and perhaps overused) example of RAII is a File class. Without RAII, the code might look something like this: File file("/path/to/file"); // Do stuff with file file.close(); In other words, we must make sure that we close the file once we've finished with it. This has two drawbacks - firstly, wherever we use File, we

What is a smart pointer and when should I use one?

℡╲_俬逩灬. 提交于 2019-11-25 21:34:55
问题 What is a smart pointer and when should I use one? 回答1: UPDATE This answer is rather old, and so describes what was 'good' at the time, which was smart pointers provided by the Boost library. Since C++11, the standard library has provided sufficient smart pointers types, and so you should favour the use of std::unique_ptr, std::shared_ptr and std::weak_ptr. There was also std::auto_ptr. It was very much like a scoped pointer, except that it also had the "special" dangerous ability to be

What C++ Smart Pointer Implementations are available?

醉酒当歌 提交于 2019-11-25 20:08:37
Comparisons, Pros, Cons, and When to Use? This is a spin-off from a garbage collection thread where what I thought was a simple answer generated a lot of comments about some specific smart pointer implementations so it seemed worth starting a new post. Ultimately the question is what are the various implementations of smart pointers in C++ out there and how do they compare? Just simple pros and cons or exceptions and gotchas to something you might otherwise think should work. I've posted some implementations that I've used or at least glossed over and considered using as an answer below and my