Vijay
Vijay

Reputation: 67319

Smart pointers - cases where they cannot replace raw pointers

HI,

I have this query about smart pointers.

I heard from one of my friends that smart pointers can almost always replace raw pointers. but when i asked him what are the other cases where smart pointers cannot replace the raw pointers,i did not get the answer from him.

could anybody please tell me when and where they cannot replace raw pointers?

Upvotes: 11

Views: 1489

Answers (7)

UncleBens
UncleBens

Reputation: 41351

It would be quite hard to implement smart pointers if at some point you don't use plain pointers.

I suppose it would also be harder to implement certain data structures with smart pointers. E.g freeing the memory of a regular linked list is quite trivial, but it would take some thought to figure out the combination of owning and non-owning smart pointers to get the same result.

Upvotes: 1

Matthieu M.
Matthieu M.

Reputation: 300439

It's a matter of semantics:

  • smart pointer: you own (at least partly) the memory being pointed to, and as such are responsible for releasing it
  • regular pointer: you are being given a handle to an object... or not (NULL)

For example:

class FooContainer
{
public:
  typedef std::vector<Foo> foos_t;

  foos_t::const_iterator fooById(int id) const; // natural right ?
};

But you expose some implementation detail here, you could perfectly create your own iterator class... but iterator usually means incrementable etc... or use a pointer

class FooContainer
{
public:
  const Foo* fooById(int id) const;
};

Possibly it will return NULL, which indicates a failure, or it will return a pointer to an object, for which you don't have to handle the memory.

Of course, you could also use a weak_ptr here (you get the expired method), however that would require using shared_ptr in the first place and you might not use them in your implementation.

Upvotes: 3

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

  1. Passing pointers to legacy APIs.
  2. Back-references in a reference-counted tree structure (or any cyclic situation, for that matter). This one is debatable, since you could use weak-refs.
  3. Iterating over an array.

There are also many cases where you could use smart pointers but may not want to, e.g.:

  1. Some small programs are designed to leak everything, because it just isn't worth the added complexity of figuring out how to clean up after yourself.
  2. Fine-grained batch algorithms such as parsers might allocate from a pre-allocated memory pool, and then just blow away the whole pool on completion. Having smart pointers into such a pool is usually pointless.

Upvotes: 11

stonemetal
stonemetal

Reputation: 6208

Depends on the smart pointer you use. std::auto_ptr is not compatible with STL containers.

Upvotes: 3

Ferruccio
Ferruccio

Reputation: 100748

If you have a situation where a raw pointer is cast to an intptr_t and back for some reason, it cannot be replaced by a smart pointer because the casting operation would lose any reference counting information contained in the smart pointer.

Upvotes: 1

jk.
jk.

Reputation: 14004

interaction with legacy code. if the api needs a raw pointer you need to provide a raw pointer even if once its in your code you wrap it in a smart pointer.

Upvotes: 2

anon
anon

Reputation:

An API that is going to be called from C, would be an obvious example.

Upvotes: 4

Related Questions