C#
  Home arrow C# arrow Page 4 - 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 - The meaning of ownership


    (Page 4 of 6 )

    Because the auto_ptr forces the object it contains to have exactly one owner, this means that no two auto_ptrs should be pointing to the same object at the same time. It is up to you to make sure that this doesn’t happen.

    So what happens when ownership is transferred? Look at the following example:

     std::auto_ptr<MyClass> pMyObj(new MyClass);
     std::auto_ptr<MyClass> pMyObj2 = pMyObj;
     /*or*/
     std::auto_ptr<MyClass> pMyObj3(pMyObj2);

    It means that when ownership is transferred from pMyObj to pMyObj2, whatever pMyObj2 was pointing to is freed (not a big deal in this case, but what happens when you assign it another auto_ptr again?), the pointer to the resource is copied from pMyObj to pMyObj2, and pMyObj no longer points to that resource. In fact, it is pointing to NULL!

    When you are using auto_ptr for making sure that resources are freed upon exiting a function, its usage is straightforward. Still, I recommend you make the object const, just to show that it is not your intention to allow its ownership to be transferred:

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

    You can still make changes to the object to which the pointer contained by pMyObj refers:

    pMyObj->myVar = 10;

    But the const modifier prohibits you from making changes to pMyObj itself:

    pMyObj = other_auto_ptr;

    This way you can make it clear to other coders (who might have to maintain your code later) that the resource pMyObj contains has to be freed upon exiting this function and that its ownership is not to be transferred. Why is this important, you ask? Well, simply because the auto_ptr violates the general behavior of initializations and assignments in programming languages.

    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 3 hosted by Hostway
    Stay green...Green IT