Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
C And C Plus Plus Programming > using vector instead of arrays to prevent most of memory leaks

Using vector instead of arrays to prevent most of memory leaks

Most of beginners define arrays of limited size such as:

int array[100]; // array that can accomodate 100 integer values


These are ok, but your requirement may be different, and you want your application to determines the size of array at runtime, so if your requirement is to use 10 elements only, that means 90 are unused but memory is still allocated for them while application is running. On other hand your application requires 110 elements to be saved, the last 10 will be unallocated spaces if array size is 100, and access to unallocated memory address may result in applciation crash and/or system level damage.

The other way used by moderate and advanced level users is allocate memory spaces at runtime, but sometimes programmer leaves flaws by leaving memory leaks in code by forgetting to free the allocated memory, which overall damages the application even it is best structured, and also destabilizes the operating system. It mostly happens when application is often allocating and deallocating memory spaces. This kind of method is usually done two way, first is plain C way using traditonal memory allocation functions malloc and free, as:

int *array = (int*) malloc(sizeof(int)*100); //GlobalAlloc() win32 specific
free(array); //GlobalFree() win32 specific


and second way is the C++ way using new and delete operators:

int *array = new int[100];
delete [] array;


Now, there is one more way to do all this stuff without worrying about the allocation and deallocation, and prevent the use of static/constant sized arrays.


What is that 3rd way?

The way we will study is use of vector or list template classes from Standard Template Library (STL). Generally template programming seems hard to learn, but believe me, it is matter of understanding the template driven programming, which makes life easier, create some template class/function and use it for any data type (doesn't sound great, obviously yes).

Well, in this document I will introduce you to a class called 'vetor' one among many STL classes.

Take a close look at following code and enclosed comments.

#include <iostream>
#include <vector>

using namespace std;

int main() {
//define myarray as vector so which can shrink and grow at runtime
vector <int> myarray; //create vector object for type int

//add new values to vector object
myarray.push_back(10);
myarray.push_back(20);
myarray.push_back(30);
myarray.push_back(40);

//print 3 rd element
cout << myarray[2] << "\n";

myarray.clear(); //clear it at certain point to delete all existing items

cout << myarray.size(); //obtain and print number of elements, will print 0

system("pause");
return 0;
}


Whenever using precreated tempalate classes we define objects as:
template_class <template_required_arguments> our_variable_name;

so, is in our pogram:
vector <int> myarray;

Then we have some function push_back of class vector that we called to add new value to our object myarray.

At the end, in main program, we printed out value of item No.3 (zero based index so 2 means item 3, 0 means item 1, and so on), by using myarray as simple integer array. So, it is clear that we can easily use the vector class based objects as simple arrays.