malloc与new的区别

malloc与new基本区别

1.malloc和free 是c语言的库函数,new和delete是c++运算符;

2.malloc和free只是开辟和释放空间,new和delete除了开辟和释放空间还能构造对象(调用构造函数);

例子:

A *p = (A*)malloc(sizeof(A));

A *p = new A;

delete p;

int *p1 = new int;

*p1 = 5;

int *p2 = new int[5];

A *p3 = new A[5];

delete []p3;


m_str = new char[strlen(str)+1];

delete []m_str;

m_str = (char*)malloc(strlren(str)+1);

free(m_str);

m_str = NULL;

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页