What is the difference between new/delete and malloc/free?
Related (duplicate?): In what cases do I use malloc vs
This code for use of delete keyword or free function. But when create a pointer object using 'malloc' or 'new' and deallocate object memory using delete even that object pointer can be call function in the class. After that use free instead of delete then also it works after free statement , but when use both then only pointer object can't call to function in class.. the code is as follows :
#include<iostream>
using namespace std;
class ABC{
public: ABC(){
    cout<<"Hello"<<endl;
  }
  void disp(){
    cout<<"Hi\n";
  }
};
int main(){
ABC* b=(ABC*)malloc(sizeof(ABC));
int* q = new int[20];
ABC *a=new ABC();
b->disp();
cout<<b<<endl;
free(b);
delete b;
//a=NULL;
b->disp();
ABC();
cout<<b;
return 0;
}
output :
Hello Hi 0x2abfef37cc20
In C++ new/delete call the Constructor/Destructor accordingly.
malloc/free simply allocate memory from the heap. new/delete allocate memory as well.