当前位置:网站首页>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踩坑实践
- Written examination notes
- NVM Downloading npm version 6.7.0... Error
- 7. Oracle table structure
- Preemption of CFS scheduling
- Orin 两种刷机方式
- 扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
- [nvidia] CUDA_ VISIBLE_ DEVICES
- Genesis builds a new generation of credit system
- What is linting
猜你喜欢

confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)

4. Oracle redo log file management

Orin 两种刷机方式

Redis-01. First meet redis

5. Oracle TABLESPACE

7. Oracle table structure

Idea debug failed

Client use of Argo CD installation

Build a microservice cluster environment locally and learn to deploy automatically

Genesis builds a new generation of credit system
随机推荐
NVM Downloading npm version 6.7.0... Error
. Net core stepping on the pit practice
MQClientException: No route info of this topic: type_ topic
Redis-01.初识Redis
Configuration method and configuration file of SolidWorks GB profile library
International Open Source firmware Foundation (osff) organization
扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
how to understand the “model independent.“
Rehabilitation type force deduction brush question notes D2
How to answer when you encounter a jet on CSDN?
【MySQL8.0不支持表名大写-对应方案】
[Chongqing Guangdong education] National Open University 2018 autumn 0702-22t contemporary Chinese political system reference questions
Qt项目中的日志库log4qt使用
6-3 find the table length of the linked table
Xavier CPU & GPU 高负载功耗测试
Sum of two numbers, the numbers in the array are converted to decimal, added, and output inversely
Build a microservice cluster environment locally and learn to deploy automatically
TypeScript入门
mysql设置触发器问题
La redirection de l'applet Wechat ne déclenche pas onload