new and delete operator in C++ with examples
new and delete operator in C++:
new and delete operator in C++:- new operator is used to allocate memory location during program execution. This operator allocates memory for the specified object and returns its memory address (i.e. starting address of the allocated memory) to the pointer variable. The allocated memory has n name. the data into the allocated memory location is accessed with the reference of the pointer. The new operator can be used to allocate memory for simple variables, arrays, or user-defined objects etc.
The general syntax of new operator is as for follows:
Ptr= new data_type;
Where
Ptr: it represents the pointer to an object.
Data_type: it represents the data type. It may be the standard data type or name of a defined class or structure. It must be the same as type of pointer variable.
For example:
Float *p;
P=new float;
In these statements, the first statement declares pointer variable of float type. In the second statement, the new operator allocates memory of size 4 bytes to store float type data. The address of allocated memory is assigned to pointer ‘p’.
We can also allocate a consecutive block of memory to store same type of data such as an array. In this case, the memory address of first element of array is assigned to the pointer variable. For example, to allocate memory for n array of ten elements, the statements are written as:
Int *p;
P= new int[10];
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Delete operator:
Once memory is allocated with new operator, it cannot be de-allocated automatically. It the pointer (that points to the allocated memory) goes out scope, the allocated memory becomes unavailable.
The delete operator is used to de-allocated memory occupied by an object. The de-allocated memory can be used by operating system for other purposes.
The general syntax of delete operator to de-allocated memory occupied by an array is as follows:
Delete[] ptr;
Where
Ptr: it represents the pointer variable which holds the memory address of the object which is to deleted from memory.
Programming Example: How to use new and delete operator in C++ that allocates memory location for two integer values and computes the sum of these values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int *p1, *p2, sum; p1=new int; p2= new int; cout<<" Enter first value :"; cin>>*p1; cout<<" Enter second value :"; cin>>*p2; sum = *p1 + *p2; cout<<" Sum of values = "<<sum<<endl; delete p1; delete p2; } |
How to use new and delete operator in C++ that allocates memory location for an array of int type and find the largest value in the array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; int main() { float *p1, *p2, max; int n, i; cout<<" Enter the size of array :"; cin>>n; p1=new float[n]; p2=p1; cout<<" Enter "<<n<<" values in array\n"; for(i=0; i<n; i++) cin>>*p1++; i=0; p1=p2; max = *p1; while(i<n) { if(max<*p1) max= *p1; *p1++; i++; } cout<<" the largest value is :"<<max; delete p1; } |
How to use new and delete operator in C++ that allocates memory location for four integer values and store the addresses of allocated memory and find out the smallest value from the four values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> using namespace std; int main() { int *p[4], *pn1, *pn2, *pn3, *pn4, min, i; pn1= new int; cout<<" enter first value :"; cin>>*pn1; pn2 = new int; cout<<" enter second value :"; cin>>*pn2; pn3 =new int; cout<<" enter third value :"; cin>>*pn3; pn4= new int; cout<<" enter fourth value :"; cin>>*pn4; p[0] = pn1; p[1] = pn2; p[2] = pn3; p[3] = pn4; min= *p[0]; i = 1; while (i<=3) { if(min > *p[i]) min = *p[i]; i++; } cout<<" the smallest value is :"<<min; delete pn1; delete pn2; delete pn3; delete pn4; } |