newdelete 与 mallocfree的区别

    xiaoxiao2021-03-25  81

    1.new申请空间比malloc简单:

    int *p1 = (int*)malloc(sizeof(int)*length); int *p2 = new int[length];

    2.new/delete实现了动态内存分配:

       new将调用constructor而malloc不能;delete将调用destructor,而free不能。

    LinkList *p1 = new LinkList(); delete p1; LinkList *p2 =(LinkList*)malloc(sizeof(LinkList)); p2->LinkList(); ~LinkList(); free(p2);

    3.malloc/free是函数,要库文件支持;new/delete是运算符,则不要。

    #include<stdlib.h>
    转载请注明原文地址: https://ju.6miu.com/read-19091.html

    最新回复(0)