当前位置:网站首页>new和malloc的区别
new和malloc的区别
2022-07-05 06:36:00 【一只嵌入式爱好者】
new和malloc的区别
1、new从自由存储区上分配内存,malloc从堆上分配内存。自由存储区是C++基于new操作符的一个抽象概念,凡是通过new操作符进行内存申请,该内存即为自由存储区。而堆是操作系统中的术语,是操作系统所维护的一块特殊内存,用于程序的内存动态分配。那么自由存储区是否能够是堆(问题等价于new是否能在堆上动态分配内存),这取决于operator new 的实现细节。自由存储区不仅可以是堆,还可以是静态存储区,这都看operator new在哪里为对象分配内存。
2、new、delete 返回的是某种数据类型指针;malloc、free 返回的是 void 指针。
3、使用new操作符申请内存分配时无须指定内存块的大小,编译器会根据类型信息自行计算;使用malloc则需要显式地指出所需内存的尺寸。
4、new 可以调用对象的构造函数,对应的 delete 调用相应的析构函数;malloc 仅仅分配内存,free 仅仅回收内存,并不执行构造和析构函数。在new一个对象的时候,首先会调用malloc为对象分配内存空间,然后调用对象的构造函数。delete会调用对象的析构函数,然后调用free回收内存。
5、new、delete 是操作符,可以重载;malloc、free 是函数,可以重写(覆盖)。
边栏推荐
- cgroup_ memcg
- LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
- testing framework
- Genesis builds a new generation of credit system
- Ret2xx---- common CTF template proposition in PWN
- Modnet matting model reproduction
- 代码中的英语全部
- H5 module suspension drag effect
- GDB code debugging
- Vscode editor
猜你喜欢

Vant Weapp SwipeCell设置多个按钮
![[algorithm post interview] interview questions of a small factory](/img/62/6e330b1eba38f2dc67b21a10f0e2a8.jpg)
[algorithm post interview] interview questions of a small factory

UTC, GPS time and Tai

VLAN experiment

Client use of Argo CD installation

All English in the code

inux摄像头(mipi接口)简要说明

.net core踩坑实践
![[MySQL 8.0 does not support capitalization of table names - corresponding scheme]](/img/ea/a1e0722c43f56aff3e79f95c99ba8a.png)
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]

The “mode“ argument must be integer. Received an instance of Object
随机推荐
Integer to 8-bit binary explanation (including positive and negative numbers) scope of application -127~+127
In C language, int a= 'R'
'mongoexport 'is not an internal or external command, nor is it a runnable program or batch file.
Rehabilitation type force deduction brush question notes D2
RecyclerView的应用
Sum of two numbers, the numbers in the array are converted to decimal, added, and output inversely
Xavier CPU & GPU 高负载功耗测试
ethtool 原理介绍和解决网卡丢包排查思路(附ethtool源码下载)
H5内嵌App适配暗黑模式
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
微信小程序路由再次跳轉不觸發onload
Stack acwing 3302 Expression evaluation
PR automatically moves forward after deleting clips
Modnet matting model reproduction
Game theory acwing 891 Nim games
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
Knapsack problem acwing 9 Group knapsack problem
Skywalking全部
Instruction execution time
3. Oracle control file management