Code Examples
  Home arrow Code Examples arrow SatView: Pointer Perfect, Part 2: Construc...
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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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? 
CODE EXAMPLES

SatView: Pointer Perfect, Part 2: Construction / Destruction
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2004-12-06

    Table of Contents:
  • SatView: Pointer Perfect, Part 2: Construction / Destruction
  • The This Pointer
  • Copying Pointers vs. Copying Memory
  • Pointer Pointers

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    SatView: Pointer Perfect, Part 2: Construction / Destruction


    (Page 1 of 4 )

    In this second article in our series about pointers, J. Nakamura explains malloc and free, and new and delete. He also examines the this pointer and discusses the difference between copying pointers and copying memory (and the hazards of shallow vs. deep copying). Then he points out the potential problems when crossing DLL boundaries, and finally takes us into the exotic(?) world of pointer pointers.

    When you use the address dereference operator for pointer creation as mentioned in part 1, you are using pointers to objects that were created on the stack. More often you want to create objects on the heap (since the stack has a limited size), at which time you will have to use malloc/free or new/delete respectively.

    The stack is that section of memory used for the storage of local variables and function parameters (among other things) and is used to keep track of function execution in your application. This means that upon leaving a function, all local variables created in that function will be thrown from the stack, i.e. destroyed.

    You can use the heap when you need large chunks of data (e.g. large arrays of objects) that you don’t want to store on the stack to prevent it from overflowing. Or maybe you would like to keep the data alive across function calls or maybe you want to delay the construction of large objects.

    Malloc & Free vs. New & Delete

    malloc & free originate from C, while new & delete only work with a C++ compiler. Here lies their most important difference as well: malloc & free don’t understand constructors and destructors.

    Lets take a look at the following two lines of code:

    MyObj *pObj1 = static_cast<MyObj*>( malloc ( sizeof ( MyObj ) ) );
    MyObj *pObj2 = new MyObj;

    The first line creates a pointer to a chunk of memory that is large enough to hold MyObj, but there was no MyObj object constructed in that piece of memory. The second line creates a pointer to memory where MyObj has been constructed.

    Now if you could somehow construct a MyObj object in the memory pObj1 points at, you would still have problems freeing that memory. Just as malloc doesn’t construct, free doesn’t destruct: you would end up with a memory leak.

    When you are using C-functions, you might run into situations where you are given the responsibility to clean up chunks of memory (when you are using strdup for example). Be careful to match free with malloc and delete with new, because the results will be disastrous when you start mixing them!

    Now take look at the following two lines of code:

      MyObj *pObjArray = new MyObj[100];
      /* some code here */
      delete pObjArray;

    What is wrong with this picture? Well, new MyObj[100] allocates enough memory for 100 MyObj objects and constructs them in that chunk of memory. The behavior of delete pObjArray is undefined, however. Most likely 99 of the 100 MyObj objects are not destructed! The proper way to delete allocated arrays on the heap is with the delete[] operator:

      delete[] pObjArray;

    More Code Examples Articles
    More By J. Nakamura


     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway