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("\n"); return 0; }

优质内容筛选与推荐>>
1、C++ 容器之 list的使用
2、快速排序算法
3、字符串基本操作
4、《我是一只IT小小鸟》【1】
5、HNUSTOJ-1698 送外卖(TSP问题 + 状态压缩DP)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号