现在的位置: 首页 > 综合 > 正文

malloc/free与new/delete的区别

2013年06月28日 ⁄ 综合 ⁄ 共 310字 ⁄ 字号 评论关闭

1. malloc/free是C语言中的标准库函数, new/delete是C++中的运算符.

2. 若动态创建的对象是类类型, 用new时, 自动调用该对象的默认构造函数:

class Test
{
public:
    Test() {}
    ~Test() {}
};

int main()
{
    Test *t = new Test();
    
    /* do something */

    delete t;
    return 0;
}

用malloc的代码如下:

    Test *t = (Test *) malloc(sizeof(Test));
    
    /* do something */

    free(t);

参考:

1. http://apps.hi.baidu.com/share/detail/2945055

2. VS2010 MSDN

抱歉!评论已关闭.