Smart Pointers

When I first started my ParaView project, I ignored vtkSmartPointer because using vtkSmartPointer made it very difficult for me to debug and understand the large number of classes in VTK and ParaView.

As I started my quest to be a better developer, and improving my understanding of C++ , i discovered the RAII idiom and smart pointers. Apparently if you use a smart pointer to a resource instead of a regular pointer, the compiler counts the number of references to the resource and deletes the allocated memory when the resource is no longer referenced.

Advertisements

About Alexis Chan

https://alexisylchan.wordpress.com/about/
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

2 Responses to Smart Pointers

    • Alexis Chan says:

      Yep, I first became comfortable with smart pointers when I used Boost smart pointers for an interview question. I was mentioning VTK’s implementation of smart pointers in the context of my ParaView project.

      When I wrote this blog post, I actually did not look into the implementation of vtkSmartPointer – that was bad. Your comment prompt me to look it up – apparently they do use reference counting, but the reference count is stored in the object, not the smart pointer: http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers

      I’ve never implemented a smart pointer or garbage collection, so I’m sure I’ve missed some nuances of the implementation, but this seems like a good place to start if I have the time for it : http://ootips.org/yonat/4dev/smart-pointers.html#Which

      Today, a friend also mentioned that C# has smart pointers for memory management even though it has garbage collection – apparently the implementation is different from C++ – STL or Boost.

      If you do find anything that summarizes the different implementation of smart pointers, do let me know. Thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s