Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

New &amp Delete

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Describe new operator and delete operator.

new and delete operators are provided by C++ for runtime memory management. They
are used for dynamic allocation and freeing of memory while a program is running.

The new operator allocates memory and returns a pointer to the start of it. The delete
operator frees memory previously allocated using new. The general form of using them
is:

p_var = new type;


delete p_var;
new allocates memory on the heap. If there is insufficient memory, then new will fail and
a bad_alloc exception will be generated. The program should handle this exception.

Consider following program:


#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try
{
p = new int; //dynamic allocation of memory
}
catch (bad_alloc x)
{
cout << “Memory allocation failed”;
return 1;
}
*p = 100;
cout <<”P has value”<<*p;
delete p; //free the dynamically allocated memory
return 0;
}

What is Dynamic memory management for array?


Using the new and delete operators, we can create arrays at runtime by dynamic memory
allocation. The general form for doing this is:

p_var = new array_type[size];


size specifies the no of elements in the array
To free an array we use:
delete[ ]p_var; // the [ ] tells delete that an array is being freed.

Consider following program:


#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p, i;
try
{
p = new int(10); //allocate array of 10 integers
}
catch (bad_alloc x)
{
cout << “Memory allocation failed”;
return 1;
}
for (i = 0; i < 10; i++)
p[i] = i;
for (i = 0; i < 10; i++)
cout <<p[i]<<”\n”;
delete [ ] p; //free the array
return 0;
}

Question - Distinguish between new and malloc and delete and free().

Answer
Delete is assocated with new and free(0 is associated with malloc()

New
Its an operator
delete is associated with new
It creates an object
It throws exceptions if memory is unavailable
Operator new can be overloaded
You can state the number of objects to be created.

malloc()
It’s a function
free() is associated with malloc()
It does not create objects
It returns NULL
This cannot be overloaded
You need to specify the number of bytes to be allocated.
What is the difference between realloc() and free()?

An existing block of memory which was allocated by malloc() subroutine, will be freed
by free() subroutine. In case, an invalid pointer parameter is passed, unexpected results
will occur. If the parameter is a null pointer, then no action will occur.

Where as the realloc() subroutine allows the developer to change the block size of the
memory which was pointed to by the pointer parameter, to a specified bytes size through
size parameter and a new pointer to the block is returned. The pointer parameter specified
must have been created by using malloc(),calloc() or realloc() sub routines and should not
deallocated with realloc() or free() subroutines. If the pointer parameter is a null pointer,
then no action will occur.

You might also like