smart-pointers

Smart pointers/safe memory management for C?

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:17:02
问题 I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes, operator overloading, et cetera. For someone writing in raw C99, where could you point (no pun intended) to help with safe memory management? Thanks. 回答1: The question is a bit old, but I figured I would take the time to link to my smart pointer

How to return smart pointers (shared_ptr), by reference or by value?

≡放荡痞女 提交于 2019-11-26 23:51:17
问题 Let's say I have a class with a method that returns a shared_ptr . What are the possible benefits and drawbacks of returning it by reference or by value? Two possible clues: Early object destruction. If I return the shared_ptr by (const) reference, the reference counter is not incremented, so I incur the risk of having the object deleted when it goes out of scope in another context (e.g. another thread). Is this correct? What if the environment is single-threaded, can this situation happen as

When should I use raw pointers over smart pointers?

假如想象 提交于 2019-11-26 23:40:36
After reading this answer , it looks like it is a best practice to use smart pointers as much as possible, and to reduce the usage of "normal"/raw pointers to minimum. Is that true? No, it's not true. If a function needs a pointer and has nothing to do with ownership, then I strongly believe that a regular pointer should be passed for the following reasons: No ownership, therefore you don't know what kind of a smart pointer to pass If you pass a specific pointer, like shared_ptr , then you won't be able to pass, say, scoped_ptr The rule would be this - if you know that an entity must take a

std::auto_ptr to std::unique_ptr

会有一股神秘感。 提交于 2019-11-26 21:17:43
With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr . Does their usage exactly overlap (so I can do a global find/replace on my code (not that I would do this, but if I did)) or should I be aware of some differences that are not apparent from reading the documentation? Also if it is a direct replacement, why give it a new name rather than just improve the std::auto_ptr ? Cubbi You cannot do a global find/replace because you can copy an auto_ptr (with known consequences), but a unique_ptr

What's the best signature for clone() in C++?

醉酒当歌 提交于 2019-11-26 20:14:31
问题 As Scott Myers wrote, you can take advantage of a relaxation in C++'s type-system to declare clone() to return a pointer to the actual type being declared: class Base { virtual Base* clone() const = 0; }; class Derived : public Base { virtual Derived* clone() const }; The compiler detects that clone() returns an pointer to the type of the object, and allows Derived to override it to return a pointer to derived. It would desirable to have clone() return a smart pointer that implies transfer of

Smart pointers in container like std::vector?

为君一笑 提交于 2019-11-26 20:13:06
问题 I am learning about smart pointers ( std::auto_ptr ) and just read here and here that smart pointers ( std::auto_ptr ) should not be put in containers (i.e. std::vector ) because even most compilers won't complain and it might seem correct. There is no rule that says smart pointers won't be copied internally (by vector class for example) and transfer its ownership, then the pointer will become NULL. In the end, everything will be screwed up. In reality, how often does this happen? Sometimes I

Is there a non-atomic equivalent of std::shared_ptr? And why isn't there one in <memory>?

 ̄綄美尐妖づ 提交于 2019-11-26 19:57:54
This is a bit of a two part question, all about the atomicity of std::shared_ptr : 1. As far as I can tell, std::shared_ptr is the only smart pointer in <memory> that's atomic. I'm wondering if there is a non-atomic version of std::shared_ptr available (I can't see anything in <memory> , so I'm also open to suggestions outside of the standard, like those in Boost). I know boost::shared_ptr is also atomic (if BOOST_SP_DISABLE_THREADS isn't defined), but maybe there's another alternative? I'm looking for something that has the same semantics as std::shared_ptr , but without the atomicity. 2. I

Example to use shared_ptr?

孤街醉人 提交于 2019-11-26 17:55:52
问题 Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... virtual void Run() { //A virtual function } }; class ANDgate :public gate {..... ....... void Run() { //AND version of Run } }; class ORgate :public gate {..... ....... void Run() { //OR version of Run } }; //Running the

How could one implement std::auto_ptr's copy constructor?

旧街凉风 提交于 2019-11-26 16:55:59
问题 Back on my crazy AutoArray thingy... (quoting important bits from there: class AutoArray { void * buffer; public: //Creates a new empty AutoArray AutoArray(); //std::auto_ptr copy semantics AutoArray(AutoArray&); //Note it can't be const because the "other" reference //is null'd on copy... AutoArray& operator=(AutoArray); ~AutoArray(); //Nothrow swap // Note: At the moment this method is not thread safe. void Swap(AutoArray&); }; ) Anyway, trying to implement the copy constructor. There's a

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

谁说我不能喝 提交于 2019-11-26 16:33:07
问题 Some of my code still uses malloc instead of new . The reason is because I am afraid to use new because it throws exception, rather than returning NULL , which I can easily check for. Wrapping every call to new in a try{}catch(){} also doesn't look that good. Whereas when using malloc I can just do if (!new_mem) { /* handle error */ } . Therefore I have a question. Can I use smart pointers together with malloc ? Something like: SmartPointer<Type> smarty = malloc(sizeof(Type)); Something like