C#
  Home arrow C# arrow Page 2 - 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 - Functions


    (Page 2 of 6 )

    In any procedural programming language, you heavily rely on functions. Functions acquire resources, perform operations and then free the resources. Functions create resources on the stack when the function is being entered and might create resources there during its execution. As soon as we leave the function, the resources are thrown from the stack, destroying them.

    A typical function could look like this:

     void foo() {
      MyClass *pMyObj = new MyClass;
      /* perform some operations here */
      delete pMyObj;
    }

    The most obvious problem with pointers is the possible misbalancing of new and delete (every allocated resource has to be freed to prevent memory leaks). In this example, you see the memory allocated being freed just before we exit the function. But is this the only way we can exit this function? An exception might be thrown in the body of this function, causing us to leave it without ever reaching the delete statement. If we want to avoid causing the resulting memory (or other resources like files) to leak, we need to catch all exceptions:

     void foo() {
      try {
      MyClass *pMyObj = new MyClass;
      /* perform some operations here */
      }
      catch (…) {
       delete pMyObj;
       throw; // rethrow the exception
      }
      delete pMyObj;
     }

    You will most probably have found yourself writing code like this and know that this structure only makes the code more complex and more difficult to maintain--especially when we want to handle different types of exceptions differently. Sometimes you might find solutions that don’t need to rethrow the exception employ the following solution:

     void foo() {
      MyClass *pMyObj = NULL;
      try {
      *pMyObj = new MyClass;
       /* perform some operations here */
      }
      catch (std::exception const &ex) {
       (void)printf(“Exception! %s\n”, ex.what());
       goto cleanup;
    }
      catch (…) {
       (void)printf(“Unknown exception!\n”);
       goto cleanup;
    }
     cleapup:
      delete pMyObj;
    }

    Still, the whole exception mechanism was invented to make sure the stack unwinds when an error is faced (offering constructed objects the possibility of properly destructing); all because the goto statement completely ignores this. Looking upon the code above, it becomes clear that this mixture of try/catch and goto is not the right way to handle possible resource leaks when exceptions are thrown.

    Goto’s are bad and should never be used, unless you are 100% sure of what you are doing. This means that you are not jumping backwards and not out of a function; the goto label is nearby and there are no possible resource leaks when you jump.

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