Loading....
Coupon Accepted Successfully!

 

The new and delete Operators

So far, we have used the new and delete operators in short programs, but for applying them in huge applications, we need to understand them completely. A small mistake in syntax may cause a critical error and possibly, a corrupt memory heap. Before beginning a study on the memory heap in detail, let us repeat a few points regarding new and delete operators.
  1. The new operator not only creates an object but also allocates memory.
  2. The new operator allocates memory from the heap that is also called a free store.
  3. The object created and the memory allocated by using the new operator should be deleted and memory should be released by the delete operator; otherwise, such a mismatch operation may corrupt the heap or may crash the system. According to the ANSI standard, it is a valid outcome for this invalid operation, and the compiler should have routines to handle such errors.
  4. The delete operator not only destroys objects but also releases allocated memory.
  5. The new operator creates an object, and it remains in the memory until it is released using the delete operator. Sometimes, the object deleted using the delete operator remains in the memory.
  6. If we send a null pointer to the delete operator, it is secure. Using delete to zero has no result.
  7. The statement delete x does not destroy the pointer x. It destroys the object associated with it.
  8. Do not apply C functions such as malloc(), realloc(), or free() with new and delete operators. These functions are unfit for object-oriented techniques (Table).
  9. Do not destroy the pointer repetitively or more than once. First time, the object is destroyed and memory is released. If second time the same object is deleted, the object is sent to the destructor, and, no doubt, it will corrupt the heap.
  10. If the object created is not deleted, it occupies the memory unnecessarily. It is a good habit to destroy the object and release the system resources.
Table: Difference between new and malloc()
 

new Operator

malloc()

Creates objects

Allocates memory

Returns pointer of relevant type

Returns void pointer

It is possible to overload a new operator

malloc() cannot be overloaded

 

 

14.4 Write a program to allocate memory to store 3 integers. Use new and delete operators for allocating and de-allocating memory. Initialize and display the values.

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int i, *p;
p=&i;
p=new int[3];
*p=2; // first element
*(p+1)=3; // second element
*(p+2)=4; // third element
cout<<“Value Address”;
for (int x=0;x<3;x++)
cout<<endl<<*(p+x)<<“\t”<<(unsigned)(p+x);
delete []p;
return 0;

}

OUTPUT
Value Address
2 3350
3 3352
4 3354

Explanation:
In the above program, integer variable i and pointer *p are declared. The pointer p is initialized with the address of variable i, and by using the new operator memory for three integers is allocated. The pointer *p can hold three integers in successive memory locations. The pointer variable is initialized with numerical values. The for loop is used to display the contents of the pointer *p. The delete operator releases the memory.
 

14.5 Write a program to allocate memory to store 3 floats. Use new and delete operators for allocating and de-allocating memory. Initialize and display the values.

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

float i, *p;

p=&i;

p=new float[3];

for (int x=0;x<3;x++)

*(p+x)=x+3.11;

cout<<“Value Address”;

for ( x=0;x<3;x++)

cout<<endl<<*(p+x)<<“\t”<<(unsigned)(p+x);

delete []p;

return 0;

}

OUTPUT
Value Address
3.11 4698
4.11 4702
5.11 4706
 
Explanation: In this program, the memory for three float numbers is allocated to the float pointer p. The first for loop initializes the pointer p, and the second for loop displays the values on the screen. The delete operator releases the memory.
 

 

14.6 Write a program to allocate memory for two objects. Initialize and display the contents and de-allocate the memory.

#include<iostream.h>

#include<conio.h>

struct boy

{

char *name;

int age;

};

int main()

{

clrscr();

boy *p;

p=new boy[2];

p->name=”Rahul”;

p->age=20;

(p+1)->name=”Raj”;

(p+1)->age=21;

for (int x=0;x<2;x++)

{

cout<<“\nName:”<<(p+x)->name<<endl<<“Age:”<<(p+x)->age;

}

delete []p;

return 0;

}

OUTPUT
Name : Rahul
Age : 20
Name : Raj
Age : 21

 

Explanation: In the above program, structure boy is declared, and memory for two objects is allocated to the pointer p. The pointer p is initialized, and the first for loop displays the contents of the pointer on the screen. Finally, the delete operator deallocates the memory.
 

14.7 Write a program to use new and delete operators to create and destroy objects.

#include<iostream.h>

#include<string.h>

#include<conio.h>

class student

{

private :

char name[25];

int age;

public :

student()

{

strcpy(name,“”);

age=0;

}

student (char *s,int g)

{

strcpy (name,s);

age=g;

}

void assign (char *s, int g)

{

strcpy(name,s);

age=g;

}

void display()

{

cout<<endl<<name<<“\t” <<age;

}

~student()

{ cout<<endl<<“In destructor”;}

};

void main()

{

clrscr();

student *s;

s=new student ;

s->assign (“santosh”,24);
student *t;
t=new student (“Amit”,15);
s->display();
t->display();
delete s;
delete t;

}

OUTPUT
santosh 24
Amit 15
In destructor
In destructor

 


Explanation: In the above program, the class student has two data members of character and integer type. The class also has zero-argument, two-argument constructors, and member function display(). The constructor initializes the object, and the display() function displays data on the screen.

In function
main(), s and t are pointers to class student and using the new operator, memory is allocated and constructors are executed. The objects are initialized, and contents are displayed by calling display() function. The delete operator destroys the objects, and destructor() is executed after this.





Test Your Skills Now!
Take a Quiz now
Reviewer Name