C#
  Home arrow C# arrow Page 3 - Satview: Pointer Perfect, Part 3
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2004-12-13

    Table of Contents:
  • Satview: Pointer Perfect, Part 3
  • Functions
  • Smart pointers
  • The meaning of ownership
  • The auto_ptr is counter-intuitive
  • The concept of source and sink

  • 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 - Smart pointers


    (Page 3 of 6 )

    It is obvious why the implementation of a garbage collector takes a lot of the worries of memory management away from you. In C++ there is another alternative--a ‘smart pointer.’ A smart pointer behaves much like a built-in pointer, except that it automatically frees resources when they are destroyed. The concept of a smart pointer is essentially simple, though the many different ways we can implement this concept provides different behaviours and leaves us with much to investigate.

    When an object is declared locally in a function and thus on the stack, its destructor is automatically called when the object is thrown from the stack. If we were to create an object that can hold a built-in pointer for us, it can then call delete for us in its destructor. Our resource is automatically released and there is no need for the complex try/catch (or even worse goto) structure to make sure we are not leaking. You can see that this is as useful for freeing allocated memory as it is for guaranteeing the closure of opened files.

    The smart pointers we will be looking at can be used just like the built-in pointers we have been looking at in previous articles. This means that you can use the reference and pointer-to-member operators, but you are prohibited from using the dereference and arithmetic operators. Why this is will be discussed when we look at common pitfalls when using pointers.

    The Standard Library offers a smart pointer known as the auto_ptr. The auto_ptr serves as the owner of the resource it refers to and frees it automatically when it gets destroyed. Exactly the kind of thing we are looking for, right? Well there is one important caveat with the std::auto_ptr – a requirement of the std::auto_ptr is that its object can have only one owner. We will look at the consequences of this requirement, but first let’s rewrite previous example using an auto_ptr.

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

    There no longer is a need to worry about memory leaks in the face of exceptions, and we can even forget about including the delete statement! Great, isn’t it? Note that you can only initialise smart pointers through the constructor; the assignment operator has been reserved to only work with objects of the same (smart pointer) class, making the following statement invalid:

    std::auto_ptr<MyClass> pMyObj = new MyClass;

    More C# Articles
    More By J. Nakamura


     

    C# ARTICLES

    - 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#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT