当前位置:网站首页>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踩坑实践
- How to answer when you encounter a jet on CSDN?
- Ffmpeg build download (including old version)
- Package webapp or H5 pages into apps
- The “mode“ argument must be integer. Received an instance of Object
- Cloud native related technology learning
- CGroup CPU group source code analysis
- Find the combination number acwing 887 Find combination number III
- Relevant information of National Natural Science Foundation of China
- H5 module suspension drag effect
猜你喜欢

Build a microservice cluster environment locally and learn to deploy automatically

Client use of Argo CD installation

Redis-01. First meet redis

.net core踩坑实践

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

摄像头的MIPI接口、DVP接口和CSI接口

Paper reading report
![[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]

. Net core stepping on the pit practice

Utf8 encoding
随机推荐
How to correctly ask questions in CSDN Q & A
2022-5-the fourth week daily
NVM Downloading npm version 6.7.0... Error
The differences and connections among cookies, sessions, JWT, and tokens
Genesis builds a new generation of credit system
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
mysql设置触发器问题
解读最早的草图-图像翻译工作SketchyGAN
SolidWorks template and design library are convenient for designers to call
5.Oracle-錶空間
Edge calculation data sorting
vsCode创建自己的代码模板
About vscode, "code unreachable" will be displayed when calling sendline series functions with pwntools“
Empire help
[Chongqing Guangdong education] 1185t administrative leadership reference test of National Open University in autumn 2018
6-4 search by serial number of linked list
H5 embedded app adapts to dark mode
Client use of Argo CD installation
Use ffmpeg to rotate, flip up and down, and flip horizontally
Cookie、Session、JWT、token四者间的区别与联系