当前位置:网站首页>new和malloc的区别
new和malloc的区别
2022-07-02 01:37:00 【wrdoct】
new和malloc区别(8个)
(1)new和delete是C++的关键字/运算符,malloc与free是c++/c语言的标准函数。
(2)malloc需要显式地指定分配的内存大小,new不需要。
(3)new操作符从自由存储区上为对象动态分配内存空间,而malloc函数从堆上动态分配内存。
【注】凡是通过new操作符进行内存申请,该内存即为自由存储区。
(4)new操作符内存分配成功时,返回对象类型,无须进行类型转换,故new是符合类型安全性的操作符;malloc返回void*,需要通过强制类型转换将void*指针转换成我们需要的类型。
(5)new操作符内存分配失败时,抛出bad_alloc异常;malloc内存分配失败时返回NULL。
(6)new操作符有构造函数和析构函数,在开辟空间的同时,会调用自定义对象的构造函数来完成初始化;malloc只会开辟空间。
(7)malloc分配空间后,可以通过realloc扩张内存;new操作符则不能进行再次扩张内存的操作。
(8)new相对malloc效率要低,因为new的底层封装了malloc。
边栏推荐
- How can I batch produce the same title for the video?
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
- Altium designer measure distance (ctrl+m)
- matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- Three core problems of concurrent programming
- Data visualization in medical and healthcare applications
- [IVX junior engineer training course 10 papers to get certificates] 0708 news page production
- This is the form of the K-line diagram (pithy formula)
- No converter found for return value of type: class
猜你喜欢

The concept and application of Cartland number

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages

LeetCode、3无重复最长子序列

Feature extraction and detection 16 brisk feature detection and matching
![[IVX junior engineer training course 10 papers to get certificates] 0708 news page production](/img/ad/a1cb672d2913b6befd6d8779c993ec.jpg)
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production

GL Studio 5 安装与体验
![[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility](/img/8b/e51867cfe9d200ac385e1d1f01e4b3.jpg)
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility

We should make clear the branch prediction

Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model

Exclusive delivery of secret script move disassembly (the first time)
随机推荐
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
学习笔记24--多传感器后融合技术
Part 29 supplement (XXIX) basis of ECMAScript
Altium designer measure distance (ctrl+m)
Liteos learning - first knowledge of development environment
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
The role of artificial intelligence in network security
A problem about function template specialization
PR second training
游戏思考15:全区全服和分区分服的思考
1217 supermarket coin processor
About asp Net core uses a small detail of datetime date type parameter
Six lessons to be learned for the successful implementation of edge coding
Have you stepped on the nine common pits in the e-commerce system?
Should enterprises choose server free computing?
MySQL application day02
No converter found for return value of type: class
I Brief introduction of radio energy transmission technology
Basic usage of three JS high-order functions --filter---map---reduce
The concept and application of Cartland number