Why the ctor and dtor are not getting invoked even though the memory is allocated or freed? What is actually happening here? Please share your thoughts.
#include<iostream>
#include<stdlib.h>
using namespace std;
class a{
public:
int i;
a() {cout<<"\n a ctor \n";}
~a(){cout<<"\n a dtor \n";}
};
main() {
a *ap = NULL;
ap = (a*)malloc(sizeof(a));
ap->i = 11;
cout<<ap->i<<"\n";
cout<<ap<<"\n";
free(ap); //does this actually work? Does this free the memory?
cout<<ap<<"\n";
ap = NULL;
cout<<ap;
}
does the above mean ctor and dtor are not useful or they are just useless?