Using new and delete operator

From WikiEducator
Jump to: navigation, search

Using New and Delete Operator:

Activity :Execute the following and analyze the run time error :
main ()

{
  int a[4]; // fixed sized array

 for(int i = 0 ; i <= 5 ; i++)

 {

    cout<< "\nInput";

    cin>>a[i];

    cout<< "\n a["<< i<<"] ="<<a[i];

 }

 getch();

}

When you execute this code you will find that the code will take 6 inputs and then it will terminate abnormally by either displaying message "Program terminated abnormally" or "Your CPU encountered illegal instruction". Can you say why it did happen? You see this happened because we have declared a fixed size array in line number 1 by writing int a[4]. This fixed size array is being allocated in 4 * 2 = 8 bytes of contiguous memory location. While our for loop statement in the program is iterating for 6 times. So after reading a 4 inputs the CPU step on to read and write that memory location which is not the part of the static array a[4]. So it causes a Run time error. This is one of the problems we face with Static arrays having fixed size; more problems may arise due to its static nature (try to find out some more errors and drawbacks). So we are in desperate need of an array variable whose size could be well defined during the run time of the program, not during the design time itself, as is done in the case of the example given above.

C++ provides us with a NEW operator to create fixed size array with a size defined during run time. This whole block is treated to be a static block of memory but created in a more dynamic way, asking the size of the array to be inputted from the user.
Let’s understand the concept by running the following code snippet: main()
{

 int *MyArray; // Line 1 :

int size ; 

cout<< “Define the size of the array you want to create”;

cin>> size; // Line 4

MyArray = new int[size];       // Line 5 : initializing pointer MyArray with an address.

for(int i = 0 ; i <= size-1 ; i++)

{

 cout<<"Input a value \n";

 cin>> MyArray[i];

}

for( i = 0  ; i <= size -1 ; i++)

{

 cout<< MyArray[i] << “\n”;}

 getch();

}

Observe the Line 1 , 4 , and 5 of the above program. In Line 1 we have declared a Pointer variable which will act as a address holder. In Line 4 we ask user to input desired number of array members (i,e : size ). Finally in Line 5 we acquire the required memory space in run-time, using the new operator by statement:

PointerPic5JPG.JPG

The Delete Operator :

The memory chunk allocated by new operator during run-time, assigned to a pointer remains reserved till you restart your machine. That means that if the above explained program is executed for 10 times then it will reserve 10 * size * 2 bytes in memory, and all these bytes would be reserved till you restart your machine. This reserved memory can’t be used by any other program, till we restart machine. Hence in some situations the scarcity of memory occurs. This scarcity is also termed as Memory Leakage. To avoid the memory leakage problem the programmer tries to release the reserved memory space just when he feels that no more the reservation is required. This de-allocation process could be done using delete operator.
Consider The statement :

 int * MyArray = new int [size];

In the above step since the pointer MyArray holds the base address of the memory chunk thrown to it by new operator in the R.H.S. So if somehow we are successful in making this pointer to forget this base address, we are done with our job of releasing the reserved memory space. This is exactly being done by the delete operator by using the following statement :

   delete MyArray ;   // delete the reference held by the pointer MyArray. 

PointerPic6.JPG