当前位置:网站首页>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。
边栏推荐
- Hcip day 14 (MPLS protocol)
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
- MPLS experiment operation
- 学习笔记3--高精度地图关键技术(上)
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
- Electronic Association C language level 1 33, odd even number judgment
- Convolutional neural network (including code and corresponding diagram)
- 10 minutes to get started quickly composition API (setup syntax sugar writing method)
- II Basic structure of radio energy transmission system
- Unity AssetBundle subcontracting
猜你喜欢

GL Studio 5 installation and experience

SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5

The role of artificial intelligence in network security

Four basic strategies for migrating cloud computing workloads

MySQL application day02

Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
![[Floyd] post disaster reconstruction](/img/7a/f72c7781ef148212c870a56fb9a607.jpg)
[Floyd] post disaster reconstruction

Edge computing accelerates live video scenes: clearer, smoother, and more real-time

Docker安装Oracle_11g

Principle of finding combinatorial number and template code
随机推荐
The role of artificial intelligence in network security
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
[Chongqing Guangdong education] Tianshui Normal University universe exploration reference
并发编程的三大核心问题
学习笔记25--多传感器前融合技术
[IVX junior engineer training course 10 papers] 06 database and services
[Maya] the error of importing Maya into Metahuman
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
Brief introduction to the development of mobile network
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Design and implementation of radio energy transmission system
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Altium designer measure distance (ctrl+m)
学习笔记3--高精度地图关键技术(上)
机器学习基本概念
Penser au jeu 15: penser au service complet et au sous - service
电商系统中常见的9大坑,你踩过没?