Complete the following code by deallocating the memory Int p

Complete the following code by deallocating the memory. Int *ptr1; int *ptr2; ptr1 = new Int; ptr2 = new intl[10]; *ptr1 = 10;

Solution

Dear Student,

The memory in c++ is created by a operator call new.

And the memory is deallocated by a operator called delete.

Below is the complete program...

#include<iostream>
using namespace std;
int main()
{
    int *ptr1;
    int *ptr2;
    ptr1 = new int;            //allocate memory for the variable
    ptr2= new int[10];      // allocate memory for integer variable which size is 10.
    *ptr1=10;                   // assignment of value to pointer
    cout << *ptr1 << endl;
    delete ptr1;                //Deallocate memory allocated for ptr1
    delete ptr2;                //Deallocate memory allocated for ptr2
    return 0;
}

Output : 10

Note- The above program is tested on g++ compiler under ubuntu 14.04 system.

***** Dear Student if you are satisfied with the answer kindly provide your valubale feedback******

 Complete the following code by deallocating the memory. Int *ptr1; int *ptr2; ptr1 = new Int; ptr2 = new intl[10]; *ptr1 = 10;SolutionDear Student, The memory

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site