Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » C/C++/C#
 C++: Destructor
Pages: [1]   Go Down
  Print  
Author Topic: C++: Destructor  (Read 3272 times)
Taruna
Elite Member
*****



Karma: 13
Offline Offline

Posts: 845

Hi ALL


View Profile
C++: Destructor
« Posted: December 27, 2006, 12:46:46 AM »


The implementation of C++'s Destructor efficiently

When writting a destructor it is important to understand why and what before just writting it.
*a structure and pointer recipe are on the way to explain anything thing complex here.
The point or rather to not point. A destructor removes pointers in the case of manually watching your memory leak. This method is automaticlally called whenever an object loses scope.

Scope - part of the code (eg, method, class, etc) in which a variable or parameter can be accessed.

thus, it takes care of all local and dynamic variables for us, preventing memory leak if it is written properly.

*The example will be written as if it were inside a class which has a structure within it implementing a linked list by the pointer:
Next* next;
which of course the struct is called Next.

Destructor Syntax:

Code:
~Destructor() {}

-A destructor never takes any parameters, it's only parameter is the implied one accesable by this.
-always starts with a ~, tild or tilda as it in known

now within a structure iplementing a linked list this becomes the most crucial part of your code. The structure not only has things pointing to it, but it is pointing to other objects of it's own type. These all need to be handled dynamically, but how?
*I said earlier that C++ will call this for every object, but we have no way to be sure that it will be efficient and call them in the reverse order they are pointing in! if we were to delete the lead pointer only it is possible that C++ would accept this as a completed destruction, which is not true!!
As long as the other pointers and objects are linked, the memory is not freed and we have memory leak.

So we need to delete all pointers below the current this object to insure a proper destruciton. If all goes well each object will only destroy one pointer, but we cannot be sure of this.

This will require a loop and a temp variable (which we will also destroy at the end of the destructor)

Complete Linked List Destructor:
*within the Next structure, so it gets the title of it's class or struct.

Code:
~Next(){ 
   Next* n = this->next;
   Next* nn = NULL;
   while(n){
      nn = n->next;
      delete n;
      n = nn;
   }
   nn = NULL;
   n = NULL;
}

-This loop is based on n which is true as long as n is not NULL (eg there is another Next object in the linked list)
this code itterates through the linked list objects and deletes the pointers 1 by 1, once finished with all pointers below itself setting the temporary variables to NULL, which will allow the GC or Garbage Collector to destroy them.

This example is meant to improve your knowledge of memory leak, the less leakage there is in your own programs the better, because we all know that Operating Systems are not leak free, so the more help we can give them, the better!!

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.102 seconds with 24 queries.