MrHuggies
MrHuggies

Reputation: 201

Should I use smart pointers on everything and forget about classic normal pointers?

I've been using C++ for a long time and know very well about carefulness in allocating and deallocating memory, especially not forgetting to delete unused instances.

Now, I've just recently used boost and with a problem I am facing I'm forced to used smart pointers (specifically the shared_ptr) one. So, if I am going to use shared_ptr for this problem, should I use smart pointers to all my normal pointer codebase?

Upvotes: 8

Views: 2961

Answers (5)

jcoder
jcoder

Reputation: 30035

No, you should not use smart pointers for everything. What you should consider whenever typing new:

  • What is the lifetime of this object?
  • Which object owns it?
  • How is that object going to manage its lifetime?

Sometimes the solution is a smart pointer but also the lazy answer. "I don't want to be bothered to work out which object owns this pointer and what its lifetime should be hence I'll make it a shared_pointer!"

The important question is; What is managing the lifetime of this object?, the answer can be a smart pointer, but oftentimes, it doesn't need to be.

Upvotes: 0

Omnifarious
Omnifarious

Reputation: 56038

Yes, you should greatly prefer smart pointers over bare pointers for almost everything. But that does not mean you should be using ::boost::shared_ptr for most of those cases. In fact I think you should use shared_ptr sparingly and carefully.

But for those pointers you don't use shared_ptr for you should be using ::std::auto_ptr or, if you have C++0x ::std::unique_ptr. And if they aren't appropriate, you should find a smart pointer type that is.

Now this isn't always the right answer, just almost always. Smart pointers are invaluable for tracking and freeing memory resources appropriately even in the face of exceptions or other such oddities of control flow.

There are cases in which you will need to either write your own smart pointer class or not use one. These cases are very rare, but they exist.

For example, using a smart pointer in your own smart pointer class is probably not the right thing to be doing. Pointing at stuff allocated in a C library would probable require a custom smart pointer that called free instead of delete. Maybe you want a stretchy buffer you can call realloc on and don't want to use a ::std::vector for some reason.

But generally, a smart pointer (though not usually ::boost::shared_ptr (or in C++0x ::std::shared_ptr)) is the right answer to your resource management problem.

Upvotes: 5

wheaties
wheaties

Reputation: 35970

Yes. You should be using the Boost smart pointers for most everything if you have them available. Remember this, while you can and from your comments do use pointers effectively, the person/people that come after you may not. Using the smart pointers can and will (hopefully, can't guard against bad code) mitigate some of these issues.

I'd also recommend using scoped_ptr inside your classes even if your classes are designed well. It'll help guard against issues the next guy could/might/most likely will introduce when faced with a naked pointer. It'll encourage them to use them too, by example. The last thing you want is to have to track down a memory issue because someone forgot to initialize the pointer to NULL and it's passing the if statement check.

Upvotes: 1

EboMike
EboMike

Reputation: 77732

No. It depends on what you're doing.

  • Smart pointers have a performance overhead. In desktop applications, this is typically not a concern, but depending on what you do, it might be.
  • Smart pointers will not work right if you have reference cycles, i.e. A pointing to B and B pointing to A, or even something pointing to itself.

Upvotes: -1

Cătălin Pitiș
Cătălin Pitiș

Reputation: 14341

You should use smart pointers careful. They are not the silver bullet when considering memory management. Circular references are still an issue.

When making the class design, always think who has the ownership of an object (has the responsibility to destroy that object). Complement that with smart pointers, if necessary, but don't forget about the ownership.

Upvotes: 19

Related Questions