C#
  Home arrow C# arrow SatView: Pointer Perfect, part 3.5
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

SatView: Pointer Perfect, part 3.5
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 1
    2004-12-20

    Table of Contents:
  • SatView: Pointer Perfect, part 3.5
  • BOOST::SHARED_PTR.
  • SMART PIMPL
  • BOOST::WEAK_PTR

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    SatView: Pointer Perfect, part 3.5


    (Page 1 of 4 )

    In this article in our ongoing series on pointers, J. Nakamura talks about some additional smart pointers, their advantages and drawbacks, and when and where you would employ them.

    In the previous article we saw that the usage of pointers is not entirely trivial, if only proven by the numerous garbage collectors developed for today’s modern programming languages. The usage of smart pointers can help make the usage of pointers in C++ a lot safer, although the std::auto_ptr provided by the STL has some counter-intuitive properties.

    The Boost library (www.boost.org) provides additional smart pointers, all with their advantages and drawbacks. Still you will find that they will cover most of the situations where you would like to employ a smart pointer. I highly recommend you check Boost out, since it offers lots more than just smart pointers.

    In this article I will only try to motivate you to use these smart pointers. It is not my goal to provide a complete description of the interfaces… please go to www.boost.org for the latest documentation instead.

    BOOST::SCOPED_PTR.

    This is the simplest of all the smart pointers Boost offers. It basically behaves the same as const std::auto_ptr and its name makes its intention quite clear: it frees the allocated memory it owns when it goes out of scope (by leaving a function or by moving on to the next iteration of a loop for example), or by calling its reset() function.

    Being simple, the scoped_ptr comes without shared-ownership or transfer-of-ownership semantics. You should understand by now that because of this behavior, we cannot store a scoped_ptr in (STL) containers.

    The scoped_ptr is actually so simple that it doesn’t need any member variables and has therefore no more space overhead than that of a built-in pointer. The code example of the previous article can easily be adopted to the usage of a boost::scoped_ptr:

    void foo() {
    boost::scoped_ptr<MyClass> pMyObj(new MyClass);
    /* perform some operations here */
    }

    The main reason why you should prefer using this pointer over the const std::auto_ptr is that its name makes it clear when the pointer is being deleted. While the auto_ptr allows you to transfer ownership, the scoped_ptr strictly forbids it. It is also possible to free the object pointed at and store a new pointer in a scoped_ptr, while the equivalent usage of a const auto_ptr will prohibit you from doing this. The scoped pointer is pretty useful to use as a member of a class to ensure that the resource it is holding is freed when the object is destructed.

    scoped_ptr cannot correctly deal with a pointer to a dynamically allocated array. In one of the previous articles you saw that the new[] operator has to be matched with the delete[] operator, or otherwise the result would be undefined and most likely result in only the first object in the array being deleted. The scoped_ptr uses the delete operator in its destructor to free the object; thus it cannot correctly destruct dynamically allocated arrays.

    Use the boost::scoped_array to manage pointers to dynamically allocated arrays. Its behavior is like that of a scoped_ptr with the difference that the delete[] operator is used in its destructor and it doesn’t implement the operator* and operator-> , but the operator[] instead.

    More C# Articles
    More By J. Nakamura


     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek