Malloc与Free不调用构造函数与析构函数

例子:

#include "stdafx.h"
#include <new>
#include <iostream>
using namespace std;

class Obj
{
public:
    Obj()
    {
        cout << "constructor" << endl;
    };
    ~Obj()
    {
        cout << "destructor" << endl;
    };
};

void use_malloc_and_free(void)
{
    Obj* a = (Obj*)malloc(sizeof(Obj));
    free(a);
};

void use_new_and_free(void)
{
    Obj* a = new Obj;
    delete a;
};

int _tmain(int argc, _TCHAR* argv[])
{
    //use_malloc_and_free();
    //use_new_and_free();
printf(" "); return 0; }
原文地址:https://www.cnblogs.com/shuada/p/3557182.html