当前位置:网站首页>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 是函数,可以重写(覆盖)。
边栏推荐
- kata container
- 【高德地图POI踩坑】AMap.PlaceSearch无法使用
- Dataframe (1): introduction and creation of dataframe
- Get class files and attributes by reflection
- Speedtree01 generator properties
- [Chongqing Guangdong education] National Open University 2018 autumn 0702-22t contemporary Chinese political system reference questions
- The problem of Chinese garbled code in the vscode output box can be solved once for life
- All English in the code
- Vscode creates its own code template
- 6-3 find the table length of the linked table
猜你喜欢

Vant Weapp SwipeCell設置多個按鈕

Paper reading report

'mongoexport 'is not an internal or external command, nor is it a runnable program or batch file.

ollvm编译出现的问题纪录

The problem of Chinese garbled code in the vscode output box can be solved once for life

Vant weave swipecell sets multiple buttons

Ret2xx---- common CTF template proposition in PWN

5.Oracle-錶空間

数据库Mysql全部

Positive height system
随机推荐
The “mode“ argument must be integer. Received an instance of Object
扫盲-以太网MII接口类型大全-MII、RMII、SMII、GMII、RGMII、SGMII、XGMII、XAUI、RXAUI
Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
'mongoexport 'is not an internal or external command, nor is it a runnable program or batch file.
International Open Source firmware Foundation (osff) organization
Use ffmpeg to rotate, flip up and down, and flip horizontally
.net core踩坑实践
6-3 find the table length of the linked table
2022年中纪实 -- 一个普通人的经历
6-4 search by serial number of linked list
Vscode configures the typera editor for MD
【MySQL8.0不支持表名大写-对应方案】
Client use of Argo CD installation
SolidWorks template and design library are convenient for designers to call
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
inux摄像头(mipi接口)简要说明
TypeScript入门
Getting started with typescript
Redis-02.Redis命令
SRE核心体系了解