What is the difference between new and malloc()?
来源:互联网 发布:西方国家为什么不淘宝 编辑:程序博客网 时间:2024/06/05 04:13
I don't know how many times I had search for the difference between new and malloc on the Internet, and the former several times I may use the Baidu. I also got lots of different descriptions about what's the difference between new and malloc, I got an answer which may not be complete and I forgot it, then I search for it again and got another answer about it, until I found the question answered by Bjarne Stroustrup,in the link as follows:
class Circle : public Shape {
public:
Cicle(Point c, int r);
// no default constructor
// ...
};
class X {
public:
X(); // default constructor
// ...
};
void f(int n)
{
void* p1 = malloc(40);// allocate 40 (uninitialized) bytes
int* p2 = new int[10];// allocate 10 uninitialized ints
int* p3 = new int(10);// allocate 1 int initialized to 10
int* p4 = new int();// allocate 1 int initialized to 0
int* p4 = new int;// allocate 1 uninitialized int
Circle* pc1 = new Circle(Point(0,0),10); // allocate a Circle constructed
// with the specified argument
Circle* pc2 = new Circle;// error no default constructor
X* px1 = new X;// allocate a default constructed X
X* px2 = new X();// allocate a default constructed X
X* px2 = new X[10];// allocate 10 default constructed Xs
// ...
}
Note that when you specify a initializer using the "(value)" notation, you get initialization with that value. Unfortunately, you cannot specify that for an array. Often, a vector is a better alternative to a free-store-allocated array (e.g., consider exception safety).
Whenever you use malloc() you must consider initialization and convertion of the return pointer to a proper type. You will also have to consider if you got the number of bytes right for your use. There is no performance difference between malloc() and new when you take initialization into account.
malloc() reports memory exhaustion by returning 0. new reports allocation and initialization errors by throwing exceptions.
Objects created by new are destroyed by delete. Areas of memory allocated by malloc() are deallocated by free().
I wonder why many people won't share this link to those guys who search for the difference between new and malloc, I suffered a lot from the questions answered in shabby Chinese.
0 0
- What is the difference between new/delete and malloc/free?
- What is the difference between new and malloc()?
- What is the difference between malloc and new other than syntax?
- C++ Memory Management : What is the difference between malloc/free and new/delete?
- The difference between "malloc" and "new"
- What is the difference between test, [ and [[ ?
- What is the difference between << and += while injecting elements to Hash.new([])?
- what is the difference between the setTimeout and setInterval
- What is the difference between DHTML and the DOM?
- What is the difference between the mouseover and mouseenter events?
- What is the difference between AWT and SWT?
- What is the difference between User Control and Custom Control?
- What is the difference between an EXE and a DLL?
- What is the difference between XML and C or C ?
- What is the difference between SMOD and CMOD?
- what is the difference between little-endian and big-endian.
- What is the difference between procedural and nonprocedural language?
- What is the difference between a thread and a process?
- NSStringFromSelector
- Tair LDB基于Prefixkey的范围查找性能优化项目测试及完成总结报告
- 2014年百度实习生面试题及总结
- [基础]JPanel自由布局简介
- raspberrypihi之玩转pi的专用摄像头
- What is the difference between new and malloc()?
- MFC .ini文件读浮点数
- 关于EL自定义函数和如果其中参数是一个自定义的类的问题
- 单例模式的七种实现方式
- 年度总结—平凡路上的不平凡
- LeetCode-Balanced Binary Tree
- 将某年中的第几天的日期表示形式转化为某月某日的表示形式
- 机房收费系统之上下机
- 循环队列(java实现)