Jump to content
  • Advertisement
Sign in to follow this  
CoffeeMug

Using smart pointers

This topic is 5473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you use smart pointers? Do you actually pass them around as function arguments like so:
void MyClass::f(smart_ptr<SomeClass> a);
Do you pass them as references?
void MyClass::f(smart_ptr<SomeClass> &a);
Or do you simply pass the class directly and use the smart pointer internally?
void MyClass::f(SomeClass *a);
{
    m_SomeClass = a;
}
class MyClass
{
    smart_ptr<SomeClass> m_SomeClass;
};
I've never used smart pointers before and getting accustomed to them is a little hard as I'm not always sure how to properly use them.

Share this post


Link to post
Share on other sites
Advertisement
Smart pointers are supposed to maintain the semantics of pointers. In other words, use them just as you would a pointer. Choosing whether to pass them by reference (yes, passing a pointer by reference), etc is a function of the specifics of the use case.

Share this post


Link to post
Share on other sites
Another words I would generally use the very first case I presented. Ok. But what is the benefit of that over the last case? There is an obvious drawback of making a bunch of copies, but what do I gain (compared to the third case)? Aside from some sort of a uniform syntax, I can't really see the benefit.

I realize that the performance hit of copying smart pointers is more often than not negligible, but I'm still wondering how people use them.

Share this post


Link to post
Share on other sites
Your third case simply won't work if your smart pointers aren't intrusive, ie when the reference count isn't stored in the instance itself (boost::shared_ptr is an example of this). The instant you grab the pointer from this type of smart pointer and create another smart pointer to it, you now have 2 separate reference counts which is a Very Bad Thing(tm).

Also, consider multithreading environments. Even if your smart pointer is intrusive, if you start passing around that normal pointer for even an instant you run the risk of the reference count being decremented to 0 in another thread resulting in the object being deleted while you're still holding a normal pointer to it.

As for the second case, I'm guessing you actually meant 'void MyClass::f(const smart_ptr<SomeClass> &a);', in which case your probably only saving copying 4 bytes of data at the cost of having to dereference it.

Share this post


Link to post
Share on other sites
Another question that begs to be asked is whether people typedef smart_ptr<SomeClass> to be, say, SomeClassPtr or do they just use smart_ptr<SomeClass> in their code.

Share this post


Link to post
Share on other sites
I usually prefer to typedef them, i think it makes the code clean and easy to read. But thats just me.

I know many others do it the typedef way as well. I guess what it comes down to, is a matter of preferance.

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Your third case simply won't work if your smart pointers aren't intrusive, ie when the reference count isn't stored in the instance itself (boost::shared_ptr is an example of this). The instant you grab the pointer from this type of smart pointer and create another smart pointer to it, you now have 2 separate reference counts which is a Very Bad Thing(tm).

Also, consider multithreading environments. Even if your smart pointer is intrusive, if you start passing around that normal pointer for even an instant you run the risk of the reference count being decremented to 0 in another thread resulting in the object being deleted while you're still holding a normal pointer to it.


I'd just like to reenforce this point, I did the whole 'assign pointer to shared_ptr when it was already held by another shared_ptr' and spent 2 days trying to work out why my memory was going AWOL when it was held by a shared_ptr... boy did I feel silly [sad]

and I prefer to typedef, it keeps things cleaner and easier to read

Share this post


Link to post
Share on other sites
Quote:
Original post by Allmight
I usually prefer to typedef them, i think it makes the code clean and easy to read. But thats just me.

I know many others do it the typedef way as well. I guess what it comes down to, is a matter of preferance.

typedef-ing is definetly a good practice for clean easy to read code. Do it. Do it now! :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!