Difference Between new vs malloc() and delete vs free() in C++
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Program for difference between new and malloc() , delete free()
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Test
{
public:
Test()
{
cout<<"\nHello I am constructor of class Test\n";
}
~Test()
{
cout<<"\nHello I am destructor of class Test\n";
}
};
// class MyClass
// {
// public:
// MyClass()
// {
// cout<<"\nHello I am constructor of class Myclass\n";
// }
// ~MyClass()
// {
// cout<<"\nHello I am destructor of class Myclass\n";
// }
// };
int main()
{
clrscr();
Test *T=new Test; // using new
cout<<"\nDynamic memory using new operator";
delete T; // using delete
cout<<"\nDynamic memory remove using delete operator";
Test *T1=(Test*)malloc(sizeof (Test));
cout<<"\nDynamic memory using malloc function";
free(T1);
cout<<"\nDynamic memory remove using free function";
}
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

