当前位置:网站首页>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 是函数,可以重写(覆盖)。
边栏推荐
- . Net core stepping on the pit practice
- 微信小程序路由再次跳轉不觸發onload
- Rehabilitation type force deduction brush question notes D3
- [algorithm post interview] interview questions of a small factory
- The problem of Chinese garbled code in the vscode output box can be solved once for life
- [MySQL 8.0 does not support capitalization of table names - corresponding scheme]
- What is linting
- Vscode creates its own code template
- 时间很快,请多做有意义的事情
- About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
猜你喜欢
Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
Vant Weapp SwipeCell设置多个按钮
2. Addition and management of Oracle data files
Configuration method and configuration file of SolidWorks GB profile library
Qt项目中的日志库log4qt使用
Stack acwing 3302 Expression evaluation
GDB code debugging
Find the combination number acwing 888 Find the combination number IV
‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
随机推荐
Redis-02. Redis command
6-3 find the table length of the linked table
VLAN experiment
Spinningup drawing curve
Error: “MountVolume.SetUp failed for volume pvc 故障处理
Modnet matting model reproduction
Record of problems in ollvm compilation
Get class files and attributes by reflection
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
Ret2xx---- common CTF template proposition in PWN
7.Oracle-表结构
2048 project realization
[Gaode map POI stepping pit] amap Placesearch cannot be used
Integer to 8-bit binary explanation (including positive and negative numbers) scope of application -127~+127
Edge calculation data sorting
Orin 两种刷机方式
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
在本地搭建一个微服务集群环境,学习自动化部署
cgroup_ memcg
UIO driven framework