Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
4 captures
26 Sep 2011 - 29 Dec 2011
NovDECJan
Previous capture29Next capture
201020112012
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20111229234521/http://www.codeproject.com:80/KB/cpp/cppMemory.aspx
Click here to Skip to main content
8,372,394 members and growing!
EmailPassword Lost password?
Home
Search within:




Licence CPOL
First Posted 25 Mar 2009
Views 9,730
Bookmarked 5 times

C++ Memory Clean Up

Demonstrating one of the C++ features , the dynamic allocation and de-allocation of arrays using pointers
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
6
  1.36 (5 votes)
4 votes, 80.0%
1

2
1 vote, 20.0%
3

4

5
1.36/5 - 5 votes
μ 1.36, σa 1.57 [?]
Sponsored Links

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>usingnamespace 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 =newfloat*[size];cout<<"Allocating..."<<endl;for(int i =0 ; i< size ; i++){float* SubArray = NULL;   SubArray =newfloat[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 underThe Code Project Open License (CPOL)

About the Author

Fady Aladdin

Software Developer

Egypt Egypt

Member


loading...
Sign Up to vote  PoorExcellent
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 RefreshFirstPrevNext
GeneralMy vote of 1memberJang Yong Suk4:13 17 Nov '09  
GeneralMy vote of 1member22:34 25 Mar '09  
GeneralMy vote of 1membermilan161210:20 25 Mar '09  
GeneralMy vote of 1memberadrian_0079:56 25 Mar '09  
GeneralAutomatic garbage collection classes e.g scoped_arraymemberEnda Mannion8:44 25 Mar '09  
GeneralNice and simple, but sadly nothing that you couldn't read in a text book already.memberMike Diack7:45 25 Mar '09  
Last Visit: 19:00 31 Dec '99     Last Update: 13:45 29 Dec '111

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

Permalink |Advertise |Privacy |Mobile
Web04 |2.5.111208.1 |Last Updated 25 Mar 2009
Article Copyright 2009 by Fady Aladdin
Everything elseCopyright ©CodeProject, 1999-2011
Terms of Use
Layout:fixed|fluid

See Also...
The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp