当前位置:网站首页>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。
边栏推荐
- uTools
- 6-2 vulnerability exploitation - inevitable problems of FTP
- Android: the kotlin language uses grendao3, a cross platform app development framework
- Laravel artisan 常用命令
- Learn basic K-line diagram knowledge in three minutes
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- 1217 supermarket coin processor
- Liteos learning - first knowledge of development environment
- 开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
- 笔者更加愿意将产业互联网看成是一个比消费互联网要丰富得多的概念
猜你喜欢

企业应该选择无服务器计算吗?

Docker installing Oracle_ 11g

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

Docker安装Oracle_11g

Penser au jeu 15: penser au service complet et au sous - service

遷移雲計算工作負載的四個基本策略

What is AQS and its principle

Self drawing of menu items and CListBox items

It's already 30. Can you learn programming from scratch?

Learn C language from scratch day 025 (maze)
随机推荐
Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development
Learning note 24 - multi sensor post fusion technology
6-2漏洞利用-ftp不可避免的问题
Have you stepped on the nine common pits in the e-commerce system?
只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
II Basic structure of radio energy transmission system
CEPH buffer yyds dry inventory
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
Hcip day 14 (MPLS protocol)
Basic concepts of machine learning
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
遊戲思考15:全區全服和分區分服的思考
[disease detection] realize lung cancer detection system based on BP neural network, including GUI interface
No converter found for return value of type: class
GL Studio 5 安装与体验
The concept and application of Cartland number
学习笔记24--多传感器后融合技术
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions