Wednesday, March 28, 2018

Write a c++ program for new and delete operators for dynamic memory allocation.


AIM: Write  a c++ program for new and delete operators for dynamic memory allocation.
THEORY:
 Dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely mallocrealloccalloc and free.
The C++ programming language includes these functions for compatibility with C; however, the operators new and delete provide similar functionality and are recommended by that language's authors.
Many different implementations of the actual memory allocation mechanism, used by malloc, are available. Their performance varies in both execution time and required memory.

SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
            int *p=new int[3],k;
            for(k=0;k<3;k++)
            {
                        cout<<"enter a number:";
                        cin>>*p;
                        p++;
            }
            p-=3;
            cout<<"\n entered numbers with their address are:\n";
            for(k=0;k<3;k++)
            {
                        cout<<"\n \t"<<*p<<"t"<<p;
                        p++;
            }
            p-=3;
            delete p;
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home