Click here to Skip to main content
13,796,494 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

31.4K views
6 bookmarked
Posted 25 Mar 2009
Licenced CPOL

C++ Memory Clean Up

, 25 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Demonstrating one of the C++ features , the dynamic allocation and de-allocation of arrays using pointers

Introduction

In this article I am going to demonstrate the dynamic allocation and de-allocation of a two dimensional array using pointers.One of the great features of C++ is the memory management,it enables us to allocate the memory and clean it up when we finish using it. While in the languages that compiles to an intermediate language like Java and C# , the garbage collect is not leaved to the developer it is done automatically when the object allocated in the memory is not in use. But this is done by the operating system and may leave some allocated memory for a while. 

The Code

Prepare the main.cpp file by including iostream library and using the namespace std

#include <iostream>

using namespace std;

void main(void)
{

}

In function main() declare an int variable to store the size of the array and declare a pointer to a pointer to a float (it may be a little bit confusing ...but imagine u declared a pointer to a float then you decalred a pointer to point to that pointer ... ).

int size = 7000;

float** MainArray = NULL;
MainArray = new float*[size];

cout <<"Allocating..."<<endl;
for(int i = 0 ; i < size ; i++)
{	
   float* SubArray = NULL;
   SubArray = new float[size];	  
   
   for(int j = 0 ; j < size ; j++)
   {
	SubArray[j] = (float)j;
   }

   MainArray[i] = SubArray;
}

The variable MainArray supposed to point to the SubArray and the SubArray supposed to point to a float values hence we get a two dimensional array.

After we allocated the array (the developer may do some operations on it or use it for storing data etc.. ) we are now concerned with de-allocate it and this is done using the key word delete and provide it with a pointer to delete it then assign it to NULL like in the next code block.

int key = 1 ;


  cout << "Press 0 then Enter to clean up the memory" << endl;
  cin >> key ;

  if(key == 0)
  {
      for(int i = 0 ; i < size ; i++)
      {
         delete[]  MainArray[i];
         MainArray[i] = NULL;
      }

      delete[] MainArray;
      MainArray = NULL;
      cout << "Memory cleared" << endl;

  }


  system("PAUSE");

The loop iterates throw the main array and delete every pointer to a float then after finshing the  main array pointer is deleted.

You can see the effect of the clean up by opening the Process tab in the task manager and notice the memory usage of MemoryCleanUp.exe before and after hitting 0 and Enter do de-allocate the array,thats it ..very simple

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fady Aladdin
Software Developer
Egypt Egypt
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Yongsuk Jang17-Nov-09 4:13
memberYongsuk Jang17-Nov-09 4:13 
GeneralMy vote of 1 Pin
Emilio Garavaglia25-Mar-09 22:34
memberEmilio Garavaglia25-Mar-09 22:34 
GeneralMy vote of 1 Pin
milan161225-Mar-09 10:20
membermilan161225-Mar-09 10:20 
GeneralMy vote of 1 Pin
adrian_00725-Mar-09 9:56
memberadrian_00725-Mar-09 9:56 
GeneralAutomatic garbage collection classes e.g scoped_array Pin
Eanna M-annion25-Mar-09 8:44
memberEanna M-annion25-Mar-09 8:44 
GeneralNice and simple, but sadly nothing that you couldn't read in a text book already. Pin
Mike Diack25-Mar-09 7:45
memberMike Diack25-Mar-09 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.181207.3 | Last Updated 25 Mar 2009
Article Copyright 2009 by Fady Aladdin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid